Version: 3.1; Project Source
Hi folks. I'm working on a 2D grid-based game based on a prototype by GDquest. In the game the player can traverse a tilemap. I would also like the player to be able to move to other tilemaps. Some approaches I've seen of "connecting" tilemaps together simply have the entire game in one tilemap and move a Camera2D around, ala The Legend of Zelda. That is not what I want. I am trying to do something closer to Pokemon or other RPGs where, for example, the player can go inside a house and be transferred to the house's unique interior.
One thread I found gives us a way to switch scenes back and forth. It stores the scenes in a dictionary in order to switch between them. I made an improvement that allows the changes in the scenes to persist. I detach the scene from the node tree and store it in the dictionary instead of destroying & recreating it. This allows the player to move things around in one map, go to a different map, and come back to the previous map and their changes will still be present.
Here is my project source. You can observe the behaviour above by pressing 1 and 2 to alternate between maps. Try pushing some blocks around and notice how they stay where you left them after switching between 1 and 2.
However, an important part of this mechanic is being able to move the player to the receiving tilemap/scene. I created a "Portal" class that attempts to solve this by detecting when the player collides with it and moving the player to a corresponding Portal object on the tilemap in the second scene. My logic is that I first detach the player from inside the first scene and save it. Then I detach the first scene (it's saved in the dictionary). Then I attach the second/receiving scene to the SceneTree. Then I finally reattach the player to the second scene (which now becomes the current map). I also explained this in this reddit thread.
But for whatever reason, the Player won't reattach to the second scene. When I try to do
add_child it is not working. I can tell because I don't see the Player in the Remote view in the editor after I go through the Portal, regardless of the Player's coordinates on the screen. You can reproduce this by walking through the top of the level into the diamond shape (ie. the Portal). (If the game crashes, keep trying until it goes through).
I think that I'm not doing the
add_child call correctly, or I am not doing the Node removals/reattaching correctly since my research on this problem suggested I use
call_deferred, which did not work. This thread on
add_child might be the issue I am having.
Any insight would be appreciated. If there is a better way to accomplish what I am doing please tell me. If I can solve this problem and complete this mechanic then I will distribute the solution in a tutorial or back to GDquest. I will also be sharing this Ask to the Godot/GDquest reddit and discord communities.
TL;DR: Download my project and please tell me why the Player isn't being added to
Level2.tscn when I try to add it as a child. The concerning code is in
res://world/props/Portal.gd. The two levels are in