Mobs aren't appearing and timers aren't working

Godot Version

4

Question

i tried to use the print function to debug but its not even printing… also the mobs don’t even appear and i don’t know why. here’s my code:

extends Node

@export var mob_scene: PackedScene
var score

func game_over():
$MobTimer.stop()

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

func _on_start_timer_timeout():
print (“timer good”)
$MobTimer.start()

func _on_mob_timer_timeout() → void:
print(“mob timer triggered”)
var mob = mob_scene.instantiate()
add_child(mob)
print (“mob instantiated and added”)

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

# Set the mob's position to a random location.
mob.position = mob_spawn_location.position

# Apply a velocity to the RigidBody2D mob
var velocity = Vector2(100, 0)  # Set an initial velocity
var direction = 45.0 * deg_to_rad(1)  # Angle in radians (45 degrees)
var rotated_velocity = velocity.rotated(direction)

# Set the mob's linear_velocity (this works for RigidBody2D)
mob.linear_velocity = rotated_velocity

if mob_scene == null:
	print("mob_scene is not assigned!")

Are none of the print statements printing? make sure to format your code pastes

1 Like

@export var mob_scene: PackedScene
var score


func game_over():
	$MobTimer.stop()

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

func _on_start_timer_timeout():
	print ("timer good")
	$MobTimer.start()
	

func _on_mob_timer_timeout() -> void:
	print("mob timer triggered")
	var mob = mob_scene.instantiate()
	add_child(mob)
	print ("mob instantiated and added")

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

	# Set the mob's position to a random location.
	mob.position = mob_spawn_location.position

	# Apply a velocity to the RigidBody2D mob
	var velocity = Vector2(100, 0)  # Set an initial velocity
	var direction = 45.0 * deg_to_rad(1)  # Angle in radians (45 degrees)
	var rotated_velocity = velocity.rotated(direction)

	# Set the mob's linear_velocity (this works for RigidBody2D)
	mob.linear_velocity = rotated_velocity

	if mob_scene == null:
		print("mob_scene is not assigned!") ```

Oh! Thank you! I was very confused on why it wasn’t typing the whole thing correctly! And yes, none of the print statements are printing so I’m very confused about where I went wrong.

I would bet your timer’s timeout signal isn’t connected, you can do this through the editor or through code. If you did it through the editor you should see a green symbol by the func _on_start_timer_timeout name.

To connect the timer through code use .connect

func _ready() -> void:
    $StartTimer.timeout.connect(_on_start_timer_timeout)

like this? because it still doesn’t work ;-;
timeout

sorry I’m very much a beginner

That’s correct. Are you sure new_game is called? seems like the only thing in your way could be that the timer isn’t being started at all

Thank you so much

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.