mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-03 05:48:42 +03:00
Update & fix C# blocks under getting_started/
- `_Process` take its delta argument as a double - Node classes need to be partial - Use `SignalName.*` instead of `nameof(*)` for signals - Add a note about how `delta` arguments are doubles
This commit is contained in:
@@ -487,7 +487,7 @@ this code to the function:
|
||||
public void OnPlayerBodyEntered(PhysicsBody2D body)
|
||||
{
|
||||
Hide(); // Player disappears after being hit.
|
||||
EmitSignal(nameof(Hit));
|
||||
EmitSignal(SignalName.Hit);
|
||||
// Must be deferred as we can't change physics properties on a physics callback.
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ Now add this script to ``HUD``:
|
||||
public partial class HUD : CanvasLayer
|
||||
{
|
||||
// Don't forget to rebuild the project so the editor knows about the new signal.
|
||||
|
||||
|
||||
[Signal]
|
||||
public delegate void StartGameEventHandler();
|
||||
}
|
||||
@@ -158,7 +158,7 @@ Now add this script to ``HUD``:
|
||||
|
||||
#endif // HUD_H
|
||||
|
||||
We now want to display a message temporarily,
|
||||
We now want to display a message temporarily,
|
||||
such as "Get Ready", so we add the following code
|
||||
|
||||
.. tabs::
|
||||
@@ -304,7 +304,7 @@ signal of ``StartButton``, and add the following code to the new functions:
|
||||
public void OnStartButtonPressed()
|
||||
{
|
||||
GetNode<Button>("StartButton").Hide();
|
||||
EmitSignal(nameof(StartGame));
|
||||
EmitSignal(SignalName.StartGame);
|
||||
}
|
||||
|
||||
public void OnMessageTimerTimeout()
|
||||
|
||||
@@ -97,23 +97,12 @@ the following line of code:
|
||||
|
||||
.. code-tab:: csharp C#
|
||||
|
||||
public class Sprite : Godot.Sprite2D
|
||||
// Declare member variables here. Examples:
|
||||
// private int a = 2;
|
||||
// private string b = "text";
|
||||
using Godot;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
public partial class Sprite : Sprite2D
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// // Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
// public override void _Process(float delta)
|
||||
// {
|
||||
//
|
||||
// }
|
||||
|
||||
Every GDScript file is implicitly a class. The ``extends`` keyword defines the
|
||||
class this script inherits or extends. In this case, it's ``Sprite2D``, meaning
|
||||
our script will get access to all the properties and functions of the Sprite2D
|
||||
@@ -233,9 +222,9 @@ At the bottom of the script, define the function:
|
||||
|
||||
.. code-tab:: csharp C#
|
||||
|
||||
public override void _Process(float delta)
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
Rotation += AngularSpeed * delta;
|
||||
Rotation += AngularSpeed * (float)delta;
|
||||
}
|
||||
|
||||
The ``func`` keyword defines a new function. After it, we have to write the
|
||||
@@ -261,6 +250,10 @@ Run the scene to see the Godot icon turn in-place.
|
||||
|
||||
.. image:: img/scripting_first_script_godot_turning_in_place.gif
|
||||
|
||||
.. note:: In C#, notice how the ``delta`` argument taken by ``_Process()`` is a
|
||||
``double``. We therefore need to convert it to ``float`` when we apply
|
||||
it to the rotation.
|
||||
|
||||
Moving forward
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
@@ -279,7 +272,7 @@ them.
|
||||
|
||||
var velocity = Vector2.Up.Rotated(Rotation) * Speed;
|
||||
|
||||
Position += velocity * delta;
|
||||
Position += velocity * (float)delta;
|
||||
|
||||
As we already saw, the ``var`` keyword defines a new variable. If you put it at
|
||||
the top of the script, it defines a property of the class. Inside a function, it
|
||||
@@ -332,17 +325,16 @@ Here is the complete ``Sprite2D.gd`` file for reference.
|
||||
|
||||
using Godot;
|
||||
|
||||
public class Sprite : Godot.Sprite2D
|
||||
public partial class Sprite : Sprite2D
|
||||
{
|
||||
private int Speed = 400;
|
||||
private float AngularSpeed = Mathf.Pi;
|
||||
|
||||
public override void _Process(float delta)
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
Rotation += AngularSpeed * delta;
|
||||
Rotation += AngularSpeed * (float)delta;
|
||||
var velocity = Vector2.Up.Rotated(Rotation) * Speed;
|
||||
|
||||
Position += velocity * delta;
|
||||
|
||||
Position += velocity * (float)delta;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ code below.
|
||||
direction = 1;
|
||||
}
|
||||
|
||||
Rotation += AngularSpeed * direction * delta;
|
||||
Rotation += AngularSpeed * direction * (float)delta;
|
||||
|
||||
Our ``direction`` local variable is a multiplier representing the direction in
|
||||
which the player wants to turn. A value of ``0`` means the player isn't pressing
|
||||
@@ -141,13 +141,13 @@ Here is the complete ``Sprite2D.gd`` file for reference.
|
||||
.. code-tab:: csharp C#
|
||||
|
||||
using Godot;
|
||||
|
||||
public class Sprite : Godot.Sprite2D
|
||||
|
||||
public partial class Sprite : Sprite2D
|
||||
{
|
||||
private float Speed = 400;
|
||||
private float AngularSpeed = Mathf.Pi;
|
||||
|
||||
public override void _Process(float delta)
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
var direction = 0;
|
||||
if (Input.IsActionPressed("ui_left"))
|
||||
@@ -159,7 +159,7 @@ Here is the complete ``Sprite2D.gd`` file for reference.
|
||||
direction = 1;
|
||||
}
|
||||
|
||||
Rotation += AngularSpeed * direction * delta;
|
||||
Rotation += AngularSpeed * direction * (float)delta;
|
||||
|
||||
var velocity = Vector2.Zero;
|
||||
if (Input.IsActionPressed("ui_up"))
|
||||
@@ -167,7 +167,7 @@ Here is the complete ``Sprite2D.gd`` file for reference.
|
||||
velocity = Vector2.Up.Rotated(Rotation) * Speed;
|
||||
}
|
||||
|
||||
Position += velocity * delta;
|
||||
Position += velocity * (float)delta;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user