Manual: How to use "ScriptConditionEvaluator""InvokeScript"

Overview

“InvokeScript” allows you to use Godot’s scripting language, GDScript, as an Action,
and “ScriptConditionEvaluator” allows you to use GDScript as a Condition.
By using GDScript, you can handle behaviors in Visual Script that cannot be achieved with the preset actions or conditions.

Technical Overview

These Actions and Conditions make it possible to use methods defined in GDScript attached to child nodes of a GameObject as executable actions or conditional checks.

Step-by-Step Usage

  1. Create a Node2D as a child of the GameObject where you want to use the Action or Condition.
    image

  2. Attach a GDScript to the Node2D you just created.
    The script name can be anything.

  3. Copy and paste the following sample code.
    This code includes a method called is_player_in_x_range, which returns TRUE (triggering a transition) when an object in the “Player” group comes within 100 pixels on the X axis.
    It checks only the X coordinate, so it works even if the player is at a different height.
    This method is useful for things like traps or enemies that fall from above when the player approaches.

extends Node2D

@export var attack_range := 100.0
var player: Node2D

func _ready():
    # Automatically get the first node registered in the "Player" group
    player = get_tree().get_first_node_in_group("Player")

func is_player_in_x_range() -> bool:
    if not player:
        return false

    var diff_x = abs(global_position.x - player.global_position.x)
    return diff_x <= attack_range
  1. In any link’s “Other Conditions,” add “ScriptConditionEvaluator”,
    set the target node to the Node2D you created earlier,
    and specify the method name is_player_in_x_range.

  2. Test whether the transition works when the player approaches the object.

You can also define multiple methods within a single GDScript and use them selectively.
In this example, we used “ScriptConditionEvaluator” for clarity,
but the setup process is the same for “InvokeScript.”
Only the options differ slightly.


“Execute Script” Option: Execution Timing

The Execution Timing option determines when the script method is executed.

  • OnStateEnter: Executes the method immediately upon entering the specified state.
  • OnStateUpdate: Executes the method every frame while remaining in the state.
    (Be careful—this can be relatively heavy on performance.)
  • OnStateExit: Executes the method when leaving the specified state.