Skip to content

Commit a7637e0

Browse files
raulsntosRedworkDE
andauthored
Update getting started C# code, remove randomize (#6811)
* Update getting started C# code, remove randomize - Sync C# code with GDScript. - Remove `randomize` calls. - Rename members that have been renamed in Godot's C# API for 4.0. - Change `delta` parameter type to `double` in C#. - Follow our C# code style more closely. - Other minor code fixes. --------- Co-authored-by: RedworkDE <[email protected]>
1 parent adc15aa commit a7637e0

14 files changed

+374
-314
lines changed

getting_started/first_2d_game/01.project_setup.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Launch Godot and create a new project.
2525
and ``fonts/`` directories to your project's directory.
2626

2727
Ensure that you have the required dependencies to use C# in Godot.
28-
You need the .NET Core 3.1 SDK, and an editor such as VS Code.
28+
You need the latest stable .NET SDK, and an editor such as VS Code.
2929
See :ref:`doc_c_sharp_setup`.
3030

3131
.. tab:: C++

getting_started/first_2d_game/03.coding_the_player.rst

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,18 @@ node and you'll see the property now appears in the "Script Variables" section
8989
of the Inspector. Remember, if you change the value here, it will override the
9090
value written in the script.
9191

92-
.. warning:: If you're using C#, you need to (re)build the project assemblies
93-
whenever you want to see new export variables or signals. This
94-
build can be manually triggered by clicking the word "Mono" at the
95-
bottom of the editor window to reveal the Mono Panel, then clicking
96-
the "Build Project" button.
92+
.. warning::
93+
94+
If you're using C#, you need to (re)build the project assemblies
95+
whenever you want to see new export variables or signals. This
96+
build can be manually triggered by clicking the "Build" button at
97+
the top right of the editor.
98+
99+
.. image:: img/build_dotnet.webp
100+
101+
A manual build can also be triggered from the MSBuild Panel. Click
102+
the word "MSBuild" at the bottom of the editor window to reveal the
103+
MSBuild Panel, then click the "Build" button.
97104

98105
.. image:: img/export_variable.webp
99106

@@ -390,7 +397,7 @@ movement. Let's place this code at the end of the ``_process()`` function:
390397
391398
.. code-tab:: csharp
392399

393-
if (velocity.x < 0)
400+
if (velocity.X < 0)
394401
{
395402
animatedSprite2D.FlipH = true;
396403
}
@@ -490,12 +497,12 @@ this code to the function:
490497

491498
.. code-tab:: csharp
492499

493-
public void OnBodyEntered(PhysicsBody2D body)
500+
private void OnBodyEntered(PhysicsBody2D body)
494501
{
495502
Hide(); // Player disappears after being hit.
496503
EmitSignal(SignalName.Hit);
497504
// Must be deferred as we can't change physics properties on a physics callback.
498-
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
505+
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
499506
}
500507

501508
.. code-tab:: cpp
@@ -530,9 +537,9 @@ starting a new game.
530537

531538
.. code-tab:: csharp
532539

533-
public void Start(Vector2 pos)
540+
public void Start(Vector2 position)
534541
{
535-
Position = pos;
542+
Position = position;
536543
Show();
537544
GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
538545
}

getting_started/first_2d_game/04.creating_the_enemy.rst

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,16 @@ and randomly choose one of the three animation types:
108108
.. code-tab:: gdscript GDScript
109109

110110
func _ready():
111-
$AnimatedSprite2D.play()
112-
var mob_types = $AnimatedSprite2D.get_sprite_frames().get_animation_names()
113-
$AnimatedSprite2D.animation = mob_types[randi() % mob_types.size()]
111+
var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
112+
$AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])
114113

115114
.. code-tab:: csharp
116115

117116
public override void _Ready()
118117
{
119-
var animSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
120-
animSprite2D.Play();
121-
string[] mobTypes = animSprite2D.SpriteFrames.GetAnimationNames();
122-
animSprite2D.Animation = mobTypes[GD.Randi() % mobTypes.Length];
118+
var animatedSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
119+
string[] mobTypes = animatedSprite2D.SpriteFrames.GetAnimationNames();
120+
animatedSprite2D.Play(mobTypes[GD.Randi() % mobTypes.Length]);
123121
}
124122

125123
.. code-tab:: cpp
@@ -132,7 +130,6 @@ and randomly choose one of the three animation types:
132130

133131
void Mob::_ready() {
134132
godot::Ref<godot::RandomNumberGenerator> random = godot::RandomNumberGenerator::_new();
135-
random->randomize();
136133
_animated_sprite = get_node<godot::AnimatedSprite2D>("AnimatedSprite2D");
137134
_animated_sprite->set_playing(true);
138135
godot::PoolStringArray mob_types = _animated_sprite->get_sprite_frames()->get_animation_names();
@@ -147,10 +144,6 @@ We then need to pick a random number between ``0`` and ``2`` to select one of
147144
these names from the list (array indices start at ``0``). ``randi() % n``
148145
selects a random integer between ``0`` and ``n-1``.
149146

150-
.. note:: You must use ``randomize()`` if you want your sequence of "random"
151-
numbers to be different every time you run the scene. We're going to
152-
use ``randomize()`` in our ``Main`` scene, so we won't need it here.
153-
154147
The last piece is to make the mobs delete themselves when they leave the screen.
155148
Connect the ``screen_exited()`` signal of the ``VisibleOnScreenNotifier2D`` node and
156149
add this code:
@@ -163,7 +156,7 @@ add this code:
163156

164157
.. code-tab:: csharp
165158

166-
public void OnVisibleOnScreenNotifier2DScreenExited()
159+
private void OnVisibleOnScreenNotifier2DScreenExited()
167160
{
168161
QueueFree();
169162
}

getting_started/first_2d_game/05.the_main_game_scene.rst

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,10 @@ to instance.
9393
{
9494
// Don't forget to rebuild the project so the editor knows about the new export variable.
9595

96-
#pragma warning disable 649
97-
// We assign this in the editor, so we don't need the warning about not being assigned.
9896
[Export]
99-
public PackedScene MobScene;
100-
#pragma warning restore 649
97+
public PackedScene MobScene { get; set; }
10198

102-
public int Score;
99+
private int _score;
103100
}
104101

105102
.. code-tab:: cpp
@@ -219,7 +216,7 @@ new game:
219216

220217
public void NewGame()
221218
{
222-
Score = 0;
219+
_score = 0;
223220

224221
var player = GetNode<Player>("Player");
225222
var startPosition = GetNode<Marker2D>("StartPosition");
@@ -258,12 +255,12 @@ the other two timers. ``ScoreTimer`` will increment the score by 1.
258255

259256
.. code-tab:: csharp
260257

261-
public void OnScoreTimerTimeout()
258+
private void OnScoreTimerTimeout()
262259
{
263-
Score++;
260+
_score++;
264261
}
265262

266-
public void OnStartTimerTimeout()
263+
private void OnStartTimerTimeout()
267264
{
268265
GetNode<Timer>("MobTimer").Start();
269266
GetNode<Timer>("ScoreTimer").Start();
@@ -332,14 +329,14 @@ Note that a new instance must be added to the scene using ``add_child()``.
332329

333330
.. code-tab:: csharp
334331

335-
public void OnMobTimerTimeout()
332+
private void OnMobTimerTimeout()
336333
{
337334
// Note: Normally it is best to use explicit types rather than the `var`
338335
// keyword. However, var is acceptable to use here because the types are
339336
// obviously Mob and PathFollow2D, since they appear later on the line.
340337

341338
// Create a new instance of the Mob scene.
342-
var mob = MobScene.Instantiate<Mob>();
339+
Mob mob = MobScene.Instantiate<Mob>();
343340

344341
// Choose a random location on Path2D.
345342
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");

getting_started/first_2d_game/06.heads_up_display.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ We also need to process what happens when the player loses. The code below will
233233
ShowMessage("Game Over");
234234

235235
var messageTimer = GetNode<Timer>("MessageTimer");
236-
await ToSignal(messageTimer, "timeout");
236+
await ToSignal(messageTimer, Timer.SignalName.Timeout);
237237

238238
var message = GetNode<Label>("Message");
239239
message.Text = "Dodge the\nCreeps!";
240240
message.Show();
241241

242-
await ToSignal(GetTree().CreateTimer(1), "timeout");
242+
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
243243
GetNode<Button>("StartButton").Show();
244244
}
245245

@@ -306,13 +306,13 @@ signal of ``StartButton``, and add the following code to the new functions:
306306

307307
.. code-tab:: csharp
308308

309-
public void OnStartButtonPressed()
309+
private void OnStartButtonPressed()
310310
{
311311
GetNode<Button>("StartButton").Hide();
312312
EmitSignal(SignalName.StartGame);
313313
}
314314

315-
public void OnMessageTimerTimeout()
315+
private void OnMessageTimerTimeout()
316316
{
317317
GetNode<Label>("Message").Hide();
318318
}
@@ -364,7 +364,7 @@ In ``new_game()``, update the score display and show the "Get Ready" message:
364364
.. code-tab:: csharp
365365

366366
var hud = GetNode<HUD>("HUD");
367-
hud.UpdateScore(Score);
367+
hud.UpdateScore(_score);
368368
hud.ShowMessage("Get Ready!");
369369

370370
.. code-tab:: cpp
@@ -400,7 +400,7 @@ with the changing score:
400400

401401
.. code-tab:: csharp
402402

403-
GetNode<HUD>("HUD").UpdateScore(Score);
403+
GetNode<HUD>("HUD").UpdateScore(_score);
404404

405405
.. code-tab:: cpp
406406

@@ -435,7 +435,7 @@ the ``new_game()`` function in ``Main``:
435435

436436
// Note that for calling Godot-provided methods with strings,
437437
// we have to use the original Godot snake_case name.
438-
GetTree().CallGroup("mobs", "queue_free");
438+
GetTree().CallGroup("mobs", Node.MethodName.QueueFree);
439439

440440
.. code-tab:: cpp
441441

3.7 KB
Loading

0 commit comments

Comments
 (0)