Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getting started C# code, remove randomize #6811

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion getting_started/first_2d_game/01.project_setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
27 changes: 17 additions & 10 deletions getting_started/first_2d_game/03.coding_the_player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>("CollisionShape2D").SetDeferred("disabled", true);
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
}

.. code-tab:: cpp
Expand Down Expand Up @@ -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>("CollisionShape2D").Disabled = false;
}
Expand Down
19 changes: 6 additions & 13 deletions getting_started/first_2d_game/04.creating_the_enemy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>("AnimatedSprite2D");
animSprite2D.Play();
string[] mobTypes = animSprite2D.SpriteFrames.GetAnimationNames();
animSprite2D.Animation = mobTypes[GD.Randi() % mobTypes.Length];
var animatedSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
string[] mobTypes = animatedSprite2D.SpriteFrames.GetAnimationNames();
animatedSprite2D.Play(mobTypes[GD.Randi() % mobTypes.Length]);
}

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

void Mob::_ready() {
godot::Ref<godot::RandomNumberGenerator> random = godot::RandomNumberGenerator::_new();
random->randomize();
_animated_sprite = get_node<godot::AnimatedSprite2D>("AnimatedSprite2D");
_animated_sprite->set_playing(true);
godot::PoolStringArray mob_types = _animated_sprite->get_sprite_frames()->get_animation_names();
Expand All @@ -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:
Expand All @@ -163,7 +156,7 @@ add this code:

.. code-tab:: csharp

public void OnVisibleOnScreenNotifier2DScreenExited()
private void OnVisibleOnScreenNotifier2DScreenExited()
{
QueueFree();
}
Expand Down
19 changes: 8 additions & 11 deletions getting_started/first_2d_game/05.the_main_game_scene.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -219,7 +216,7 @@ new game:

public void NewGame()
{
Score = 0;
_score = 0;

var player = GetNode<Player>("Player");
var startPosition = GetNode<Marker2D>("StartPosition");
Expand Down Expand Up @@ -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<Timer>("MobTimer").Start();
GetNode<Timer>("ScoreTimer").Start();
Expand Down Expand Up @@ -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 mob = MobScene.Instantiate<Mob>();

// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
Expand Down
14 changes: 7 additions & 7 deletions getting_started/first_2d_game/06.heads_up_display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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<Timer>("MessageTimer");
await ToSignal(messageTimer, "timeout");
await ToSignal(messageTimer, Timer.SignalName.Timeout);

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

await ToSignal(GetTree().CreateTimer(1), "timeout");
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
GetNode<Button>("StartButton").Show();
}

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

.. code-tab:: csharp

public void OnStartButtonPressed()
private void OnStartButtonPressed()
{
GetNode<Button>("StartButton").Hide();
EmitSignal(SignalName.StartGame);
}

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

var hud = GetNode<HUD>("HUD");
hud.UpdateScore(Score);
hud.UpdateScore(_score);
hud.ShowMessage("Get Ready!");

.. code-tab:: cpp
Expand Down Expand Up @@ -400,7 +400,7 @@ with the changing score:

.. code-tab:: csharp

GetNode<HUD>("HUD").UpdateScore(Score);
GetNode<HUD>("HUD").UpdateScore(_score);

.. code-tab:: cpp

Expand Down Expand Up @@ -435,7 +435,7 @@ the ``new_game()`` function in ``Main``:

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

.. code-tab:: cpp

Expand Down
Binary file not shown.
Loading