0 votes

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://Game.gd and res://world/props/Portal.gd. The two levels are in res://world/levels

Thank you!

asked May 5, 2019 in Engine by Hugh Bagan (17 points)

2 Answers

+2 votes
Best answer

Here https://docs.godotengine.org/en/3.1/getting_started/workflow/best_practices/scene_organization.html#choosing-a-node-tree-structure, and in the "changing scenes" are some ideas of how to organize your scenes.

TL;DR, your scenes do no t need to be the main scene of the game, just put levels on a sub-node of your game, hide or remove from the tree when not needed.

In that case you can chose to keep important data outside of the player and re-initialize it on a new instance on the new tilemap, also the player can be a sibling of the tilemap (if can avoid being a child).

Otherwise, you will need some scene manager and data storage autoloaded nodes/scripts to maintain states between transitions.

All the "best practices" sections mention many things that can give you more ideas of how to adapt features to your design, or how to refactor your scenes to fit your needs.

answered May 5, 2019 by eons (7,764 points)
selected May 10, 2019 by Hugh Bagan

Thanks for the reply. That documentation you linked was especially useful.

I could re-instance the Player in each level. But luckily I've managed to get my prototype working. I can move the same Player instance from level to level without having to recreate it. I'll leave a breadcrumb trail of the sources I used to get to this point:


https://docs.godotengine.org/en/3.1/getting_started/workflow/best_practices/scene_organization.html#choosing-a-node-tree-structure: followed the link to...

https://docs.godotengine.org/en/3.1/tutorials/misc/change_scenes_manually.html#doc-change-scenes-manually: Referring to this page I am using strategy 3 to change levels.

Then I was googling things like godot move node since I wanted to move the player to a different part of the SceneTree. I found this: https://davcri.github.io/posts/godot-reparent-node/ which reminded me that I need to remove the player as a child from the first level in order to add it to the second. Then I rewrote the method completely and used this https://godotengine.org/qa/9806/reparent-node-at-runtime to write the part that reparents the Player to the new level.

Finally, this thread https://docs.godotengine.org/en/3.1/tutorials/threads/thread_safe_apis.html#scene-tree gave me the idea to add a liberal sprinkling of call_deferred in place of every regular add_child and remove_child in my script. After that it seems to be working now.


Here is the commit with this change. The most important code is inside the portal method of Game.gd. All that's left to do is set the player's position correctly in the receiving level.

If anybody has improvements to suggest then please have at it. Reading the Debugger it looks like there are still some set_owner shenanigans going on. Godot is a mystery.

When I have this prototype polished and cleaned up then I might present it.

–1 vote

I found this tutorial that I think will help you https://www.youtube.com/watch?v=9laHKHYNyXc&feature=youtu.be

answered May 6, 2019 by Skydome (68 points)
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 How to use this Q&A? before posting your first questions.