State management with State node

If you haven’t completed the previous tutorials yet, please complete them starting from Basic Usage.

This time, let’s use the State node to manage states.
The contents are as follows:

  • Create a GameObject with a Logic Behavior component.
  • Open and edit the graph in the Logic Editor window.
  • The State node detects when the button is clicked within a certain time.

Create a new scene

If you have other scenes open, it will be confusing, so create a new scene.

  • Select File > New Scene from the menu.
  • In the New Scene window, select Basic (URP).
  • Click the Create button

Creating a GameObject with Logic Behavior

Create a GameObject with a Logic Behavior component added to the Scene.

  • Click the + button in Hierarchy.
  • Select Logic Toolkit > Logic Behavior from the menu.
  • Confirm the name by pressing Enter.

Opening the Logic Editor window

Graphs of Logic Behavior components are edited in the Logic Editor window.
If the window is not displayed, select a GameObject and click the “Edit” button in the Inspector to display it.

  • Select the Logic Behavior object
  • Click the Edit button on the Logic Behavior component in the Inspector window.

If the Logic Editor window is already displayed, the graph will switch in conjunction with selecting a GameObject in the Hierarchy window.

Creating a State node

Create a State node.

  • Drag the output execute port of the Start node
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Select State
  • Confirm the node name with the Enter key

What is a State node?

A State node represents the state of an object.
For example, possible actions for an enemy NPC might be “patrol and search state,” “target tracking state,” “target attack state,” etc.

The Logic Toolkit’s State node allows you to manage states by combining ActionComponents, TaskComponents, EventComponents, etc. and executing them simultaneously.

Add Wait For Seconds to State

Add Wait For Seconds to the State node.

  • Click the + button in Components of the State node to open the menu for adding node components.
  • Select the Scripts tab.
  • Select Tasks > Wait For Seconds from the list.

Wait For Seconds setting

Here we set it to wait for 5 seconds.

  • Set the Seconds field to 5

Creating a Debug.Log Action node

The transition from Wait For Seconds Completed to Debug.Log will occur.

  • Drag the Completed port of Wait For Seconds
  • Drop it near the right side to open the node creation menu
  • Select Actions > Debug > Debug.Log from the list
  • Confirm the node name with the Enter key

Debug.Log Settings

Sets the string to be output to the console.

  • Set the Message field to Time out

Creating a Restart node

After Debug.Log, set the Restart node to return to the beginning.

  • Drag the output transition port of the Debug.Log Action node
  • Drop it near the right side to open the node creation menu
  • Select the Scripts tab
  • Select Flow Controls > Restart from the list

Add Button.onClick to State

Next, add Button.onClick to the State node.

  • Hover the mouse over the bottom edge of the State node, click the + button that appears, and open the menu for adding node components.
  • Select the Scripts tab.
  • Enter Button.onClick in the search field.
  • Select Button.onClick from the list.

As a State node can have multiple NodeComponents, it has a function that allows you to insert node components in the middle.
If you hover the mouse over the space at the beginning, between NodeComponents, or at the very end, a + button for insertion will appear.
The + button for Components will also be added at the very end.
Use whichever you prefer.

If there is no Button.onClick in the Scripts tab

The script was generated in the previous article, “Waiting for a trigger with an Event node,” so if you did not perform the previous content, add Button.onClick using the following method.

  • Hover the mouse over the bottom edge of the State node, click the + button that appears, and open the menu for adding node components.
  • Select the Members tab
  • Enter Button.onClick in the search field
  • Select onClick of UnityEngine.UI.Button from the list.
  • Wait for compilation to finish

Creating a Button

Create a Button in the scene.

  • Click the + button in Hierarchy.
  • From the menu, select UI > Button – TextMeshPro
  • When the TMP Importer window appears, click Import TMP Essentials.
  • If the TMP Importer window appears, click Import TMP Essentials

Setting Button.onClick

Set the created Button

  • Set the Target field to Button
    (You can set it by changing the reference type from Self to Value and then setting the object field, or by dragging and dropping a Button object from the Hierarchy into the Target field.)

Duplicate Debug.Log and Restart

Copy and paste the Debug.Log and Restart you have already created.

  • Select the Debug.Log Action node and Restart node
  • Enter Ctrl C(Win), ⌘ C (Mac)
  • Enter Ctrl V (Win), ⌘ V (Mac) near the right side of the Button.onClick Completed port

Connecting the Transition Wires

Connect Button.onClick to the duplicated Debug.Log Action node.

  • Drag the Completed port of Button.onClick
  • Drop it onto the input execute port of the duplicated Debug.Log Action node

Debug.Log Settings

Sets the string to be output to the console.

  • Set the Message field to Clicked

Play and check

Once you’ve done this, your graph will look like this:

Press the play button to see how it works.

  • Wait 5 seconds and confirm that Time out is displayed on the Console.
  • Click the Button and confirm that Clicked is displayed on the Console.

If the Console window is not visible, select Window > General > Console from the menu to display it.

A note about State

State nodes can be executed by connecting execution wires or transition wires, just like Action nodes and Task nodes.
They are basically handled no differently from Actions and Tasks, so States can be directly incorporated into the execution flow, such as “Action -> State -> Task -> If -> State”.

Next time

Next time, we will do Evaluate State transitions with the Signal Evaluate node

If you try the trial version and like it, you can purchase it from the Asset Store.