Write and edit overridable_functions.rst, remove scripting_continued.rst

This commit is contained in:
Nathan Lovato
2020-11-08 09:47:57 -06:00
parent 5a21563707
commit 5450fa236c
5 changed files with 149 additions and 122 deletions

View File

@@ -10,7 +10,6 @@ Step by step
instancing
instancing_continued
scripting
scripting_continued
signals
your_first_game
exporting

View File

@@ -1,118 +0,0 @@
.. _doc_scripting_continued:
Scripting (continued)
=====================
Notifications
-------------
Godot has a low-level notifications system. You don't are usually not needed for
scripting, as it's too low-level and virtual functions are provided for
most of them. You can learn more about how notifications work under the hood in :ref:`doc_godot_notifications`.
.. tabs::
.. code-tab:: gdscript GDScript
func _notification(what):
match what:
NOTIFICATION_READY:
print("This is the same as overriding _ready()...")
NOTIFICATION_PROCESS:
print("This is the same as overriding _process()...")
.. code-tab:: csharp
public override void _Notification(int what)
{
base._Notification(what);
switch (what)
{
case NotificationReady:
GD.Print("This is the same as overriding _Ready()...");
break;
case NotificationProcess:
var delta = GetProcessDeltaTime();
GD.Print("This is the same as overriding _Process()...");
break;
}
}
The documentation of each class in the :ref:`Class Reference <toc-class-ref>`
shows the notifications it can receive. However, in most cases GDScript
provides simpler overridable functions.
Overridable functions
---------------------
Such overridable functions, which are described as
follows, can be applied to nodes:
.. tabs::
.. code-tab:: gdscript GDScript
func _enter_tree():
# When the node enters the Scene Tree, it becomes active
# and this function is called. Children nodes have not entered
# the active scene yet. In general, it's better to use _ready()
# for most cases.
pass
func _ready():
# This function is called after _enter_tree, but it ensures
# that all children nodes have also entered the Scene Tree,
# and became active.
pass
func _exit_tree():
# When the node exits the Scene Tree, this function is called.
# Children nodes have all exited the Scene Tree at this point
# and all became inactive.
pass
func _process(delta):
# This function is called every frame.
pass
func _physics_process(delta):
# This is called every physics frame.
pass
.. code-tab:: csharp
public override void _EnterTree()
{
// When the node enters the Scene Tree, it becomes active
// and this function is called. Children nodes have not entered
// the active scene yet. In general, it's better to use _ready()
// for most cases.
base._EnterTree();
}
public override void _Ready()
{
// This function is called after _enter_tree, but it ensures
// that all children nodes have also entered the Scene Tree,
// and became active.
base._Ready();
}
public override void _ExitTree()
{
// When the node exits the Scene Tree, this function is called.
// Children nodes have all exited the Scene Tree at this point
// and all became inactive.
base._ExitTree();
}
public override void _Process(float delta)
{
// This function is called every frame.
base._Process(delta);
}
public override void _PhysicsProcess(float delta)
{
// This is called every physics frame.
base._PhysicsProcess(delta);
}

View File

@@ -25,9 +25,9 @@ sessions and what information we want to keep from those objects. For
this tutorial, we will use groups to mark and handle objects to be saved,
but other methods are certainly possible.
We will start by adding objects we wish to save to the "Persist" group.
As in the :ref:`doc_scripting_continued` tutorial, we can do this through
either the GUI or script. Let's add the relevant nodes using the GUI:
We will start by adding objects we wish to save to the "Persist" group. we can
do this through either the GUI or script. Let's add the relevant nodes using the
GUI:
.. image:: img/groups.png

View File

@@ -38,6 +38,7 @@ below will help you make the most of Godot.
idle_and_physics_processing
groups
nodes_and_scene_instances
overridable_functions
cross_language_scripting
creating_script_templates
change_scenes_manually

View File

@@ -0,0 +1,145 @@
.. _doc_overridable_functions:
Overridable functions
=====================
Godot's Node class provides virtual functions you can override to update nodes
every frame or on specific events, like when they enter the scene tree.
This document presents the ones you'll use most often.
.. seealso:: Under the hood, these functions rely on Godot's uses a low-level
notifications system. To learn more about it, see
:ref:`doc_godot_notifications`.
Two functions allow you to initialize and get nodes, besides the class's
constructor: ``_enter_tree()`` and ``_ready()``.
When the node enters the Scene Tree, it becomes active and the engine calls its
``_enter_tree()`` method. Children may not be part of the active scene yet. As
you can remove and re-add nodes to the scene tree, this function may be called
multiple times throughout a node's lifetime.
Most of the time, you'll use ``_ready()`` instead. This function is called only
once in a node's lifetime, after ``_enter_tree()``. It ensures that all children
have entered the scene tree first, so you can safely call ``get_node()`` in it.
.. seealso:: To learn more about getting node references, read
:ref:`doc_nodes_and_scene_instances`.
Another related callback is ``_exit_tree()``, which the engine calls every time
a node exits the scene tree. This can be when you call :ref:`Node.remove_child()
<class_Node_method_remove_child>` or when you free a node.
.. tabs::
.. code-tab:: gdscript GDScript
# Called every time the node enters the scene tree.
func _enter_tree():
pass
# Called when both the node and its children have entered the scene tree.
func _ready():
pass
# Called when the node is about to leave the scene tree, after all its
children.
func _exit_tree():
pass
.. code-tab:: csharp
// Called every time the node enters the scene tree.
public override void _EnterTree()
{
base._EnterTree();
}
// Called when both the node and its children have entered the scene tree.
public override void _Ready()
{
base._Ready();
}
// Called when the node is about to leave the scene tree, after all its
children.
public override void _ExitTree()
{
base._ExitTree();
}
The two virtual methods ``_process()`` and ``_physics_process()`` allow you to
update the node, respectively, every frame and every physics frame. For more
information, read the dedicated documentation:
:ref:`doc_idle_and_physics_processing`.
.. tabs::
.. code-tab:: gdscript GDScript
# Called every frame, as often as possible.
func _process(delta):
pass
# Called every physics frame.
func _physics_process(delta):
pass
.. code-tab:: csharp
public override void _Process(float delta)
{
// Called every frame, as often as possible.
base._Process(delta);
}
public override void _PhysicsProcess(float delta)
{
// Called every physics frame.
base._PhysicsProcess(delta);
}
Two more essential built-in node callback functions are
:ref:`Node._unhandled_input() <class_Node_method__unhandled_input>` and
:ref:`Node._input() <class_Node_method__input>`, which you both use to receive
and process individual input events. The ``_unhandled_input()`` method receives
every key press, mouse click, etc. that had not been handled already in an
``_input()`` callback or in a piece of user interface. You want to use it for
gameplay input in general. The ``_input()`` callback allows you to intercept and
process input events before ``_unhandled_input()`` gets them.
There's a lot to say about input handling in Godot. You'll find a lot more
information in the :ref:`Input section <toc-learn-features-inputs>`.
.. tabs::
.. code-tab:: gdscript GDScript
# Called once for every event.
func _unhandled_input(event):
pass
# Called once for every event, before _unhandled_input(), allowing you to
consume some events.
func _input(event):
pass
.. code-tab:: csharp
// Called once for every event.
public override void _UnhandledInput(InputEvent @event)
{
base._UnhandledInput(event);
}
// Called once for every event, before _unhandled_input(), allowing you to
consume some events.
public override void _Input(InputEvent @event)
{
base._Input(event);
}
There are some more overridable functions like
:ref:`Node._get_configuration_warning()
<class_Node_method__get_configuration_warning>`. Specialized node types provide
more callbacks like :ref:`CanvasItem._draw() <class_CanvasItem_method__draw>` to
draw programmatically or :ref:`Control._gui_input()
<class_Control_method__gui_input>` to handle clicks and input on UI elements.