getting_started/first_2d_game/05.the_main_game_scene #21
Replies: 56 comments 110 replies
-
For the code to actually work, the timer need to be activated through: "func _on_start_timer_timeout():". However, to do so you need to head into the timer node(for the example above, it would be the StartTimer node on the left side of your screen under main in the scene) change tab from inspector to node on the right side of your screen and connect the timeout signal to the main node. Just right click the timeout signal(or double click it), a window will pop up and then left click the main node and press connect. Then do that for all three of the timers(StartTimer, ScoreTimer, MobTimer). (The code didn't work for me until I did this, so I dont know if its my personal issue or not but ill leave the solution here for future viewers) |
Beta Was this translation helpful? Give feedback.
-
I'm having problems with the "$Player.start($StartPosition.position)", it gives "Invalid call. Nonexistent function 'start' in base 'Area2D (Player.gd)'? I was thinking it would be more like "$Player.position(...)" but that doesn't work either. |
Beta Was this translation helpful? Give feedback.
-
In the testing phase, the creatures don't appear. |
Beta Was this translation helpful? Give feedback.
-
C# users must change the connections under the Node tab to C# nomenclature, not the default it provides you. For example: When connecting timeout() from MobTimer, ScoreTimer, StartTimer to Main, it will default to _on_mob_timer_timeout and so on. This needs to be changed to OnMobTimerTimeout(), and so on for the rest. If you are having this issue at this point, you will also need to go back into the Player class and change _on_body_entered to OnBodyEntered() |
Beta Was this translation helpful? Give feedback.
-
This is crazy talk... please maybe an animation or a screenshot of what you're saying here? |
Beta Was this translation helpful? Give feedback.
-
Hi, I keep getting the error "Invalid set index 'position' (on base: 'RigidBody2D (mob.gd)') with value of type 'PathFollow2D'. I'm new to Godot so I've got no idea what it means, it causes the game to crash after a couple seconds. Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
-
I get this error: "player.gd:10 @ start(): Node not found: "CollisionShape2D" (relative to "/root/Main/Player")", so it doesn't run at all. I double checked and there is indeed a CollisionShape2D node underneath Player... What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
-
Hi, newbie here trying to get tutorial working. Godot version 4.3 stable in question. "Script inherits from native type 'RigidBody2D', so it can't be assigned to an object of type: 'AnimatedSprite2D' Tried to find out if anyone else has this problem but no luck. |
Beta Was this translation helpful? Give feedback.
-
I was following along pretty well but I´m a bit lost on the _on_mob_timer_timeout function. My questions are:
The rest of the tutorial is amazing. Thank you for the excellent work you put into it. |
Beta Was this translation helpful? Give feedback.
-
Something odd with Godot 4.3 on Mac Sequoia (not verified with other platforms). The game starts and plays as expected but the MobTimer stops firing after a random number of mob instances has been created and freed. Eventually the player is left on the screen with no more mob instances and I have to quit the game. I added prints to verify this. |
Beta Was this translation helpful? Give feedback.
-
so its not working when i put hide() in the player script on _ready the player never pops back up. When I put new_game() in the _ready function in Main the game crashes and I get 3 errors about unused parameters these include: _process function delta parameter in main.gd Even when new_game() is taken out of the ready function in main.gd the game runs with an empty window just a blank grey screen. Please Help. |
Beta Was this translation helpful? Give feedback.
-
I'm confused and stuck at _on_mob_timer_timeout. I copy and pasted the code into the main script but can't see the creatures. Using GDScript. |
Beta Was this translation helpful? Give feedback.
-
I had to add
|
Beta Was this translation helpful? Give feedback.
-
To elaborate on comments above and in the code on how Imagine that the path drawn is a clockwise race car track where the start/finish line is point 1 (the upper-left corner, where we started drawing the path). When an imaginary race car is on any point of the track, it's pointing in the "forward" direction of the track. As the race car is going along the track, it's progressing along from 0 to 100% of the track... (or a ratio 0.0 to 1.0).
Node
... is setting the race car location to a random amount of "progress" along the track (or lap). Its direction is pointing "forward" for its position on the track. To change the direction to point inward, (which is on the race car's right side), we first rotate it +90 degrees (
|
Beta Was this translation helpful? Give feedback.
-
I do not have the 'hit' scene. Even without filter 'hit' I cannot find it anywhere in the scenes. https://ibb.co/vjnyDM2 |
Beta Was this translation helpful? Give feedback.
-
Hi all, Im new to Godot and trying to learn it for a team project (we all are but im the furthest ahead) and we all picked this to be our practice to get used to it, I was able to get my lil man back on screen after having to re do the entire thing due to an error on my part but the enemies wont spawn (earlier nothing spawned) 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.
func _ready():
screen_size = get_viewport_rect().size
hide()
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 the following 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: Node2D) -> void:
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
mob.gd
extends RigidBody2D
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()
main.gd
extends Node
@export var mob_scene: PackedScene
var score
func _ready() -> void:
new_game()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func game_over() -> void:
$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) also I appologies, i dont know how to indent the code here, maybe i did it there, but if not, please forgive me |
Beta Was this translation helpful? Give feedback.
-
Has anyone encountered this error before? It gets raised in my _on_mob_timer_timemout() function, more specifically here:
|
Beta Was this translation helpful? Give feedback.
-
Hi! I started learning Godot, and the tutorial is working as intended, but i think i miss something. When i try to collide the player agaisnt a Mob, it pass through the mob and the game doesnt end. Can someone help please? |
Beta Was this translation helpful? Give feedback.
-
Hi! I just started to learn Godot and have been following this tutorial. I'm having a problem with where the mobs spawn. They keep spawning in the top left corner where the path starts, but nowhere else. I re-read the steps at least 10 times and even re-did the path two times while checking it with the tutorial every second. Does anyone have any tips on how to fix this and make mobs spawn around the screen? |
Beta Was this translation helpful? Give feedback.
-
I am new and I have followed the steps all the way till we test it on this step and I keep getting this error main.gd:21 @ new_game(): Node not found: "Player" (relative to "/root/main"). I have tried to fix it but I nothing I do fixes it please help |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm a newbie and when testing the game at this step, I keep getting the error mob.position = mob_spawn_location.position and I'm not sure how to fix this. Here's the rest of the code for the Mob node, which should be identical to the tutorial. extends Node
@export var mob_scene: PackedScene
var score
func _ready() -> void:
new_game()
func _process(delta: float) -> void:
pass
func game_over() -> void:
$ScoreTimer.stop()
$MobTimer.stop()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
func _on_mob_timer_timeout() -> void:
var mob = mob_scene.instantiate()
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()
mob.position = mob_spawn_location.position
var direction = mob_spawn_location.rotation + PI/2
direction += randf_range(-PI/4, PI/4)
mob.rotation = direction
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
add_child(mob)
func _on_score_timer_timeout() -> void:
score += 1
func _on_start_timer_timeout() -> void:
$MobTimer.start()
$ScoreTimer.start() |
Beta Was this translation helpful? Give feedback.
-
ISSUE (Solved): Mob Enemy Animations not playing (stuck on one animation frame) As of 3/25/2025 The problem is one changed line (actually from the previous section, but as the game is not tested until this part of the tutorial, I am putting it here, as I'd assume others with the same issue would check this comment section first before going to a previous one.) In mob.tscn at the function " func _ready()" : the current tutorial has you use this line: To get the mob sprites to play properly, I had to revert to the old line: and then it worked for me |
Beta Was this translation helpful? Give feedback.
-
Is there a reason the "_ready" and "_procces" functions are unused? I though they were both required or at the every least useful to make things in your game work? |
Beta Was this translation helpful? Give feedback.
-
I am determined to use C# but the whole "Signals" event system seems to cause trouble. As suggested by someone else, I am creating the callback functions and then building the project, before I connect them to the signal by using "Pick". So far it has worked just fine, but with the "hit" and "timeout" signals I am getting the following warning:
These methods exist and they are indeed connected. The game runs fine... Then why do I get those warnings? |
Beta Was this translation helpful? Give feedback.
-
I added the code and rechecked it 3 times now, why won't my character move!? |
Beta Was this translation helpful? Give feedback.
-
nothing shows up when i start.. am i missing anything or??? |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, when I test the game. The mobs are just flying across the screen and not using their animation properly. Looking for some help :) Velocity controls that right? I have it set to 150.0 and 250.0 in the mob timer timeout function. |
Beta Was this translation helpful? Give feedback.
-
Maybe this is useful to somebody: I ran into the problem that my player wasn't showing up during the gameplay test. Here's what had happened: When creating the mobs, I accidentally created them as a part of the player scene. Reading the main game scene section of the tutorial, I realized my error and went to the player scene to "Save Branch as Scene" the mobs. After that, I could complete the main game scene and the mobs showed up, but the player didn't. It turns out, I also had to remove the mobs from the player scene. Exporting them created a separate mob.tscn, but it did not remove the assets and code from the player scene. After I had removed the mobs from the player scene, the player showed up in the game. |
Beta Was this translation helpful? Give feedback.
-
Hey I'm new to Godot and programming in general and am using 4.4.1. I have the player node linked to the main node like it said to do and am having an issue where it won't load the test. The error I keep receiving is "Script inherits from native type 'Area2D', so it can't be assigned to an object type: 'Node'". As a note I also have the "main" node set as the Main Node for the project like the tutorial says and have all the code written as presented throughout the tutorial up to this point. I've tried unlinking it and still get the same error and I've reattached it just for my own sanity. I've been staring at it for at least an hour trying to figure this out myself and even tried googling the issue to no avail. Any help/advice would be appreciated |
Beta Was this translation helpful? Give feedback.
-
I'm a bit confused. I have everything done exactly like the tutorial says (I double checked) but when I run the game I can move the player but the enemies appear briefly at the side of the screen and then immediatelly dissapear again. They don't move. Not sure what could be wrong here? Thanks |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
getting_started/first_2d_game/05.the_main_game_scene
Now it's time to bring everything we did together into a playable game scene. Create a new scene and add a Node named Main. (The reason we are using Node instead of Node2D is because this node will...
https://docs.godotengine.org/en/latest/getting_started/first_2d_game/05.the_main_game_scene.html
Beta Was this translation helpful? Give feedback.
All reactions