having sidekick (NPC) following the player from scene to scene

  • #1, by quentin-ayralThursday, 21. March 2019, 11:23 5 years ago
    Hi everyone,
    i'm trying to have a NPC following me 90% of the time, i'm able to do that, but i've find pretty laborious having to create for every scene change, a command for the player and a different  one for the NPC, and starting points too.

    I don't mind doing that all the way down if thats the only way to do it, but i was thinking maybe there is another way smile

    Thanks !

    Newbie

    6 Posts


  • #2, by afrlmeFriday, 22. March 2019, 13:31 5 years ago
    Yes, you can use Lua script to listen out for scene change & check a condition or value or something & whether or not the chasing character is on the same scene as the current character & if they aren't then teleport them to a specified position (maybe you could store the position of the player character at begin of scene or something).
    
    
    local scn = nil
    
    local pos = nil
    
    local npc = "Tom" -- chasing npc name
    
    local cond = "npc_chase" -- chase state condition name
    
    
    
    function onSceneChange()
    
     if not game.CurrentScene:isEmpty() and game.CurrentScene:getName() ~= scn then
    
      if game.CurrentCharacter.Scene == game.CurrentScene and not Scenes[scn]:IsMenu then pos = game.CurrentCharacter.Position end
    
      scn = game.CurrentScene:getName()
    
      -- + --
    
      if Conditions[cond].Value and game.CurrentCharacter.Scene == game.CurrentScene then
    
          if Characters[npc].Scene ~= game.CurrentCharacter.Scene then
    
              setDelay(1000, function()
    
                  Characters[npc].Scene = game.CurrentCharacter.Scene
    
                  Characters[npc].Position = pos
    
              end)
    
          end
    
      end
    
     end
    
    end
    
    
    
    registerEventHandler("mainLoop", "onSceneChange")

    Just a quick example. I don't know if it will work as I've not tested it, & you will need to edit it a bit too & replace the name of the npc & the condition. The general idea is that the function listens out for scene change & then checks if the condition is true & whether or not the npc character is on the same scene as the player, if they aren't then they should be teleported to the same position that the player character started.

    Imperator

    7278 Posts

  • #3, by MachtnixFriday, 22. March 2019, 19:11 5 years ago
    Boah, ey... two years too late... wink Nice script.

    I tried to read out the character's and NPC' position and angle before I change the Scene, but I failed. You have the option to set the NPC following in a defined distance, have you? So if the character starts strongly left (or right) on the scene (what is normally so), there is no place for the NPC on the left side. Thats made me crazy. I tried to check out the NPC's position first (more left) and add the character's position onto the distance at the right side, but it doesn't work...

    Thread Captain

    1097 Posts

  • #4, by afrlmeFriday, 22. March 2019, 19:25 5 years ago
    I would recommend doing what I have done in the script, set a delay before having the npc spawn in - but sure, you could check if the player character is less than or more than x position to determine whether to offset the npc x pixels to the left or the right, but another solution would be when you spawn your character outside of the scene background would be to check their position in an at begin of scene action & start a cutscene & send them to a position inside of the scene, that way when the npc spawns in they should remain where they are until the playable character starts walking. There's other things you could do too though, like in the other scripts I wrote before where you check the state of the playable character to delay when the npc should chase, or stop, or disable chasing so the npc can perform random animations or preprogrammed actions around the particular scene they are on (like in sam & max).

    In regards to position/offsets, you need to adjust both x & y & not just position.x or position.y. For some reason only trying to update one or the other doesn't seem to work.

    local pos = game.CurrentCharacter.Position
    
    
    
    if pos < 500 then
    
      Characters["npc"].Position = {x = pos.x - 100, y = pos.y}
    
    else
    
      Characters["npc"].Position =  {x = pos.x + 100, y = pos.y}
    
    end

    Imperator

    7278 Posts

  • #5, by MachtnixSaturday, 23. March 2019, 13:18 5 years ago
    It would be possible to set both characters on the same place behind a wall or a door (you can't see them). Or only the main character is been visible (correct English?? wink wink  ). The setting is "NPC follows". When the main character starts walking out, the NPC will follow automatically using the defined distance, won't he?

    Thread Captain

    1097 Posts

  • #6, by darren-beckettThursday, 28. March 2019, 08:28 5 years ago
    The simplest solution (And i think it works very well):
    - I always have my main character start offscreen and walk to the required starting point
    - The script will watch for a scene change and teleport any following characters onto the same scene as the main character.
    - Because the main character is offscreen, the npc's will also be offscreen and will follow them onto the scene as the main character walks towards the required starting point.

    A neat trick i've also added for slow moving NPC's, is if my following character is more than a certain distance away from the main character, they will switch to a "Running" outfit, which means they will run to catch up, which you switch back when they stop again.

    Great Poster

    384 Posts