Displaying Text For an Object With Lua Script

  • #1, by anthonyirwin82Friday, 16. June 2017, 12:21 7 years ago
    Hi,

    I am new to visionaire and have been looking at trying to use lua to do some stuff but the documentation doesn't really show many usage examples.

    I have a scene with someone locked in a closest and want to have text constantly poping up.

    I also want to be able to disable/hide the text when the character speaks using a function before and after character speaks in the game.

    The commented lines are what I am trying to do with lua but my thought process may be wrong and the current documentation has not put be on the right track.

    Anyway below is what I have created and trying to do. Any usage examples or pointers to get me on the right track would be greatly appreciated.

    Thanks,
    Anthony
    -- List of stuff to say over and over again
    local closetSpeech = {
      "Help I'm locked in the closet",
      "Please let me out",
      "You can't do this to me",  
      "Why won't anyone help me?",
      "When I get out of hear you'll be sorry"
    }
    
    
    
    -- always loop and say this stuff.
    while (true) do
      for i = 1, #closetSpeech, 1 do
        -- Trying to display object text using lua
        --Scenes["Office"].SceneObjects["Closet"].ActiveText.TextCurrentText = closetSpeech[i]
        --pause for x milliseconds or seconds 
        -- not sure how to pause with lua
      end
    end
    
    --[[
    
    -- want a function that will disable/hide or enable/show object text
    -- depending on if it is shown/hidden already.
    function ChangeClosetTextStatus()
      if Scenes["Office"].SceneObjects["Closet"].ActiveText.TextActive == true then
        Scenes["Office"].SceneObjects["Closet"].ActiveText.TextActive = false
      else
        Scenes["Office"].SceneObjects["Closet"].ActiveText.TextActive = true
      end
    end
    
    ]]

    Newbie

    20 Posts


  • #2, by SimonSFriday, 16. June 2017, 12:43 7 years ago
    Hi, 

    you currently can't do this with lua only.

    You need to save your text in a value and then show the text again with <vs=value>.

    Thread Captain

    1581 Posts

  • #3, by sebastianFriday, 16. June 2017, 14:09 7 years ago
    wouldn't the draw text function for lua help here? By that it isn't object text anymore so you may also can't use a sound file connected to it or make it language dependant (unless you start sound / show the text individually depending on the gamelanguage separately)...

    The easiest way though would be as SimonS mentioned:
    save the current text into a VS-ValueString and then start an action with Lua which outputs <vs=valuename>

    Thread Captain

    2346 Posts

  • #4, by anthonyirwin82Saturday, 17. June 2017, 02:49 7 years ago
    Thanks guys.

    I have tried the following, still trying to work out the proper way to use lua in visionaire:

    In the Office Scene --> Closet Object --> Values --> ClosetSpeechText I made the value of String = ClosetSpeechText

    In the Display object Text Action part I have the following text:

    Text from script is $<vs=ClosetSpeechText>

    In the Execute a script Action part I have the following:
    ChangeClosetText(Closet) -- closet is the Object Name

    In the Actual Script I have:
    -- Definition script
    
    registerHookFunction("setTextPosition", "ChangeClosetText")
    registerEventHandler("textStarted", "ChangeClosetText")
    
    function ChangeClosetText(text)
      text:setValue(ClosetSpeechText, "Please Let Me Out")
    end


    The Text displayed to the screen while running the game is:

    Text from script is $'?'


    I am just trying to work out how lua works with visionaire at the moment. Once I get how to use it I should be able to progress further with it.



    Thanks,
    Anthony

    Newbie

    20 Posts

  • #5, by SimonSSaturday, 17. June 2017, 13:21 7 years ago
    You can't change the text on textStarted, it's already fixed there. Change it before you start the text and reshow the text every time you change it.

    You need to create a value somewhere in the game, that is named ClosetSpeechText. This is then an object, which value will be read by the text <vs=ClosetSpeechText>. To change the text of the value, you need to do this:

    Values["ClosetSpeechText"].String = "Please Let Me Out"

    This looks up the Value ClosetSpeechText and sets the field ValueString to "Please Let Me Out".

    Thread Captain

    1581 Posts

  • #6, by anthonyirwin82Sunday, 18. June 2017, 10:50 7 years ago
    Thanks Simon

    I have tried to do what you suggested but I am not getting it to work.

    I am pretty sure I have done as you said.

    I have attached three screenshots as it is clearer then writing text here.

    I have an error shown in the messages log shown below and on one of the screenshots.

    18:46:06.101:Unknown data-field "string" for object (-1,-1) .
    18:46:06.101:Failed to run string in Lua:
    18:46:06.101:String content: Values["ClosetSpeechText"].string = "Please let me out"
    Print(ClosetSpeechText)

    I am not sure what is wrong.

    Anthony

    Newbie

    20 Posts

  • #7, by sebastianSunday, 18. June 2017, 11:47 7 years ago
    the string stuff seems to be correct by you. 
    Maybe the script is failing because of your "print":
    It tries to print a lua value "ClosetSpeechText", which doesn't exist (or does it elsewhere?)

    try either to save your VS-value saving in a lua variable or just print the whole declaration:
    print(Values["ClosetSpeechText"].String)
    also i am not sure if your Print needs to be lowercase... razz

    Another thing what seems a bit odd is that the console says

    object (-1,-1) 

    which is not a typical table id nor an object id... As far as i know the object table id is "6" and the specific objects in this table has to be >= 1 ... Maybe your object is defect (?)

    In VS5 you can rightclick your scene object and choose "copy id" which you can paste inside a text editor for example... should be something like (6,somenumber)

    Thread Captain

    2346 Posts

  • #8, by afrlmeSunday, 18. June 2017, 12:31 7 years ago
    the string stuff seems to be correct by you. 
    Maybe the script is failing because of your "print":
    It tries to print a lua value "ClosetSpeechText", which doesn't exist (or does it elsewhere?)

    try either to save your VS-value saving in a lua variable or just print the whole declaration:
    print(Values["ClosetSpeechText"].String)
    also i am not sure if your Print needs to be lowercase... razz

    Another thing what seems a bit odd is that the console says

    object (-1,-1) 

    which is not a typical table id nor an object id... As far as i know the object table id is "6" and the specific objects in this table has to be >= 1 ... Maybe your object is defect (?)

    In VS5 you can rightclick your scene object and choose "copy id" which you can paste inside a text editor for example... should be something like (6,somenumber)
    Yes it's likely the print() function messing up that script as print should be in lowercase & you are also calling something that doesn't exist. Have you actually created a value somewhere in the editor called ClosetSpeechText?

    Quick note: everything you create inside of the Visionaire Studio editor should be given a unique name - especially if you are planning on accessing things with Lua script later on as the engine will get confused if you try to access/update something & other instances of the same type of VisObj exist with the same name. If you don't want to use unique names for whatever reason, then you will have to direct link to whatever you are wanting to access/update...
    Scenes["example"].SceneValues["example"].String = "hello world"

    ... for example.

    Imperator

    7278 Posts

  • #9, by anthonyirwin82Sunday, 18. June 2017, 12:52 7 years ago
    Thanks Guys

    You were right it was a corrupted object. I deleted the object and recreated it and now it is working properly. Hopefully the corrupted object is a rare thing.

    Now I guess I need to read through the docs more to try find out how to restart the object text so I can have a loop and change the text randomly over time.

    I already found the setDelay command but not sure the command for resetting the object text so I can display new text on the object.

    Thanks Again
    Anthony

    Newbie

    20 Posts

  • #10, by sebastianSunday, 18. June 2017, 13:07 7 years ago
    objects can only have one speech text sonyou will overwrite it with every new display object text action. 

    Also you could just add a pause tag in the object text to let it disappear after x time (like normal display text). 

    so you only need to do something like:

    action -  at the begin of scene enter:
    call other action: closetloop

    action - closetloop:
    display object text <vs=ClosetSpeechText><p5000ms>
    pause xxx ms 
    jump. to action part #1

    Thread Captain

    2346 Posts

  • #11, by afrlmeSunday, 18. June 2017, 13:13 7 years ago
    simply creating a new object text action part linked to the same object will overwrite it.

    Imperator

    7278 Posts