Change scene with keep player position

  • #1, by TinTinSunday, 19. November 2017, 08:23 7 years ago
    Hi

    I want to switch between two scene with keep character position. My mean is player appear in the new scene with the same position in the previous scene.
    I want this occur automatic for any time player want switch between scenes. 

    Thanks
     

    Forum Fan

    196 Posts


  • #2, by afrlmeSunday, 19. November 2017, 12:53 7 years ago
    You're going to have to use Lua script for both storing & changing the scene. Let's start off by creating a function that you can execute from inside of an execute a script action part. Create a new script in the script section of the editor & add the code block below into it.
    function cswcp(scn)
     pp = game.CurrentCharacter.Position -- store current characters position
     game.CurrentScene = Scenes[scn] -- update scene
     game.CurrentCharacter.Scene = Scenes[scn] -- update scene current character is on
     game.CurrentCharacter.Position = pp -- update current characters position
    end

    Now to use that call that function inside of an execute a script you would add something along the lines of this...
    cswcp("town")

    Untested & I'm still half asleep, but it should work.

    Imperator

    7285 Posts

  • #3, by TinTinSunday, 19. November 2017, 15:36 7 years ago
    Thanks for help
    I don't success yet.Sorry I'm not good at  Lua script. I did this :

    1) I made a new script in Lua editor and pasted your codes
    2) In scene 1 I made n excute script in the acion area and paste ' cswcp("Scene2")'  and added 'switch to the scene  ' too.

    Player disappear and goto next scene and placed in wrong location

    Where is my wrong?

    Forum Fan

    196 Posts

  • #4, by ke4Sunday, 19. November 2017, 16:45 7 years ago
    You don't need to be switching scene the script is already doing that.
    1. It stores the character's position
    2. It switches the scene
    3. It sets the character to the scene
    4. It sets the character to the correct position

    You only need the one execute script action part
    cswcp("Scene2")

    The function needs to be in the scripts tab and the script needs to be set as definition script (default)

    Key Killer

    810 Posts

  • #5, by TinTinSunday, 19. November 2017, 17:24 7 years ago
    Great!!! . Works it now . Thanks so much .But before switch to another scene , player disappeared . It's any way for fix it?

    Forum Fan

    196 Posts

  • #6, by afrlmeSunday, 19. November 2017, 19:31 7 years ago
    I suppose you could create a temporary mainLoop to listen out for when the scene has changed.

    function cswcp(scn)
     pp = game.CurrentCharacter.Position -- store current characters position
     game.CurrentScene = Scenes[scn] -- update scene
     getTime({flags=1, reset=true}) -- start a timer
     registerEventHandler("mainLoop", "update_player_pos") -- start a loop
    end
    
    function update_player_pos()
     if getTime() >= game.FadeDelay then
      game.CurrentCharacter.Scene = Scenes[scn] -- update scene current character is on
      game.CurrentCharacter.Position = pp -- update current characters position
      unregisterEventHandler("mainLoop", "update_player_pos") -- kill the loop
     end
    end

    Works the same as before, except now we have a mainLoop with a delay that waits for the global scene fade time before teleporting character to new scene & updating their position. Should work...

    Imperator

    7285 Posts

  • #7, by TinTinMonday, 20. November 2017, 15:35 7 years ago
    Thanks. I tried your new codes and have the same problem yet.  Player go to invisible before fade.

    Forum Fan

    196 Posts

  • #8, by afrlmeMonday, 20. November 2017, 15:47 7 years ago
    ok replace game.FadeTime in the update_player_pos function with a custom time. Try using a custom value instead, say 1000 or so. 1000 = 1 second in milliseconds.

    Imperator

    7285 Posts

  • #9, by ke4Monday, 20. November 2017, 15:48 7 years ago
    Hm seems the problem is in be the changing scene part. I¨ve tried to just change the scene alone and the character disappears while changing the scene.

    game.CurrentCharacter.Scene = Scenes[scn]

    I suppose it would work if you would set the fade time to 0. Not sure why is the character disapearing during that.

    Key Killer

    810 Posts

  • #10, by afrlmeMonday, 20. November 2017, 16:05 7 years ago
    Maybe it assumes it's the regular scene change? Does the character teleport to the new scene when you only change the scene with Lua script?

    Imperator

    7285 Posts

  • #11, by TinTinMonday, 20. November 2017, 16:19 7 years ago
    ok replace game.FadeTime in the update_player_pos function with a custom time. Try using a custom value instead, say 1000 or so. 1000 = 1 second in milliseconds.
     There is no "game.FadeTime" in your codes. Did you mean "game.FadeDelay" ?
     
    I write
    if getTime() >= 1000 then .... and doesn't work .



    Forum Fan

    196 Posts