Skip to content

Commit 8625ccb

Browse files
authored
Fix duplicate mob squash logic in Your first 3D game (godotengine#7767)
1 parent 1145e1a commit 8625ccb

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

getting_started/first_3d_game/06.jump_and_squash.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
255255
# If so, we squash it and bounce.
256256
mob.squash()
257257
target_velocity.y = bounce_impulse
258+
# Prevent further duplicate calls.
259+
break
258260

259261
.. code-tab:: csharp
260262

@@ -279,6 +281,8 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
279281
// If so, we squash it and bounce.
280282
mob.Squash();
281283
_targetVelocity.Y = BounceImpulse;
284+
// Prevent further duplicate calls.
285+
break;
282286
}
283287
}
284288
}
@@ -309,6 +313,10 @@ With dot products, when the result is greater than ``0``, the two vectors are at
309313
an angle of fewer than 90 degrees. A value higher than ``0.1`` tells us that we
310314
are roughly above the monster.
311315

316+
After handling the squash and bounce logic, we terminate the loop early via the ``break`` statement
317+
to prevent further duplicate calls to ``mob.squash()``, which may otherwise result in unintended bugs
318+
such as counting the score multiple times for one kill.
319+
312320
We are calling one undefined function, ``mob.squash()``, so we have to add it to
313321
the Mob class.
314322

getting_started/first_3d_game/07.killing_player.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ Finally, the longest script, ``Player.gd``:
395395
# If so, we squash it and bounce.
396396
mob.squash()
397397
target_velocity.y = bounce_impulse
398+
# Prevent further duplicate calls.
399+
break
398400

399401
# Moving the Character
400402
velocity = target_velocity
@@ -496,6 +498,8 @@ Finally, the longest script, ``Player.gd``:
496498
// If so, we squash it and bounce.
497499
mob.Squash();
498500
_targetVelocity.Y = BounceImpulse;
501+
// Prevent further duplicate calls.
502+
break;
499503
}
500504
}
501505
}

getting_started/first_3d_game/09.adding_animations.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ Here's the *Player* script.
369369
# If so, we squash it and bounce.
370370
mob.squash()
371371
target_velocity.y = bounce_impulse
372+
# Prevent further duplicate calls.
373+
break
372374

373375
# Moving the Character
374376
velocity = target_velocity
@@ -477,6 +479,8 @@ Here's the *Player* script.
477479
// If so, we squash it and bounce.
478480
mob.Squash();
479481
_targetVelocity.Y = BounceImpulse;
482+
// Prevent further duplicate calls.
483+
break;
480484
}
481485
}
482486
}

0 commit comments

Comments
 (0)