scripting_continued.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. .. _doc_scripting_continued:
  2. Scripting (continued)
  3. =====================
  4. Notifications
  5. -------------
  6. Godot has a system of notifications. These are usually not needed for
  7. scripting, as it's too low-level and virtual functions are provided for
  8. most of them. It's just good to know they exist. For example,
  9. you may add an
  10. :ref:`Object._notification() <class_Object_method__notification>`
  11. function in your script:
  12. .. tabs::
  13. .. code-tab:: gdscript GDScript
  14. func _notification(what):
  15. match what:
  16. NOTIFICATION_READY:
  17. print("This is the same as overriding _ready()...")
  18. NOTIFICATION_PROCESS:
  19. print("This is the same as overriding _process()...")
  20. .. code-tab:: csharp
  21. public override void _Notification(int what)
  22. {
  23. base._Notification(what);
  24. switch (what)
  25. {
  26. case NotificationReady:
  27. GD.Print("This is the same as overriding _Ready()...");
  28. break;
  29. case NotificationProcess:
  30. var delta = GetProcessDeltaTime();
  31. GD.Print("This is the same as overriding _Process()...");
  32. break;
  33. }
  34. }
  35. The documentation of each class in the :ref:`Class Reference <toc-class-ref>`
  36. shows the notifications it can receive. However, in most cases GDScript
  37. provides simpler overridable functions.
  38. Overridable functions
  39. ---------------------
  40. Such overridable functions, which are described as
  41. follows, can be applied to nodes:
  42. .. tabs::
  43. .. code-tab:: gdscript GDScript
  44. func _enter_tree():
  45. # When the node enters the Scene Tree, it becomes active
  46. # and this function is called. Children nodes have not entered
  47. # the active scene yet. In general, it's better to use _ready()
  48. # for most cases.
  49. pass
  50. func _ready():
  51. # This function is called after _enter_tree, but it ensures
  52. # that all children nodes have also entered the Scene Tree,
  53. # and became active.
  54. pass
  55. func _exit_tree():
  56. # When the node exits the Scene Tree, this function is called.
  57. # Children nodes have all exited the Scene Tree at this point
  58. # and all became inactive.
  59. pass
  60. func _process(delta):
  61. # This function is called every frame.
  62. pass
  63. func _physics_process(delta):
  64. # This is called every physics frame.
  65. pass
  66. .. code-tab:: csharp
  67. public override void _EnterTree()
  68. {
  69. // When the node enters the Scene Tree, it becomes active
  70. // and this function is called. Children nodes have not entered
  71. // the active scene yet. In general, it's better to use _ready()
  72. // for most cases.
  73. base._EnterTree();
  74. }
  75. public override void _Ready()
  76. {
  77. // This function is called after _enter_tree, but it ensures
  78. // that all children nodes have also entered the Scene Tree,
  79. // and became active.
  80. base._Ready();
  81. }
  82. public override void _ExitTree()
  83. {
  84. // When the node exits the Scene Tree, this function is called.
  85. // Children nodes have all exited the Scene Tree at this point
  86. // and all became inactive.
  87. base._ExitTree();
  88. }
  89. public override void _Process(float delta)
  90. {
  91. // This function is called every frame.
  92. base._Process(delta);
  93. }
  94. public override void _PhysicsProcess(float delta)
  95. {
  96. // This is called every physics frame.
  97. base._PhysicsProcess(delta);
  98. }
  99. As mentioned before, it's better to use these functions instead of
  100. the notification system.
  101. .. _doc_scripting_continued_class_name:
  102. Register scripts as classes
  103. ---------------------------
  104. Godot has a "Script Class" feature to register individual scripts with the
  105. Editor. By default, you can only access unnamed scripts by loading the file
  106. directly.
  107. You can name a script and register it as a type in the editor with the
  108. ``class_name`` keyword followed by the class's name. You may add a comma and an
  109. optional path to an image to use as an icon. You will then find your new type in
  110. the Node or Resource creation dialog.
  111. .. tabs::
  112. .. code-tab:: gdscript GDScript
  113. extends Node
  114. # Declare the class name here
  115. class_name ScriptName, "res://path/to/optional/icon.svg"
  116. func _ready():
  117. var this = ScriptName # reference to the script
  118. var cppNode = MyCppNode.new() # new instance of a class named MyCppNode
  119. cppNode.queue_free()
  120. .. image:: img/script_class_nativescript_example.png
  121. .. warning:: In Godot 3.1:
  122. - Only GDScript and NativeScript, i.e., C++ and other GDNative-powered languages, can register scripts.
  123. - Only GDScript creates global variables for each named script.