The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

How would I make an Area node change the scene in a first person game?

in Engine by (33 points)

1 Answer

0 votes
Best answer

Assuming your player-character is a Kinematic- or RigidBody, connect the area's body_entered-signal and then call the change_scene-method:

func _on_Area_body_entered(body):
    get_tree().change_scene("<PathToNextScene>")

If there are other moving bodies that might enter the area, you likely want to discern the player, e.g. by checking the bodies name or (imho better) its group:

func _on_Area_body_entered(body):
    if body.name == "player":
        get_tree().change_scene("<PathToNextScene>")

func _on_Area_body_entered(body):
    if body.is_in_group("players"):
        get_tree().change_scene("<PathToNextScene>")
by (10,634 points)
selected by

After trying these, it doesn't seem to have worked. Does it not work if the Area is a scene that I've put in another?

Have you made sure the callbacks are properly connected? Try printing something or setting a breakpoint in the beginning of the function to make sure it's run.

I have been getting an error that doesn't make any sense to me:
W 0:00:06.862 The function 'changescene()' returns a value, but this value is never used.
<C++ Error> RETURN
VALUE_DISCARDED

ToFall.gd:5

That's not an error, but a warning (as indicated by the W in the beginning, shown as a yellow dot in the debugger, errors are red). There are cases where it can be acceptable to ignore a warning, granted you know what you're doing. You can even disable warnings in the project settings altogether if you want.

In your case, the warning Informs you about the fact that the change_scene-method returns a value but you don't use it. Which might be because you forgot about it. Or because the returned value isn't needed anymore, in which case you might change the function. However, change_scene is a built-in function, so the latter is not an option. So you either can choose to ignore the warning or deal with it properly:

var error = get_tree().change_scene("<PathToNextScene>")
if error:
    print("Couldn't change scene, error code: %d" % error)

Your error handling might differ of course, printing the error just acts as an example here.

I've tried doing this, but it crashes the game.

What code are you running? The version checking the name or the version checking the group? In the former case, make sure the player-node is actually called "player" (exactly like this, context-sensitive). In the latter case, make sure you've added the player-node to a group called "players" (again: exactly like this, context-sensitive). Lastly: Which path are you providing to change_scene? Are you sure it's valid and there exists a scene? Other than that, I don't see anything that could even potentially crash the game here. If you checked all of the above and the problem still persists, that's likely due to something else in your project and not related to my answer.

I've decided to use button to transition instead.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.