How to make Area transitions?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 703337

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

:bust_in_silhouette: Reply From: njamster

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>")

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?

703337 | 2020-08-31 21:16

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.

njamster | 2020-08-31 22:11

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

ToFall.gd:5 [wrap=footnote]703337 | 2020-08-31 22:22[/wrap]

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.

njamster | 2020-08-31 22:38

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

703337 | 2020-08-31 22:48

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.

njamster | 2020-08-31 22:57

I’ve decided to use button to transition instead.

703337 | 2020-09-01 01:29