Scene Accessors

In addition to simulating the scene, the SceneRunner provides functions to access the scene’s nodes. These functions are useful for debugging and testing purposes.

For example, you can use find_child() to retrieve a specific node in the scene, and then call its methods or change its properties to test its behavior.

By using these functions, you can gain greater control over the scene and test various scenarios, making it easier to find and fix bugs and improve the overall quality of your game or application.

Function Overview

  • Function Description
    get_property Return the current value of a property.
    set_property Sets the value of the property with the specified name.
    find_child Searches for the specified node with the name in the current scene.
    invoke Executes the function specified by name in the scene and returns the result.
  • Function Description
    GetProperty Return the current value of a property.
    SetProperty Sets the value of the property with the specified name.
    FindChild Searches for the specified node with the name in the current scene.
    Invoke Executes the function specified by name in the scene and returns the result.

get_property

The get_property function returns the current value of the property from the current scene.

  • It takes the following arguments:

    # name: the name of the property
    # returns the actual value of the property
    func get_property(name: String) -> Variant:
    

    Here is an example of how to use get_property:

    var runner := scene_runner("res://test_scene.tscn")
    
    # Returns the current property `_door_color` from the scene
    var color: ColorRect = runner.get_property("_door_color")
    
  • It takes the following arguments:

    /// <summary>
    /// Returns the property by given name.
    /// </summary>
    /// <typeparam name="T">The type of the property</typeparam>
    /// <param name="name">The parameter name</param>
    /// <returns>The value of the property or throws a MissingFieldException</returns>
    /// <exception cref="MissingFieldException"/>
    public T GetProperty<T>(string name);
    

    Here is an example of how to use GetProperty:

    ISceneRunner runner = ISceneRunner.Load("res://test_scene.tscn");
    
    // Returns the current property `_door_color` from the scene
    ColorRect color = runner.GetProperty("_door_color");
    

set_property

The set_property function sets the value of a property with the specified name.

  • It takes the following arguments:

    # name: the name of the property.
    # value: the value to be assigned to the property.
    # returns true|false depending on valid property name.
    func set_property(name: String, value: Variant) -> bool:
    

    Here is an example of how to use set_property:

    var runner := scene_runner("res://test_scene.tscn")
    
    # Sets the property `_door_color` to Red
    runner.set_property("_door_color", Color.RED)
    
  • It takes the following arguments:

    /// <summary>
    /// Sets the value of the property with the specified name.
    /// </summary>
    /// <param name="name">The name of the property.</param>
    /// <param name="value">The value to set for the property.</param>
    /// <exception cref="MissingFieldException"/>
    public T SetProperty<T>(string name, Variant value);
    

    Here is an example of how to use SetProperty:

    ISceneRunner runner = ISceneRunner.Load("res://test_scene.tscn");
    
    // Sets the property `_door_color` to Red
    runner.SetProperty("_door_color", Colors.Red);
    

find_child

The find_child function searches for a node with the specified name in the current scene and returns it. If the node is not found, it returns null.

  • ## [member name] : the name of the node to find
    ## [member recursive] : enables/disables seraching recursive
    ## [member owned] : is set to true it only finds nodes who have an assigned owner
    ## [member return] : the node if find otherwise null
    func find_child(name: String, recursive := true, owned := false) -> Node:
    

    Here is an example of how to use find_child:

     var runner := scene_runner("res://test_scene.tscn")
     
     # Searchs for node `Spell` inside the scene tree
     var spell: Node = runner.find_child("Spell")
    
  • This function is not yet supported in C#.
    

invoke

The invoke function runs the function specified by given name in the scene and returns the result.

  • It takes the following arguments:

    # name: the name of the function to execute
    # optional function args 0..9
    # return: the function result
    func invoke(name: String, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9):
    

    Here is an example of how to use invoke:

    var runner := scene_runner("res://test_scene.tscn")
    
    # Invokes the function `start_color_cycle`
    runner.invoke("start_color_cycle")
    
  • It takes the following arguments:

    /// <summary>
    /// Invokes the method by given name and arguments.
    /// </summary>
    /// <param name="name">The name of method to invoke</param>
    /// <param name="args">The function arguments</param>
    /// <returns>The return value of invoked method</returns>
    /// <exception cref="MissingMethodException"/>
    public object Invoke(string name, params object[] args);
    

    Here is an example of how to use Invoke:

    ISceneRunner runner = ISceneRunner.Load("res://test_scene.tscn");
    
    // Invokes the function `start_color_cycle`
    runner.Invoke("start_color_cycle");
    

document version v4.4.0


Copyright © 2021-2024 Mike Schulze. Distributed by an MIT license.