Keeping YSort and Using Chunking for 2D Open World

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

Hey, I’m really new to this, hence the username. I have experience programming though!

Anyways, I was following an RPG tutorial, but I was just curious and wanted to mess around with say if I hypothetically made a very large map open world.

I understand I need to do chunking, so I load nearby areas to the player and free the areas that are further away.

I have been trying to implement that by having each area be a scene, all governed by one larger scene that has variable references to the adjacent scenes that need to be instanced and updates accordingly. However, one (of many questions/issues) is that the way I have the scenes set up, each scene has it’s own player character with a camera, and its own ysort. How do I maintain the appearance of the ysort working as I traverse scenes?

As in currently, the player can walk between different scenes, but the ysort only works for the scene I started in, and doesn’t work for the rest.

I noticed that the player starts in the last scene I add to the tree, so it must have something to do with how the player is a part of the scene that messes things up?

I was thinking of having each scene detect when the player leaves and enters and then to queue free or instance it again. But that doesn’t work.

I know that similar questions have been asked many times, but I couldn’t find anything that quite exactly mentioned the ysort problem or the multiple players, or i would really appreciate a link!

Thank you very much!

each scene has it’s own player character with a camera

Why? If each scene is only a chunk of the world “governed by one larger scene”, then the player instance should be part of that larger scene, isn’t it?

the ysort only works for the scene I started in, and doesn’t work for the rest.

What exactly do you mean by “doesn’t work”? Can you provide a screenshot?

I noticed that the player starts in the last scene I add to the tree

Knowing the scripts you use to spawn the player or the tree structure of your scenes would certainly help in finding the reason for that. From your description alone I’d assume there’s one player in each of the chunks?

I was thinking of having each scene detect when the player leaves and enters and then to queue free or instance it again. But that doesn’t work.

Why not? Again: Without knowing your script it’s impossible to help you. It certainly should be possible, even though I think it’s a better idea to maintain the player instance independent of the map chunks (as written above).

njamster | 2020-05-29 10:35

Yeah, I initially thought that having the player be a part of the larger scene would make sense. However, Ysort needs to be a child of the map chunk, with player underneath it to function, right?

I definitely believe that it is wrong to have a player and camera under each chunk

When I said doesn’t work, I meant like when the player goes behind something on the other map chunk, the player sprite appears above the object instead of behind it.

Yes there is a player in each chunk, which is a large problem. But I thought it was necessary in order to use YSort.

Perhaps there is a solution that could avoid YSort?

I can post some code if you like! But I’m just trying to work out how to solve this problem without having a player under each scene then.

neophyte | 2020-05-29 17:12

:bust_in_silhouette: Reply From: njamster

YSort-nodes are nestable. So something like this should work:

- Main (Node-Type: YSort)
  - Chunk1 (Node-Type: YSort)
    - Object1 (Node-Type: Sprite)
    - Object2 (Node-Type: Sprite)
    - Object3 (Node-Type: Sprite)
  - Chunk2 (Node-Type: YSort)
    - Object4 (Node-Type: Sprite)
    - Object5 (Node-Type: Sprite)
  - Player (Node-Type: KinematicBody2D)
    - Outfit (Node-Type: Sprite)

If you want to add something to a chunk that is not supposed to be y-sorted, simply separate it by a Node (not a Node2D!) which does not have a y-position:

  - Chunk3 (Node-Type: YSort)
    - Object1 (Node-Type: Sprite)
    - Object2 (Node-Type: Sprite)
    - Not-Sorted (Node-Type: Node)
      - Object3 (Node-Type: Sprite)

You can still use the z-property of Object3 to shift it to the fore- or background.

Thank you for your tip, help me to solve my problem. I put a few objects under Node2D as a scene, and when I instanced that scene to the world scene, Ysort is not working. With your input, I have changed the Node2D to a Ysort node, and it works. Now all objects in the instanced scene works nicely with Ysort in the world scene.

Idleman | 2020-08-22 05:10