Switching a scene with LUA

  • #1, by JContraFriday, 19. September 2014, 08:43 11 years ago
    Hi all,
    I'm new to Visionaire. I've done most of my game through the IDE but now some scripting is involved and I seem to be missing something.

    Basically, I would like to switch the scene for the current character using LUA. If anyone has any suggestions it would be much appreciated.

    Cheers,
    JC

    Newbie

    4 Posts


  • #2, by afrlmeFriday, 19. September 2014, 11:48 11 years ago
    What do you mean exactly? Switch to the scene of the currently active character? or show a new scene & switch the current character to that scene?

    Both of what I just said can be done much easier with the editor action parts, that with lua.

    Imperator

    7285 Posts

  • #3, by JContraFriday, 19. September 2014, 12:14 11 years ago
    Hi AFRLme and thanks for replying.
    The reason why I need to script it is because I have 3 characters you can swap between. I have a button that is part of the secondary interface and thus always visible. Basically, when you press the button the scene changes depending on which character is current and which scene they are in.
    It could be done in the editor but it'll become unwieldy. I figured with LUA I could keep it manageable.

    Newbie

    4 Posts

  • #4, by afrlmeFriday, 19. September 2014, 12:32 11 years ago
    Is there one button that toggles between characters & scenes or is there a button per character?

    Imperator

    7285 Posts

  • #5, by JContraFriday, 19. September 2014, 12:50 11 years ago
    There is a button for each character to simplify the change between characters.
    For the scene change button there is only one and hence I need to ascertain which character it is and in which scene they are in.
    I started writing the script but I can't for the life of me figure out how to make the scene change.

    The script logic would be like this:

    local scene = game:getLink(VGameCurrentScene)
    local character = game:getLink(VGameCurrentCharacter)

    if scene:getName() == "Street" and character:getName() == "Fred" then
    -- the scene will change
    else
    -- current character will say something
    end

    Newbie

    4 Posts

  • #6, by afrlmeFriday, 19. September 2014, 14:08 11 years ago
    errr.... there is an action part which switches to scene of specified character including current character.

    scene > change to scene of a character: current character

    The lua version for an interface with a button for each character would be something along the lines of...
    
    function changeChar(c)
     if getObject("Characters[" .. c .. "].CharacterConditions[activated]"):getBool(VConditionValue) and game:getLink(VGameCurrentCharacter):getName() ~= c then
      game:setValue(VGameCurrentCharacter, getObject("Characters[" .. c .. "]"))
      -- + -- 
      if game:getLink(VGameCurrentScene):getName() ~= getObject("Game.GameCurrentCharacter.CharacterScene"):getName() then game:setValue(VGameCurrentScene, getObject("Game.GameCurrentCharacter.CharacterScene")) end
     end
    end
    

    ...above would be a definition script.

    to use you would need to add a condition to each playable character called "activated" which should be set to true if character is available to play with. then to swap to specific character & scene you would create an execute a script action in the interface button containing something along the lines of this...
    changeChar("tom") -- name is case sensitive
    

    ...I can't tell you whether this would work or not as I've not tested the code. It is written off the top of my head. The only thing I can tell you is that if you were to use a single button for toggling between characters, then the code would become a lot more complicated because you would need to be checking against currently active character index & whether the next character index if playable or not etc.

    I recommend just creating the conditions above like I mentioned & querying them in each button & also in the on mouse over actions for each button, that way you will only trigger active image/opacity changes if condition is true & actions listed inside of the buttons will only work on left click if true. See screenshots below... (right click view image for full size)

    Imperator

    7285 Posts

  • #7, by JContraFriday, 19. September 2014, 16:42 11 years ago
    Thanks AFRLme, I feel like I'm getting closer to figuring this out.
    I hope this information makes it a little more clear. The character swapping is not a problem, it works well in the editor. In fact if the characters are in different locations and you swap to them then you are taken to the scene where they can be found.
    What I am trying to achieve is have 3 different scenes and depending on the character they go to a different one.
    Let's say, I have a scene with all three characters in it. They decide to go to Rome and there's a button that transports them to the Rome scene. Here's the catch, if my active character is Tom and I press the Rome button then he goes to ancient Rome. If my active character is Dick and the button is pressed then he goes to present day Rome whereas Harry will go to futuristic Rome. So I've got the three different Rome scenes, I just need to establish which character is active and then send him to the correct scene.

    local scene = game:getLink(VGameCurrentScene)
    local character = game:getLink(VGameCurrentCharacter)
    
    if scene:getName() == "Roman Transporter" then 
      if character:getName() == "Tom" then
        game:setValue(VGameCurrentScene, getObject("Scenes[Ancient Rome]"))
      elseif character:getName() == "Dick" then
        game:setValue(VGameCurrentScene, getObject("Scenes[Present Day Rome]"))
      else
        game:setValue(VGameCurrentScene, getObject("Scenes[Futuristic Rome]"))
      end
    end
    


    The only problem is that the scene changes but the character does not appear. I'm not sure how to write the code that places the current character in the scene (I assume I need to set the character to the scene object which is the starting position).

    Newbie

    4 Posts

  • #8, by afrlmeFriday, 19. September 2014, 17:16 11 years ago
    Ah you didn't specify that you wanted the characters to travel somewhere. I assumed you was talking about switching active character & scene to that of the currently active character like in Day of the Tentacles for example.

    Would you be willing to post some screenshots? or send me them via pm or email? (alternatingfrequencies at hotmail dot com)

    P.S: here's a couple of tips in regards to naming conventions...
    1. use lowercase (always) - saves time & makes it easier to remember names.
    2. no spaces between names; especially if you plan on accessing them via lua script. use_underscores_between_words_like_so
    3. keep prefixes short & simple. a_rome, p_rome, f_rome etc...

    local scn = game:getLink(VGameCurrentScene)
    local char = game:getLink(VGameCurrentCharacter):getName()
    
    local t = { tom = "a_", dick = "p_", harry = "f_" }
    
    if scn:getName() == "Roman Transporter" then 
     game:setValue(VGameCurrentScene, getObject("Scenes[" .. t[char] .."Rome]")) -- would return a_Rome, p_Rome, or f_Rome...
    end
    

    ..don't know if it's correct as I've not tested (off top of my head again). Long story short: I've created a table which contains the character names as key & the prefix as the value. If we run this query & the conditions are met then it selects the correct prefix value from the table without having to query if current character is x or y or z etc.

    Imperator

    7285 Posts