From a7637e0ef1430d8be5cfd4bf6715223060f32f12 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Thu, 9 Mar 2023 06:06:54 +0100 Subject: [PATCH] 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 <10944644+RedworkDE@users.noreply.github.com> --- .../first_2d_game/01.project_setup.rst | 2 +- .../first_2d_game/03.coding_the_player.rst | 27 ++-- .../first_2d_game/04.creating_the_enemy.rst | 19 +-- .../first_2d_game/05.the_main_game_scene.rst | 19 +-- .../first_2d_game/06.heads_up_display.rst | 14 +- .../first_2d_game/img/build_dotnet.webp | Bin 0 -> 3788 bytes .../first_3d_game/03.player_movement_code.rst | 84 ++++++----- .../first_3d_game/04.mob_scene.rst | 69 +++++---- .../first_3d_game/05.spawning_mobs.rst | 61 +++----- .../first_3d_game/06.jump_and_squash.rst | 31 ++-- .../first_3d_game/07.killing_player.rst | 137 ++++++++++-------- .../first_3d_game/08.score_and_replay.rst | 44 +++--- .../first_3d_game/09.adding_animations.rst | 129 ++++++++++------- getting_started/step_by_step/signals.rst | 52 +++---- 14 files changed, 374 insertions(+), 314 deletions(-) create mode 100644 getting_started/first_2d_game/img/build_dotnet.webp diff --git a/getting_started/first_2d_game/01.project_setup.rst b/getting_started/first_2d_game/01.project_setup.rst index 9c2f3ae82..78c3e42f8 100644 --- a/getting_started/first_2d_game/01.project_setup.rst +++ b/getting_started/first_2d_game/01.project_setup.rst @@ -25,7 +25,7 @@ Launch Godot and create a new project. and ``fonts/`` directories to your project's directory. Ensure that you have the required dependencies to use C# in Godot. - You need the .NET Core 3.1 SDK, and an editor such as VS Code. + You need the latest stable .NET SDK, and an editor such as VS Code. See :ref:`doc_c_sharp_setup`. .. tab:: C++ diff --git a/getting_started/first_2d_game/03.coding_the_player.rst b/getting_started/first_2d_game/03.coding_the_player.rst index aaa40d951..8c003d101 100644 --- a/getting_started/first_2d_game/03.coding_the_player.rst +++ b/getting_started/first_2d_game/03.coding_the_player.rst @@ -89,11 +89,18 @@ node and you'll see the property now appears in the "Script Variables" section of the Inspector. Remember, if you change the value here, it will override the value written in the script. -.. warning:: If you're using C#, you need to (re)build the project assemblies - whenever you want to see new export variables or signals. This - build can be manually triggered by clicking the word "Mono" at the - bottom of the editor window to reveal the Mono Panel, then clicking - the "Build Project" button. +.. warning:: + + If you're using C#, you need to (re)build the project assemblies + whenever you want to see new export variables or signals. This + build can be manually triggered by clicking the "Build" button at + the top right of the editor. + + .. image:: img/build_dotnet.webp + + A manual build can also be triggered from the MSBuild Panel. Click + the word "MSBuild" at the bottom of the editor window to reveal the + MSBuild Panel, then click the "Build" button. .. image:: img/export_variable.webp @@ -390,7 +397,7 @@ movement. Let's place this code at the end of the ``_process()`` function: .. code-tab:: csharp - if (velocity.x < 0) + if (velocity.X < 0) { animatedSprite2D.FlipH = true; } @@ -490,12 +497,12 @@ this code to the function: .. code-tab:: csharp - public void OnBodyEntered(PhysicsBody2D body) + private void OnBodyEntered(PhysicsBody2D body) { Hide(); // Player disappears after being hit. EmitSignal(SignalName.Hit); // Must be deferred as we can't change physics properties on a physics callback. - GetNode("CollisionShape2D").SetDeferred("disabled", true); + GetNode("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true); } .. code-tab:: cpp @@ -530,9 +537,9 @@ starting a new game. .. code-tab:: csharp - public void Start(Vector2 pos) + public void Start(Vector2 position) { - Position = pos; + Position = position; Show(); GetNode("CollisionShape2D").Disabled = false; } diff --git a/getting_started/first_2d_game/04.creating_the_enemy.rst b/getting_started/first_2d_game/04.creating_the_enemy.rst index 57c88d7bd..f5c8d5c14 100644 --- a/getting_started/first_2d_game/04.creating_the_enemy.rst +++ b/getting_started/first_2d_game/04.creating_the_enemy.rst @@ -108,18 +108,16 @@ and randomly choose one of the three animation types: .. code-tab:: gdscript GDScript func _ready(): - $AnimatedSprite2D.play() - var mob_types = $AnimatedSprite2D.get_sprite_frames().get_animation_names() - $AnimatedSprite2D.animation = mob_types[randi() % mob_types.size()] + var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names() + $AnimatedSprite2D.play(mob_types[randi() % mob_types.size()]) .. code-tab:: csharp public override void _Ready() { - var animSprite2D = GetNode("AnimatedSprite2D"); - animSprite2D.Play(); - string[] mobTypes = animSprite2D.SpriteFrames.GetAnimationNames(); - animSprite2D.Animation = mobTypes[GD.Randi() % mobTypes.Length]; + var animatedSprite2D = GetNode("AnimatedSprite2D"); + string[] mobTypes = animatedSprite2D.SpriteFrames.GetAnimationNames(); + animatedSprite2D.Play(mobTypes[GD.Randi() % mobTypes.Length]); } .. code-tab:: cpp @@ -132,7 +130,6 @@ and randomly choose one of the three animation types: void Mob::_ready() { godot::Ref random = godot::RandomNumberGenerator::_new(); - random->randomize(); _animated_sprite = get_node("AnimatedSprite2D"); _animated_sprite->set_playing(true); 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 these names from the list (array indices start at ``0``). ``randi() % n`` selects a random integer between ``0`` and ``n-1``. -.. note:: You must use ``randomize()`` if you want your sequence of "random" - numbers to be different every time you run the scene. We're going to - use ``randomize()`` in our ``Main`` scene, so we won't need it here. - The last piece is to make the mobs delete themselves when they leave the screen. Connect the ``screen_exited()`` signal of the ``VisibleOnScreenNotifier2D`` node and add this code: @@ -163,7 +156,7 @@ add this code: .. code-tab:: csharp - public void OnVisibleOnScreenNotifier2DScreenExited() + private void OnVisibleOnScreenNotifier2DScreenExited() { QueueFree(); } diff --git a/getting_started/first_2d_game/05.the_main_game_scene.rst b/getting_started/first_2d_game/05.the_main_game_scene.rst index d999ed95b..be2e85f14 100644 --- a/getting_started/first_2d_game/05.the_main_game_scene.rst +++ b/getting_started/first_2d_game/05.the_main_game_scene.rst @@ -93,13 +93,10 @@ to instance. { // Don't forget to rebuild the project so the editor knows about the new export variable. - #pragma warning disable 649 - // We assign this in the editor, so we don't need the warning about not being assigned. [Export] - public PackedScene MobScene; - #pragma warning restore 649 + public PackedScene MobScene { get; set; } - public int Score; + private int _score; } .. code-tab:: cpp @@ -219,7 +216,7 @@ new game: public void NewGame() { - Score = 0; + _score = 0; var player = GetNode("Player"); var startPosition = GetNode("StartPosition"); @@ -258,12 +255,12 @@ the other two timers. ``ScoreTimer`` will increment the score by 1. .. code-tab:: csharp - public void OnScoreTimerTimeout() + private void OnScoreTimerTimeout() { - Score++; + _score++; } - public void OnStartTimerTimeout() + private void OnStartTimerTimeout() { GetNode("MobTimer").Start(); GetNode("ScoreTimer").Start(); @@ -332,14 +329,14 @@ Note that a new instance must be added to the scene using ``add_child()``. .. code-tab:: csharp - public void OnMobTimerTimeout() + private void OnMobTimerTimeout() { // Note: Normally it is best to use explicit types rather than the `var` // keyword. However, var is acceptable to use here because the types are // obviously Mob and PathFollow2D, since they appear later on the line. // Create a new instance of the Mob scene. - var mob = MobScene.Instantiate(); + Mob mob = MobScene.Instantiate(); // Choose a random location on Path2D. var mobSpawnLocation = GetNode("MobPath/MobSpawnLocation"); diff --git a/getting_started/first_2d_game/06.heads_up_display.rst b/getting_started/first_2d_game/06.heads_up_display.rst index 75250eb8f..0266cd4e0 100644 --- a/getting_started/first_2d_game/06.heads_up_display.rst +++ b/getting_started/first_2d_game/06.heads_up_display.rst @@ -233,13 +233,13 @@ We also need to process what happens when the player loses. The code below will ShowMessage("Game Over"); var messageTimer = GetNode("MessageTimer"); - await ToSignal(messageTimer, "timeout"); + await ToSignal(messageTimer, Timer.SignalName.Timeout); var message = GetNode