Mob bug while following the "Dodge the Creeps" godot doc tutorial

Godot Version

4.2.2.

Question

Why does “mob_spawn_location.progress = randf()” set the mob_spawn_location’s position to randf()? I thought position and progress of a PathFollow2D node are different things

I’ve determined this is the case with debug prints in the code

Code snippet of the function that’s problematic

func _on_mob_timer_timeout():
	# Creating the mob
	var mob = mob_scene.instantiate()
	# Setting it up
	var mob_spawn_location = $MobPath/MobSpawnLocation
	mob_spawn_location.progress = randf()
	mob.position = mob_spawn_location.position

	print(mob_spawn_location.position)
	print(mob.position)
	print("..........")
	
	var direction = mob_spawn_location.rotation + PI / 2
	direction += randf_range(- PI / 4, PI / 4)
	mob.rotation = direction
	
	var velocity = Vector2(randf_range(150, 250), 0)
	mob.linear_velocity = velocity.rotated(direction)
	# Instantiating it on the scene
	add_child(mob)

Entire code of main scene script

extends Node

# Fields
@export var mob_scene: PackedScene
var score = 0

# Called when the node enters the scene tree for the first time
func _ready():

	new_game()
	
# Called every frame
func _process(delta):
	pass

# Connected with the Players hit() signal
func game_over():
	$ScoreTimer.stop()
	$MobTimer.stop()

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

# ----- Timers -----
func _on_mob_timer_timeout():
	# Creating the mob
	var mob = mob_scene.instantiate()
	# Setting it up
	var mob_spawn_location = $MobPath/MobSpawnLocation
	mob_spawn_location.progress = randf()
	mob.position = mob_spawn_location.position
	print(mob_spawn_location.position)
	print(mob.position)
	print("..........")
	
	var direction = mob_spawn_location.rotation + PI / 2
	direction += randf_range(- PI / 4, PI / 4)
	mob.rotation = direction
	
	var velocity = Vector2(randf_range(150, 250), 0)
	mob.linear_velocity = velocity.rotated(direction)
	# Instantiating it on the scene
	add_child(mob)
	
func _on_score_timer_timeout():
	score += 1

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

Debug result

image

A PathFollow2D’s progress is how many pixels along the path it has moved. Setting this, or progress_ratio will change it’s position to match the Path’s curve, try sliding this value in the inspector to watch in real time.

float progress = 0.0

The distance along the path, in pixels. Changing this value sets this node’s position to a point within the path.

I think you will want to use progress_ratio as that works better with numbers 0 to 1 and will give you the full range on your path.

mob_spawn_location.progress_ratio = randf()
mob.position = mob_spawn_location.position
1 Like

You haven’t copied the code of the demo, it uses progress_ratio

See:

	# Choose a random location on Path2D.
	var mob_spawn_location = $MobPath/MobSpawnLocation
	mob_spawn_location.progress_ratio = randf()