godot_interfaces.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. :article_outdated: True
  2. .. _doc_godot_interfaces:
  3. Godot interfaces
  4. ================
  5. Often one needs scripts that rely on other objects for features. There
  6. are 2 parts to this process:
  7. 1. Acquiring a reference to the object that presumably has the features.
  8. 2. Accessing the data or logic from the object.
  9. The rest of this tutorial outlines the various ways of doing all this.
  10. Acquiring object references
  11. ---------------------------
  12. For all :ref:`Object <class_Object>`\s, the most basic way of referencing them
  13. is to get a reference to an existing object from another acquired instance.
  14. .. tabs::
  15. .. code-tab:: gdscript GDScript
  16. var obj = node.object # Property access.
  17. var obj = node.get_object() # Method access.
  18. .. code-tab:: csharp
  19. GodotObject obj = node.Object; // Property access.
  20. GodotObject obj = node.GetObject(); // Method access.
  21. The same principle applies for :ref:`RefCounted <class_RefCounted>` objects.
  22. While users often access :ref:`Node <class_Node>` and
  23. :ref:`Resource <class_Resource>` this way, alternative measures are available.
  24. Instead of property or method access, one can get Resources by load
  25. access.
  26. .. tabs::
  27. .. code-tab:: gdscript GDScript
  28. var preres = preload(path) # Load resource during scene load
  29. var res = load(path) # Load resource when program reaches statement
  30. # Note that users load scenes and scripts, by convention, with PascalCase
  31. # names (like typenames), often into constants.
  32. const MyScene : = preload("my_scene.tscn") as PackedScene # Static load
  33. const MyScript : = preload("my_script.gd") as Script
  34. # This type's value varies, i.e. it is a variable, so it uses snake_case.
  35. export(Script) var script_type: Script
  36. # If need an "export const var" (which doesn't exist), use a conditional
  37. # setter for a tool script that checks if it's executing in the editor.
  38. tool # Must place at top of file.
  39. # Must configure from the editor, defaults to null.
  40. export(Script) var const_script setget set_const_script
  41. func set_const_script(value):
  42. if Engine.is_editor_hint():
  43. const_script = value
  44. # Warn users if the value hasn't been set.
  45. func _get_configuration_warning():
  46. if not const_script:
  47. return "Must initialize property 'const_script'."
  48. return ""
  49. .. code-tab:: csharp
  50. // Tool script added for the sake of the "const [Export]" example.
  51. [Tool]
  52. public MyType
  53. {
  54. // Property initializations load during Script instancing, i.e. .new().
  55. // No "preload" loads during scene load exists in C#.
  56. // Initialize with a value. Editable at runtime.
  57. public Script MyScript = GD.Load<Script>("MyScript.cs");
  58. // Initialize with same value. Value cannot be changed.
  59. public readonly Script MyConstScript = GD.Load<Script>("MyScript.cs");
  60. // Like 'readonly' due to inaccessible setter.
  61. // But, value can be set during constructor, i.e. MyType().
  62. public Script Library { get; } = GD.Load<Script>("res://addons/plugin/library.gd");
  63. // If need a "const [Export]" (which doesn't exist), use a
  64. // conditional setter for a tool script that checks if it's executing
  65. // in the editor.
  66. private PackedScene _enemyScn;
  67. [Export]
  68. public PackedScene EnemyScn
  69. {
  70. get { return _enemyScn; }
  71. set
  72. {
  73. if (Engine.IsEditorHint())
  74. {
  75. _enemyScn = value;
  76. }
  77. }
  78. };
  79. // Warn users if the value hasn't been set.
  80. public String _GetConfigurationWarning()
  81. {
  82. if (EnemyScn == null)
  83. return "Must initialize property 'EnemyScn'.";
  84. return "";
  85. }
  86. }
  87. Note the following:
  88. 1. There are many ways in which a language can load such resources.
  89. 2. When designing how objects will access data, don't forget
  90. that one can pass resources around as references as well.
  91. 3. Keep in mind that loading a resource fetches the cached resource
  92. instance maintained by the engine. To get a new object, one must
  93. :ref:`duplicate <class_Resource_method_duplicate>` an existing reference
  94. or instantiate one from scratch with ``new()``.
  95. Nodes likewise have an alternative access point: the SceneTree.
  96. .. tabs::
  97. .. code-tab:: gdscript GDScript
  98. extends Node
  99. # Slow.
  100. func dynamic_lookup_with_dynamic_nodepath():
  101. print(get_node("Child"))
  102. # Faster. GDScript only.
  103. func dynamic_lookup_with_cached_nodepath():
  104. print($Child)
  105. # Fastest. Doesn't break if node moves later.
  106. # Note that `@onready` annotation is GDScript only.
  107. # Other languages must do...
  108. # var child
  109. # func _ready():
  110. # child = get_node("Child")
  111. @onready var child = $Child
  112. func lookup_and_cache_for_future_access():
  113. print(child)
  114. # Delegate reference assignment to an external source.
  115. # Con: need to perform a validation check.
  116. # Pro: node makes no requirements of its external structure.
  117. # 'prop' can come from anywhere.
  118. var prop
  119. func call_me_after_prop_is_initialized_by_parent():
  120. # Validate prop in one of three ways.
  121. # Fail with no notification.
  122. if not prop:
  123. return
  124. # Fail with an error message.
  125. if not prop:
  126. printerr("'prop' wasn't initialized")
  127. return
  128. # Fail and terminate.
  129. # Note: Scripts run from a release export template don't
  130. # run `assert` statements.
  131. assert(prop, "'prop' wasn't initialized")
  132. # Use an autoload.
  133. # Dangerous for typical nodes, but useful for true singleton nodes
  134. # that manage their own data and don't interfere with other objects.
  135. func reference_a_global_autoloaded_variable():
  136. print(globals)
  137. print(globals.prop)
  138. print(globals.my_getter())
  139. .. code-tab:: csharp
  140. using Godot;
  141. using System;
  142. using System.Diagnostics;
  143. public class MyNode : Node
  144. {
  145. // Slow
  146. public void DynamicLookupWithDynamicNodePath()
  147. {
  148. GD.Print(GetNode("Child"));
  149. }
  150. // Fastest. Lookup node and cache for future access.
  151. // Doesn't break if node moves later.
  152. private Node _child;
  153. public void _Ready()
  154. {
  155. _child = GetNode("Child");
  156. }
  157. public void LookupAndCacheForFutureAccess()
  158. {
  159. GD.Print(_child);
  160. }
  161. // Delegate reference assignment to an external source.
  162. // Con: need to perform a validation check.
  163. // Pro: node makes no requirements of its external structure.
  164. // 'prop' can come from anywhere.
  165. public object Prop { get; set; }
  166. public void CallMeAfterPropIsInitializedByParent()
  167. {
  168. // Validate prop in one of three ways.
  169. // Fail with no notification.
  170. if (prop == null)
  171. {
  172. return;
  173. }
  174. // Fail with an error message.
  175. if (prop == null)
  176. {
  177. GD.PrintErr("'Prop' wasn't initialized");
  178. return;
  179. }
  180. // Fail with an exception.
  181. if (prop == null)
  182. {
  183. throw new InvalidOperationException("'Prop' wasn't initialized.");
  184. }
  185. // Fail and terminate.
  186. // Note: Scripts run from a release export template don't
  187. // run `Debug.Assert` statements.
  188. Debug.Assert(Prop, "'Prop' wasn't initialized");
  189. }
  190. // Use an autoload.
  191. // Dangerous for typical nodes, but useful for true singleton nodes
  192. // that manage their own data and don't interfere with other objects.
  193. public void ReferenceAGlobalAutoloadedVariable()
  194. {
  195. MyNode globals = GetNode<MyNode>("/root/Globals");
  196. GD.Print(globals);
  197. GD.Print(globals.Prop);
  198. GD.Print(globals.MyGetter());
  199. }
  200. };
  201. .. _doc_accessing_data_or_logic_from_object:
  202. Accessing data or logic from an object
  203. --------------------------------------
  204. Godot's scripting API is duck-typed. This means that if a script executes an
  205. operation, Godot doesn't validate that it supports the operation by **type**.
  206. It instead checks that the object **implements** the individual method.
  207. For example, the :ref:`CanvasItem <class_CanvasItem>` class has a ``visible``
  208. property. All properties exposed to the scripting API are in fact a setter and
  209. getter pair bound to a name. If one tried to access
  210. :ref:`CanvasItem.visible <class_CanvasItem_property_visible>`, then Godot would do the
  211. following checks, in order:
  212. - If the object has a script attached, it will attempt to set the property
  213. through the script. This leaves open the opportunity for scripts to override
  214. a property defined on a base object by overriding the setter method for the
  215. property.
  216. - If the script does not have the property, it performs a HashMap lookup in
  217. the ClassDB for the "visible" property against the CanvasItem class and all
  218. of its inherited types. If found, it will call the bound setter or getter.
  219. For more information about HashMaps, see the
  220. :ref:`data preferences <doc_data_preferences>` docs.
  221. - If not found, it does an explicit check to see if the user wants to access
  222. the "script" or "meta" properties.
  223. - If not, it checks for a ``_set``/``_get`` implementation (depending on type
  224. of access) in the CanvasItem and its inherited types. These methods can
  225. execute logic that gives the impression that the Object has a property. This
  226. is also the case with the ``_get_property_list`` method.
  227. - Note that this happens even for non-legal symbol names such as in the
  228. case of :ref:`TileSet <class_TileSet>`'s "1/tile_name" property. This
  229. refers to the name of the tile with ID 1, i.e.
  230. ``TileSet.tile_get_name(1)``.
  231. As a result, this duck-typed system can locate a property either in the script,
  232. the object's class, or any class that object inherits, but only for things
  233. which extend Object.
  234. Godot provides a variety of options for performing runtime checks on these
  235. accesses:
  236. - A duck-typed property access. These will be property checks (as described above).
  237. If the operation isn't supported by the object, execution will halt.
  238. .. tabs::
  239. .. code-tab:: gdscript GDScript
  240. # All Objects have duck-typed get, set, and call wrapper methods.
  241. get_parent().set("visible", false)
  242. # Using a symbol accessor, rather than a string in the method call,
  243. # will implicitly call the `set` method which, in turn, calls the
  244. # setter method bound to the property through the property lookup
  245. # sequence.
  246. get_parent().visible = false
  247. # Note that if one defines a _set and _get that describe a property's
  248. # existence, but the property isn't recognized in any _get_property_list
  249. # method, then the set() and get() methods will work, but the symbol
  250. # access will claim it can't find the property.
  251. .. code-tab:: csharp
  252. // All Objects have duck-typed Get, Set, and Call wrapper methods.
  253. GetParent().Set("visible", false);
  254. // C# is a static language, so it has no dynamic symbol access, e.g.
  255. // `GetParent().Visible = false` won't work.
  256. - A method check. In the case of
  257. :ref:`CanvasItem.visible <class_CanvasItem_property_visible>`, one can
  258. access the methods, ``set_visible`` and ``is_visible`` like any other method.
  259. .. tabs::
  260. .. code-tab:: gdscript GDScript
  261. var child = get_child(0)
  262. # Dynamic lookup.
  263. child.call("set_visible", false)
  264. # Symbol-based dynamic lookup.
  265. # GDScript aliases this into a 'call' method behind the scenes.
  266. child.set_visible(false)
  267. # Dynamic lookup, checks for method existence first.
  268. if child.has_method("set_visible"):
  269. child.set_visible(false)
  270. # Cast check, followed by dynamic lookup.
  271. # Useful when you make multiple "safe" calls knowing that the class
  272. # implements them all. No need for repeated checks.
  273. # Tricky if one executes a cast check for a user-defined type as it
  274. # forces more dependencies.
  275. if child is CanvasItem:
  276. child.set_visible(false)
  277. child.show_on_top = true
  278. # If one does not wish to fail these checks without notifying users,
  279. # one can use an assert instead. These will trigger runtime errors
  280. # immediately if not true.
  281. assert(child.has_method("set_visible"))
  282. assert(child.is_in_group("offer"))
  283. assert(child is CanvasItem)
  284. # Can also use object labels to imply an interface, i.e. assume it
  285. # implements certain methods.
  286. # There are two types, both of which only exist for Nodes: Names and
  287. # Groups.
  288. # Assuming...
  289. # A "Quest" object exists and 1) that it can "complete" or "fail" and
  290. # that it will have text available before and after each state...
  291. # 1. Use a name.
  292. var quest = $Quest
  293. print(quest.text)
  294. quest.complete() # or quest.fail()
  295. print(quest.text) # implied new text content
  296. # 2. Use a group.
  297. for a_child in get_children():
  298. if a_child.is_in_group("quest"):
  299. print(quest.text)
  300. quest.complete() # or quest.fail()
  301. print(quest.text) # implied new text content
  302. # Note that these interfaces are project-specific conventions the team
  303. # defines (which means documentation! But maybe worth it?).
  304. # Any script that conforms to the documented "interface" of the name or
  305. # group can fill in for it.
  306. .. code-tab:: csharp
  307. Node child = GetChild(0);
  308. // Dynamic lookup.
  309. child.Call("SetVisible", false);
  310. // Dynamic lookup, checks for method existence first.
  311. if (child.HasMethod("SetVisible"))
  312. {
  313. child.Call("SetVisible", false);
  314. }
  315. // Use a group as if it were an "interface", i.e. assume it implements
  316. // certain methods.
  317. // Requires good documentation for the project to keep it reliable
  318. // (unless you make editor tools to enforce it at editor time).
  319. // Note, this is generally not as good as using an actual interface in
  320. // C#, but you can't set C# interfaces from the editor since they are
  321. // language-level features.
  322. if (child.IsInGroup("Offer"))
  323. {
  324. child.Call("Accept");
  325. child.Call("Reject");
  326. }
  327. // Cast check, followed by static lookup.
  328. CanvasItem ci = GetParent() as CanvasItem;
  329. if (ci != null)
  330. {
  331. ci.SetVisible(false);
  332. // useful when you need to make multiple safe calls to the class
  333. ci.ShowOnTop = true;
  334. }
  335. // If one does not wish to fail these checks without notifying users,
  336. // one can use an assert instead. These will trigger runtime errors
  337. // immediately if not true.
  338. Debug.Assert(child.HasMethod("set_visible"));
  339. Debug.Assert(child.IsInGroup("offer"));
  340. Debug.Assert(CanvasItem.InstanceHas(child));
  341. // Can also use object labels to imply an interface, i.e. assume it
  342. // implements certain methods.
  343. // There are two types, both of which only exist for Nodes: Names and
  344. // Groups.
  345. // Assuming...
  346. // A "Quest" object exists and 1) that it can "Complete" or "Fail" and
  347. // that it will have Text available before and after each state...
  348. // 1. Use a name.
  349. Node quest = GetNode("Quest");
  350. GD.Print(quest.Get("Text"));
  351. quest.Call("Complete"); // or "Fail".
  352. GD.Print(quest.Get("Text")); // Implied new text content.
  353. // 2. Use a group.
  354. foreach (Node AChild in GetChildren())
  355. {
  356. if (AChild.IsInGroup("quest"))
  357. {
  358. GD.Print(quest.Get("Text"));
  359. quest.Call("Complete"); // or "Fail".
  360. GD.Print(quest.Get("Text")); // Implied new text content.
  361. }
  362. }
  363. // Note that these interfaces are project-specific conventions the team
  364. // defines (which means documentation! But maybe worth it?).
  365. // Any script that conforms to the documented "interface" of the
  366. // name or group can fill in for it. Also note that in C#, these methods
  367. // will be slower than static accesses with traditional interfaces.
  368. - Outsource the access to a :ref:`Callable <class_Callable>`. These may be useful
  369. in cases where one needs the max level of freedom from dependencies. In
  370. this case, one relies on an external context to setup the method.
  371. .. tabs::
  372. .. code-tab:: gdscript GDScript
  373. # child.gd
  374. extends Node
  375. var fn = null
  376. func my_method():
  377. if fn:
  378. fn.call()
  379. # parent.gd
  380. extends Node
  381. @onready var child = $Child
  382. func _ready():
  383. child.fn = print_me
  384. child.my_method()
  385. func print_me():
  386. print(name)
  387. .. code-tab:: csharp
  388. // Child.cs
  389. using Godot;
  390. public partial class Child : Node
  391. {
  392. public Callable? Callable { get; set; }
  393. public void MyMethod()
  394. {
  395. Callable?.Call();
  396. }
  397. }
  398. // Parent.cs
  399. using Godot;
  400. public partial class Parent : Node
  401. {
  402. private Child _child;
  403. public void _Ready()
  404. {
  405. _child = GetNode<Child>("Child");
  406. _child.Callable = Callable.From(PrintMe);
  407. _child.MyMethod();
  408. }
  409. public void PrintMe() {
  410. {
  411. GD.Print(Name);
  412. }
  413. }
  414. These strategies contribute to Godot's flexible design. Between them, users
  415. have a breadth of tools to meet their specific needs.