Skip to content

Commit d0f7ee6

Browse files
authored
Sync "Creating the Enemy" section of Getting Started/First Game 2D tutorial with demo project code (#10513)
1 parent d4a7065 commit d0f7ee6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

getting_started/first_2d_game/04.creating_the_enemy.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ and randomly choose one of the three animation types:
8585
.. code-tab:: gdscript GDScript
8686

8787
func _ready():
88-
var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
89-
$AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])
88+
var mob_types = Array($AnimatedSprite2D.sprite_frames.get_animation_names())
89+
$AnimatedSprite2D.animation = mob_types.pick_random()
9090

9191
.. code-tab:: csharp
9292

@@ -101,9 +101,10 @@ First, we get the list of animation names from the AnimatedSprite2D's ``sprite_f
101101
property. This returns an Array containing all three animation names: ``["walk",
102102
"swim", "fly"]``.
103103

104-
We then need to pick a random number between ``0`` and ``2`` to select one of
105-
these names from the list (array indices start at ``0``). ``randi() % n``
106-
selects a random integer between ``0`` and ``n-1``.
104+
In the GDScript code, we use the :ref:`Array.pick_random <class_Array_method_pick_random>` method
105+
to select one of these animation names at random. Meanwhile, in the C# code, we pick a random number
106+
between ``0`` and ``2`` to select one of these names from the list (array indices start at ``0``). The
107+
expression ``GD.Randi() % n`` selects a random integer between ``0`` and ``n-1``.
107108

108109
The last piece is to make the mobs delete themselves when they leave the screen.
109110
Connect the ``screen_exited()`` signal of the ``VisibleOnScreenNotifier2D`` node

0 commit comments

Comments
 (0)