Skip to main content
Flow Core Documentation
Menu

Stack

Composing Logic with Stacks

Layer temporary modes and restore the previous mode with explicit push and pop lifecycle.

Audience
Gameplay and UI developers
Time
12 minutes
Requires
A last-in, first-out mode requirement
Modules
Stack
On this page

Use Stack when a temporary layer covers the current mode and the previous mode must resume after the layer is removed. Typical uses include nested menus, dialogue input, inspect mode, and tutorial overlays.

Mental model

Stack Root owns the ordered collection. Stack Layer is the item that can be pushed.

Only the top layer receives Update. A layer below it remains paused until every layer above it is popped.

Lifecycle outputMeaning
PushThe layer is added to the stack.
ResumeThe layer becomes the active top layer.
UpdateThe current top layer receives its configured update.
PauseThe layer stops being top.
PopThe layer is removed.

Build a UI stack

  1. Add a Stack Root and name it UiStack.
  2. Configure its update interval and interruption policy.
  3. Add Stack Layers named Gameplay, PauseMenu, and Options.
  4. Set every layer’s Root Name to UiStack.
  5. Put show or enable work in Resume.
  6. Put hide or disable work in Pause.
  7. Put one-time setup and cleanup in Push and Pop.
  8. Enable unique layers when a screen must not be pushed twice.
Image 03_04_01Screenshot production note

此处插图应该是 UiStack 的 Stack Root 与 Gameplay、PauseMenu、Options 三个 Stack Layer 截图;显示 Push/Resume/Update/Pause/Pop 输出,并用编号标出 Gameplay→PauseMenu→Options 的入栈顺序。

Expected runtime order:

Gameplay active
Push PauseMenu -> Gameplay pauses, PauseMenu resumes
Push Options   -> PauseMenu pauses, Options resumes
Pop Options    -> Options exits, PauseMenu resumes
Pop PauseMenu  -> PauseMenu exits, Gameplay resumes

When Stack is not the owner

Do not use Stack when two systems should update concurrently. Independent movement and upper-body state families are better expressed as separate FSM groups or processes. Do not use Stack merely because several nodes form a list.

Root and layer names are runtime relationships. Keep them stable and add comments around cross-process pushes so target ownership remains visible.

Common mistakes

SymptomCheck
A layer never joins the stackIts Root Name must match the Stack Root title.
Lower layers keep updatingConfirm their work is connected to the layer Update lifecycle rather than a separate Trigger.
The same menu appears twiceEnable unique layers where duplicates are invalid.
Push waits unexpectedlyCheck the root’s Update interruption mode and active Update work.
Two concurrent systems block each otherThey should not share one last-in, first-out stack.