My First 2D Game

Godot Version

4.2.1

Question

Hello

I’ve just started learning Godo, and “The First 2D Game” is my first project!

I’ve been working on this project for hours, but I can’t get the mobs to display on screen!

I’ve been reading the GodoDoc for hours; I’ve also watched the YouTube video, but it’s outdated and doesn’t work with the 4.2 GodoDoc version. I’m really lost now, and that’s the only place I hope to find a solution.

extends Node

@export var mob_scene: PackedScene
var score


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 OnStartTimerTimeOut():
	$MobTimer.start()
	$ScoreTimer.start()


func _on_mod_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)
	
func _ready():
	new_game()

The code looks fine to me. Are you sure the signals are connected correctly? For example the OnStartTimerTimeOut looks a bit off to me, since Godot usually generates lower case method names when you connect signals. So I assume you created this method by hand. Maybe you didn’t correctly connect the signal to this method?

If a signal is connected to it, there should be a green icon in front of it like this:
image

Without this, the start timer finishes but does not do anything.

1 Like

Using the default lowercase does not work! It gives an error, therefore using OnStartTimerTimeout makes it work for some reason. I am aware of the connection issue, and I have written the code three times and checked the connections several times, but nothing works!

That is odd, it shouldn’t give an error for using lowercase with _ between the words. What error exactly did it give you?

But anyway, I suggest you try the following:

  • remove the complete OnStartTimerTimeOut function
  • select the StartTimer node and go to the signals tab
  • disconnect any methods that may be connected to the timeout currently to start over
  • create a new signal connection from the timeout signal to the script on the Main node. This should create the method for you. You should see a green arrow next to it on the left.
  • add these two lines to the newly created method:
    $MobTimer.start()
    $ScoreTimer.start()
    

It doesn’t work with the default text! I get the error in the picture. This is the first error I’ve seen using Godot, and adding upper cases appears to be the solution

Ok that error message is actually helpful! You spelled the timer node wrong. Rename the node in the scene tree to be “MobTimer” (you currently have “ModTimer”).

To add more explnations: when the method is named _on_start_timer_timeout and connected to the signal the error message comes up (because the code actually runs). When you had it named OnStartTimerTimeOut and it wasn’t connected to the signal, the code didn’t run. So there was no error message either as it never reached that part of the code.

PS: I wish I could tell you that with practice you’ll avoid these errors, but to be honest I still make spelling mistakes with many years of experience :grin:

1 Like

Wohooo! :gdparty:

2 Likes

Now it Works! Thank you very much, man. I truly appreciate it. I’ve been staring at the screen and rewriting code for hours like an idiot because of one “b.”

2 Likes

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