Godot interfaces

    1. Acquiring a reference to the object that presumably has the features.
    2. Accessing the data or logic from the object.

    The rest of this tutorial outlines the various ways of doing all this.

    For all s, the most basic way of referencing them is to get a reference to an existing object from another acquired instance.

    GDScript

    C#

    1. Object obj = node.GetObject(); // Method access.

    The same principle applies for Reference objects. While users often access and Resource this way, alternative measures are available.

    Instead of property or method access, one can get Resources by load access.

    GDScript

    C#

    1. var preres = preload(path) # Load resource during scene load
    2. var res = load(path) # Load resource when program reaches statement
    3. # Note that users load scenes and scripts, by convention, with PascalCase
    4. # names (like typenames), often into constants.
    5. const MyScene : = preload("my_scene.tscn") as PackedScene # Static load
    6. const MyScript : = preload("my_script.gd") as Script
    7. # This type's value varies, i.e. it is a variable, so it uses snake_case.
    8. export(Script) var script_type: Script
    9. # If need an "export const var" (which doesn't exist), use a conditional
    10. # setter for a tool script that checks if it's executing in the editor.
    11. tool # Must place at top of file.
    12. # Must configure from the editor, defaults to null.
    13. export(Script) var const_script setget set_const_script
    14. func set_const_script(value):
    15. if Engine.is_editor_hint():
    16. const_script = value
    17. # Warn users if the value hasn't been set.
    18. func _get_configuration_warning():
    19. if not const_script:
    20. return "Must initialize property 'const_script'."
    21. return ""
    1. // Tool script added for the sake of the "const [Export]" example.
    2. [Tool]
    3. public MyType
    4. {
    5. // Property initializations load during Script instancing, i.e. .new().
    6. // No "preload" loads during scene load exists in C#.
    7. // Initialize with a value. Editable at runtime.
    8. public Script MyScript = GD.Load<Script>("MyScript.cs");
    9. // Initialize with same value. Value cannot be changed.
    10. public readonly Script MyConstScript = GD.Load<Script>("MyScript.cs");
    11. // Like 'readonly' due to inaccessible setter.
    12. // But, value can be set during constructor, i.e. MyType().
    13. public Script Library { get; } = GD.Load<Script>("res://addons/plugin/library.gd");
    14. // If need a "const [Export]" (which doesn't exist), use a
    15. // conditional setter for a tool script that checks if it's executing
    16. // in the editor.
    17. private PackedScene _enemyScn;
    18. [Export]
    19. public PackedScene EnemyScn
    20. {
    21. get { return _enemyScn; }
    22. set
    23. {
    24. if (Engine.IsEditorHint())
    25. {
    26. _enemyScn = value;
    27. }
    28. }
    29. };
    30. // Warn users if the value hasn't been set.
    31. public String _GetConfigurationWarning()
    32. {
    33. if (EnemyScn == null)
    34. return "Must initialize property 'EnemyScn'.";
    35. return "";
    36. }
    37. }
    1. There are many ways in which a language can load such resources.
    2. When designing how objects will access data, don’t forget that one can pass resources around as references as well.
    3. Keep in mind that loading a resource fetches the cached resource instance maintained by the engine. To get a new object, one must an existing reference or instantiate one from scratch with new().

    Nodes likewise have an alternative access point: the SceneTree.

    GDScript

    C#

    1. public class MyNode
    2. {
    3. // Slow, dynamic lookup with dynamic NodePath.
    4. public void Method1()
    5. {
    6. GD.Print(GetNode(NodePath("Child")));
    7. }
    8. // Fastest. Lookup node and cache for future access.
    9. // Doesn't break if node moves later.
    10. public Node Child;
    11. public void _Ready()
    12. {
    13. Child = GetNode(NodePath("Child"));
    14. public void Method2()
    15. {
    16. GD.Print(Child);
    17. // Delegate reference assignment to an external source
    18. // Con: need to perform a validation check
    19. // Pro: node makes no requirements of its external structure.
    20. // 'prop' can come from anywhere.
    21. public object Prop;
    22. public void CallMeAfterPropIsInitializedByParent()
    23. {
    24. // Validate prop in one of three ways.
    25. // Fail with no notification.
    26. if (prop == null)
    27. {
    28. return;
    29. }
    30. // Fail with an error message.
    31. if (prop == null)
    32. {
    33. GD.PrintErr("'Prop' wasn't initialized");
    34. return;
    35. }
    36. // Fail and terminate.
    37. Debug.Assert(Prop, "'Prop' wasn't initialized");
    38. }
    39. // Use an autoload.
    40. // Dangerous for typical nodes, but useful for true singleton nodes
    41. // that manage their own data and don't interfere with other objects.
    42. public void ReferenceAGlobalAutoloadedVariable()
    43. {
    44. Node globals = GetNode(NodePath("/root/Globals"));
    45. GD.Print(globals);
    46. GD.Print(globals.prop);
    47. GD.Print(globals.my_getter());
    48. }
    49. };

    Accessing data or logic from an object

    Godot’s scripting API is duck-typed. This means that if a script executes an operation, Godot doesn’t validate that it supports the operation by type. It instead checks that the object implements the individual method.

    For example, the CanvasItem class has a visible property. All properties exposed to the scripting API are in fact a setter and getter pair bound to a name. If one tried to access , then Godot would do the following checks, in order:

    • If the object has a script attached, it will attempt to set the property through the script. This leaves open the opportunity for scripts to override a property defined on a base object by overriding the setter method for the property.
    • If the script does not have the property, it performs a HashMap lookup in the ClassDB for the “visible” property against the CanvasItem class and all of its inherited types. If found, it will call the bound setter or getter. For more information about HashMaps, see the data preferences docs.
    • If not found, it does an explicit check to see if the user wants to access the “script” or “meta” properties.
    • If not, it checks for a _set/_get implementation (depending on type of access) in the CanvasItem and its inherited types. These methods can execute logic that gives the impression that the Object has a property. This is also the case with the _get_property_list method.
      • Note that this happens even for non-legal symbol names such as in the case of ‘s “1/tile_name” property. This refers to the name of the tile with ID 1, i.e. TileSet.tile_get_name(1).

    As a result, this duck-typed system can locate a property either in the script, the object’s class, or any class that object inherits, but only for things which extend Object.

    Godot provides a variety of options for performing runtime checks on these accesses:

    • A duck-typed property access. These will property check (as described above). If the operation isn’t supported by the object, execution will halt.

      C#

      1. # All Objects have duck-typed get, set, and call wrapper methods.
      2. get_parent().set("visible", false)
      3. # Using a symbol accessor, rather than a string in the method call,
      4. # will implicitly call the `set` method which, in turn, calls the
      5. # setter method bound to the property through the property lookup
      6. # sequence.
      7. get_parent().visible = false
      8. # Note that if one defines a _set and _get that describe a property's
      9. # existence, but the property isn't recognized in any _get_property_list
      10. # method, then the set() and get() methods will work, but the symbol
      11. # access will claim it can't find the property.
      1. // All Objects have duck-typed Get, Set, and Call wrapper methods.
      2. GetParent().Set("visible", false);
      3. // C# is a static language, so it has no dynamic symbol access, e.g.
      4. // `GetParent().Visible = false` won't work.
    • A method check. In the case of , one can access the methods, set_visible and is_visible like any other method.

      GDScript

      C#

      1. Node child = GetChild(0);
      2. // Dynamic lookup.
      3. child.Call("SetVisible", false);
      4. // Dynamic lookup, checks for method existence first.
      5. if (child.HasMethod("SetVisible"))
      6. {
      7. child.Call("SetVisible", false);
      8. }
      9. // Use a group as if it were an "interface", i.e. assume it implements certain methods
      10. // requires good documentation for the project to keep it reliable (unless you make
      11. // editor tools to enforce it at editor time.
      12. // Note, this is generally not as good as using an actual interface in C#,
      13. // but you can't set C# interfaces from the editor since they are
      14. // language-level features.
      15. if (child.IsInGroup("Offer"))
      16. {
      17. child.Call("Accept");
      18. child.Call("Reject");
      19. }
      20. // Cast check, followed by static lookup.
      21. CanvasItem ci = GetParent() as CanvasItem;
      22. {
      23. // useful when you need to make multiple safe calls to the class
      24. ci.ShowOnTop = true;
      25. }
      26. // If one does not wish to fail these checks without notifying users, one
      27. // can use an assert instead. These will trigger runtime errors
      28. // immediately if not true.
      29. Debug.Assert(child.HasMethod("set_visible"));
      30. Debug.Assert(child.IsInGroup("offer"));
      31. Debug.Assert(CanvasItem.InstanceHas(child));
      32. // Can also use object labels to imply an interface, i.e. assume it implements certain methods.
      33. // There are two types, both of which only exist for Nodes: Names and Groups
      34. // Assuming...
      35. // A "Quest" object exists and 1) that it can "Complete" or "Fail" and
      36. // that it will have Text available before and after each state...
      37. // 1. Use a name.
      38. Node quest = GetNode("Quest");
      39. GD.Print(quest.Get("Text"));
      40. quest.Call("Complete"); // or "Fail".
      41. GD.Print(quest.Get("Text")); // Implied new text content.
      42. // 2. Use a group.
      43. foreach (Node AChild in GetChildren())
      44. {
      45. if (AChild.IsInGroup("quest"))
      46. {
      47. GD.Print(quest.Get("Text"));
      48. quest.Call("Complete"); // or "Fail".
      49. GD.Print(quest.Get("Text")); // Implied new text content.
      50. }
      51. }
      52. // Note that these interfaces are project-specific conventions the team
      53. // defines (which means documentation! But maybe worth it?)..
      54. // Any script that conforms to the documented "interface" of the
      55. // name/group can fill in for it. Also note that in C#, these methods
      56. // will be slower than static accesses with traditional interfaces.
    • Outsource the access to a FuncRef. These may be useful in cases where one needs the max level of freedom from dependencies. In this case, one relies on an external context to setup the method.

    GDScript

    C#

    1. # child.gd
    2. extends Node
    3. var fn = null
    4. func my_method():
    5. if fn:
    6. fn.call_func()
    7. # parent.gd
    8. extends Node
    9. onready var child = $Child
    10. func _ready():
    11. child.fn = funcref(self, "print_me")
    12. child.my_method()
    13. func print_me():
    14. print(name)
    1. // Child.cs
    2. public class Child : Node
    3. {
    4. public FuncRef FN = null;
    5. public void MyMethod()
    6. {
    7. Debug.Assert(FN != null);
    8. FN.CallFunc();
    9. }
    10. }
    11. // Parent.cs
    12. public class Parent : Node
    13. {
    14. public Node Child;
    15. public void _Ready()
    16. {
    17. Child = GetNode("Child");
    18. Child.Set("FN", GD.FuncRef(this, "PrintMe"));
    19. Child.MyMethod();
    20. }
    21. public void PrintMe() {
    22. {
    23. GD.Print(GetClass());
    24. }
    25. }

    These strategies contribute to Godot’s flexible design. Between them, users have a breadth of tools to meet their specific needs.