Problems with changing scenes/rooms

Godot Version

4.0.2 stable

Question

I have a problem with changing scenes in my RPG. I created 2 doors that go to a basement. The second scene door goes outside. All this is made with tilemaps. What i did was made the doors witha Node2D and a collisionShape2D. For documentation purposes I named them Portals.
When the player character goes over to the door it comes in contact with the collision shape. When the player presses Enter, the code for the scene change executes. Its supposed to change scenes but it changes to the wrong scene. It goes to the basement instead of going outside. I know its confusing the 2 portals but I don’t know why.

this is the code for the basement scene door.

extends Area2D

var entered = false

portal to castle basement

func _on_body_entered(body):
entered = true

func _on_body_exited(body):
entered = false

func _process(_delta):
if entered == true:
if Input.is_action_just_pressed(“ui_accept”):

get_tree().change_scene_to_file(“res://World/CastleWyvern/castle_basement.tscn”)

This is the code for the door to outside the castle.

extends Area2D

var entered = false

portal to outside

func _on_body_entered(body):
entered = true

func _on_body_exited(body):
entered = false

func _process(_delta):
if entered == true:
if Input.is_action_just_pressed(“ui_accept”):
>get_tree().change_scene_to_file(“res://World/CastleWyvern/castle_wyvern.tscn”)

call change scene in func _on_body_entered(body) and check input in _process

but it would be best if you send the param of the scene name through the signal

probably after pressing enter both calls are activated and since it is in the process, one overrides the other…
try to print() to both of them to see if they both run

have already posted here one solution how I solve it in my project, so maybe it will help

2 Likes

I tried the solution you posted but my signals don’t run. Sorry for the late reply. I ran the code below but it doesn’t even show the print command. I’m also thinking its a bug with this version of Godot. I’m using 4.0 stable. Also the player is in its group.

@export var area = “CastleWyvern”
@export var scene = “castle_wyvern”

func _on_body_entered(body):
entered = true
if Input.is_action_just_pressed(“ui_accept”):
if body.is_in_group(“player”):
var _catch = get_tree().change_scene_to_file(“res://levels/” + area + scene + “.tscn”)
print(“LOAD: res://levels/” + area + scene + “.tscn”)

I tested it in 4.x and it works ok

check if the signal is well connected
obrázok

and check if the player is actually assigned to a player group

and put one print() statement directly into the function, not after the if for the testing

1 Like

Thank you. I finally figured out what was the problem. Although its still not changing scenes. The problem was that the collision for the portal was not set to point to the player. Its was the collision mask.