Mob node doesn't work in "Your first 2D game" tutorial

Godot Version

4.2

Question

I the mob doesn’t generate when running the main scene. At this page The main game scene — Godot Engine (stable) documentation in English . There is another post with the same problem that says solved but I could not fine the solution in it.

Mob.gd
‘’’
extends RigidBody2D

-# Called when the node enters the scene tree for the first time.
func _ready():
var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
$AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

-# Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
pass
‘’’
Main.gd
‘’’
extends Node

@export var mob_scene: PackedScene
var score

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

-#Called every frame. ‘delta’ is the elapsed time since the previous frame.
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)
print("Hello")

‘’’
Player.gd
‘’’
extends Area2D
signal hit

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.

-# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
#hide()
-# Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta):
var velocity = Vector2.ZERO # The player’s movement vector.
if Input.is_action_pressed(“move_right”):
velocity.x += 1
if Input.is_action_pressed(“move_left”):
velocity.x -= 1
if Input.is_action_pressed(“move_down”):
velocity.y += 1
if Input.is_action_pressed(“move_up”):
velocity.y -= 1

if velocity.length() > 0:
	velocity = velocity.normalized() * speed
	$AnimatedSprite2D.play()
else:
	$AnimatedSprite2D.stop()
position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)

if velocity.x != 0:
	$AnimatedSprite2D.animation = "walk"
	$AnimatedSprite2D.flip_v = false
	# See the note below about boolean assignment.
	$AnimatedSprite2D.flip_h = velocity.x < 0
elif velocity.y != 0:
	$AnimatedSprite2D.animation = "up"
	$AnimatedSprite2D.flip_v = velocity.y > 0

func _on_body_entered(body):
hide() -# Player disappears after being hit.
hit.emit()
-# Must be deferred as we can’t change physics properties on a physics callback.
$CollisionShape2D.set_deferred(“disabled”, true)

func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
‘’’
Can anybody help?

Just an FYI, the complete source code is located here if you were not aware;

1 Like

I will see if I can find out what’s wrong with my code.

Thank you.

I have looked through the link in github. I have found no solution.

This function is never called. The timer is set 0.5s but never calls this function.

func _on_mob_timer_timeout():

Does anybody know how to contact the people who manage the Docs?

Hello !

I haved the same problem as Rastan but found solution.

Short answer :
It’s the link with signaltimeout” was unknown and not bind automatically.
The StartTimer need to link his “timeout” signal with the : (into main.gd script)

func _on_start_timer_timeout():

Long Answer :

  1. First : Be careful to have your StartTimer has child of Main node, like this :
    image

The rest below…

Sorry i’m new into godot community and this forum refused more than one image for new arrival …
2) Link the timeout signal with main scene script :

  • Click on the StartTimer node
  • Click on “Node” to see signal of StartTimer Node
  • Double Click on “timeout()”
  • Keep the default parameters and name
  • Click on “Connect”

The rest below…

1 Like

Sorry i’m new into godot community and this forum refused more than one image for new arrival …
3) You should have the link visible like this :
image

For me this solution work.

You will have the same problem with :

func _on_mob_timer_timeout():

Alternative solution :
You can used this alternative solution if you want to see this link explicitly in your code : Using signals — Godot Engine (stable) documentation in English

Like this :

func _ready():
	var start_timer = get_node("StartTimer")
	start_timer.timeout.connect(_on_start_timer_timeout)
1 Like

Thank you sir!

1 Like

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