|
@@ -10,7 +10,7 @@ To do so, we need to add some functionality that we can't get from a built-in
|
|
|
node, so we'll add a script. Click the ``Player`` node and click the "Attach
|
|
|
Script" button:
|
|
|
|
|
|
-.. image:: img/add_script_button.png
|
|
|
+.. image:: img/add_script_button.webp
|
|
|
|
|
|
In the script settings window, you can leave the default settings alone. Just
|
|
|
click "Create":
|
|
@@ -18,7 +18,7 @@ click "Create":
|
|
|
.. note:: If you're creating a C# script or other languages, select the language
|
|
|
from the `language` drop down menu before hitting create.
|
|
|
|
|
|
-.. image:: img/attach_node_window.png
|
|
|
+.. image:: img/attach_node_window.webp
|
|
|
|
|
|
.. note:: If this is your first time encountering GDScript, please read
|
|
|
:ref:`doc_scripting` before continuing.
|
|
@@ -75,7 +75,7 @@ Start by declaring the member variables this object will need:
|
|
|
void _ready();
|
|
|
void _process(const double p_delta);
|
|
|
void start(const godot::Vector2 p_position);
|
|
|
- void _on_Player_body_entered(godot::Node2D *_body);
|
|
|
+ void _on_body_entered(godot::Node2D *_body);
|
|
|
|
|
|
static void _register_methods();
|
|
|
};
|
|
@@ -95,7 +95,7 @@ value written in the script.
|
|
|
bottom of the editor window to reveal the Mono Panel, then clicking
|
|
|
the "Build Project" button.
|
|
|
|
|
|
-.. image:: img/export_variable.png
|
|
|
+.. image:: img/export_variable.webp
|
|
|
|
|
|
The ``_ready()`` function is called when a node enters the scene tree, which is
|
|
|
a good time to find the size of the game window:
|
|
@@ -144,13 +144,19 @@ Click on *Project -> Project Settings* to open the project settings window and
|
|
|
click on the *Input Map* tab at the top. Type "move_right" in the top bar and
|
|
|
click the "Add" button to add the ``move_right`` action.
|
|
|
|
|
|
-.. image:: img/input-mapping-add-action.png
|
|
|
+.. image:: img/input-mapping-add-action.webp
|
|
|
|
|
|
-We need to assign a key to this action. Click the "+" icon on the right, then
|
|
|
-click the "Key" option in the drop-down menu. A dialog asks you to type in the
|
|
|
-desired key. Press the right arrow on your keyboard and click "Ok".
|
|
|
+We need to assign a key to this action. Click the "+" icon on the right, to
|
|
|
+open the event manager window.
|
|
|
|
|
|
-.. image:: img/input-mapping-add-key.png
|
|
|
+.. image:: img/input-mapping-add-key.webp
|
|
|
+
|
|
|
+The "Listening for Input..." field should automatically be selected.
|
|
|
+Press the "right" key on your keyboard, and the menu should look like this now.
|
|
|
+
|
|
|
+.. image:: img/input-mapping-event-configuration.webp
|
|
|
+
|
|
|
+Select the "ok" button. The "right" key is now associated with the ``move_right`` action.
|
|
|
|
|
|
Repeat these steps to add three more mappings:
|
|
|
|
|
@@ -160,7 +166,7 @@ Repeat these steps to add three more mappings:
|
|
|
|
|
|
Your input map tab should look like this:
|
|
|
|
|
|
-.. image:: img/input-mapping-completed.png
|
|
|
+.. image:: img/input-mapping-completed.webp
|
|
|
|
|
|
Click the "Close" button to close the project settings.
|
|
|
|
|
@@ -425,7 +431,7 @@ enemies yet! That's OK, because we're going to use Godot's *signal*
|
|
|
functionality to make it work.
|
|
|
|
|
|
Add the following at the top of the script. If you're using GDScript, add it after
|
|
|
-``extends Area2D``. If you're using C#, add it after ``public class Player : Area2D {``:
|
|
|
+``extends Area2D``. If you're using C#, add it after ``public partial class Player : Area2D``:
|
|
|
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
@@ -448,7 +454,7 @@ Add the following at the top of the script. If you're using GDScript, add it aft
|
|
|
godot::register_method("_ready", &Player::_ready);
|
|
|
godot::register_method("_process", &Player::_process);
|
|
|
godot::register_method("start", &Player::start);
|
|
|
- godot::register_method("_on_Player_body_entered", &Player::_on_Player_body_entered);
|
|
|
+ godot::register_method("_on_body_entered", &Player::_on_body_entered);
|
|
|
godot::register_property("speed", &Player::speed, (real_t)400.0);
|
|
|
// This below line is the signal.
|
|
|
godot::register_signal<Player>("hit", godot::Dictionary());
|
|
@@ -459,7 +465,7 @@ This defines a custom signal called "hit" that we will have our player emit
|
|
|
collision. Select the ``Player`` node and click the "Node" tab next to the
|
|
|
Inspector tab to see the list of signals the player can emit:
|
|
|
|
|
|
-.. image:: img/player_signals.png
|
|
|
+.. image:: img/player_signals.webp
|
|
|
|
|
|
Notice our custom "hit" signal is there as well! Since our enemies are going to
|
|
|
be ``RigidBody2D`` nodes, we want the ``body_entered(body: Node)`` signal. This
|
|
@@ -468,7 +474,7 @@ the "Connect a Signal" window appears. We don't need to change any of these
|
|
|
settings so click "Connect" again. Godot will automatically create a function in
|
|
|
your player's script.
|
|
|
|
|
|
-.. image:: img/player_signal_connection.png
|
|
|
+.. image:: img/player_signal_connection.webp
|
|
|
|
|
|
Note the green icon indicating that a signal is connected to this function. Add
|
|
|
this code to the function:
|
|
@@ -476,7 +482,7 @@ this code to the function:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- func _on_Player_body_entered(body):
|
|
|
+ func _on_body_entered(body):
|
|
|
hide() # Player disappears after being hit.
|
|
|
hit.emit()
|
|
|
# Must be deferred as we can't change physics properties on a physics callback.
|
|
@@ -484,7 +490,7 @@ this code to the function:
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public void OnPlayerBodyEntered(PhysicsBody2D body)
|
|
|
+ public void OnBodyEntered(PhysicsBody2D body)
|
|
|
{
|
|
|
Hide(); // Player disappears after being hit.
|
|
|
EmitSignal(SignalName.Hit);
|
|
@@ -495,7 +501,7 @@ this code to the function:
|
|
|
.. code-tab:: cpp
|
|
|
|
|
|
// This code goes in `player.cpp`.
|
|
|
- void Player::_on_Player_body_entered(godot::Node2D *_body) {
|
|
|
+ void Player::_on_body_entered(godot::Node2D *_body) {
|
|
|
hide(); // Player disappears after being hit.
|
|
|
emit_signal("hit");
|
|
|
// Must be deferred as we can't change physics properties on a physics callback.
|