"your first 2d game" Breakpoint on line 46 mob.linear_velocity = velocity.rotated(direction)

Godot Version

4.2

Question

just started learning godot and i am currently following the “your first 2d game” guide and I’ve completed the main game sequence but whenever i try to run it there is a breakpoint on line 46: mob.linear_velocity = velocity.rotated(direction) i thought it may have been a typo so i deleted it and copy-pasted the code from the website and the same thing happened. my mob node type is set to rigged body 2d so it should have “linear_velocity” right? plz help

breakpoints are the red dots in besides the line numbers, inside what we call the “gutter” along with bookmarks, connected signals (green squares), and overriden functions (blue arrows)

If you have a breakpoint in code the debugger will always halt there so you may examine variables and how the code flows. Try pressing F12 to continue execution when you hit a breakpoint or removing it by clicking the red dot again; but a red circle may appear which is as far as I can tell a visual glitch, ignore the red circle.

it says: invalid set index ‘linear_velocity’ (on base: ‘Area2D(player.gd)’) with value of type ‘Vector2’

I think this means you’ve deleted the mob.linear_velocity for just linear_velocity? I believe you had the line correct the first time, unless mob is defined as a player.

If that’s not the case please paste your script with three ticks like so

```
paste here
```


extends Node

@export var mob_scene: PackedScene
var score
func _ready():
pass
func _process(_delta):
pass
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()

func new_game():
score = 0
$player.start($StartPosition.position)
$StartTimer.start()
func _on_score_timer_timeout():
score += 1

func _on_start_timer_timeout():
$MobTimer.start()
$ScoreTimer.start()

func _on_mob_timer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instantiate()

# Choose a random location on Path2D.
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2
# Set the mob's position to a random location.
mob.position = mob_spawn_location.position
# Add some randomness to the direction.
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
# Choose the velocity for the mob.
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
# Spawn the mob by adding it to the Main scene.
add_child(mob)

As @gertkeno said, breakpoint is like a bookmark to tell the code execution to stop when reach the designed line, check your code in the line 46 and probably you’ll find a red circle like the one pointed in the image bellow, just left-click this circle and you’ll remove the breakpoint

1 Like

I would suspect this issue is from giving the “Main” Node a player scene instead of the mob scene in the inspector. The error message sounds like it’s spawning a player.tscn instead of the mob.tscn.

Double check this part of the tutorial

ps the ticks are by the squiggly ~ without shift makes `

checked the tutorial, pasted in the code, it looks the same, but now mobs show up! only problem is that now there is no character.


extends Node

@export var mob_scene: PackedScene
var score
func _ready():
pass
func _process(_delta):
pass
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()

func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
func _on_score_timer_timeout():
score += 1

func _on_start_timer_timeout():
$MobTimer.start()
$ScoreTimer.start()

func _on_mob_timer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instantiate()

# Choose a random location on Path2D.
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2
# Set the mob's position to a random location.
mob.position = mob_spawn_location.position
# Add some randomness to the direction.
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
# Choose the velocity for the mob.
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
# Spawn the mob by adding it to the Main scene.
add_child(mob)

im not sre what exactly was wrong but copy pasting the code seems to have solved it