Move player to another scene?

Hi all,

Here’s an odd one for you. I’m looking to do something similar to what’s found in the sub game demo in order to simplify some logic in my game. Ideally I’d like to transition the player to the “sub game” without them really knowing it’s happening. By that I mean, somehow have the player character maintain any momentum or state they may have between scenes.

I could perhaps serialize the object and pass it to the new scene but that doesn’t seem like it would be very seamless. Any thoughts?

TIA

Edit: I found this thread looking to do something similar.

I wrote this hack that at first glance seems to work. I’m not sure what implications it has but it feels like powers a mortal should not wield.

obj.scene.sys.updateList.remove(pawn);
obj.scene.sys.displayList.remove(pawn);
obj.scene = scene;
scene.sys.updateList.add(obj);
scene.sys.displayList.add(obj);

i’m intrigued by this. but can’t seems to understand.

can you explain the context of these code ?
where are these placed. and what is obj ?
or even better if you could give an example case.

Sure, I’ll briefly explain my use case. I have multiple scenes running simultaneously that I want an object (Sprite) to be able to move between at will but can only exist in one scene at a time. This could be used for example in a Diablo style game where you can drag an item from the world (main scene) to your inventory (UI scene).

So in my code example, obj could be a sword sprite that is being transferred from scene to scene. As for where the code is placed, it could be in a dragend event or any event where you’d want to trigger the code.

A word of caution, though: that example code is heavily untested and only works for simple objects like Sprites. I’ve since learned that it does not work for nested objects like Containers with Sprites.

1 Like

thanks for sharing these tricks. might use this in the future when the case arrives

so this already tested when the scene shut down?

i believe it won’t work for container since every object within the container still registered as previous scene object. i believe adding an iteration over the container child should do the trick.

Instead of multiple scenes, would it be possible for you to include each scene’s content in to a separate container?
So there will be one seen with multiple containers representing groups of game objects.
Then it would be easy for you to move player from one container to another.

Hope this helps.

1 Like

it is possible but that would defer the use of scene at all.

if it could serve your need then it’s fine. but i prefer using multiple scene for reasons

This is a good thought and may work in most cases. I have many systems that need to be managed during the transition from one context to another that using the Scene lifecycle just makes easier.

I appreciate the suggestion, though!