lua: string/value comparison query

  • #1, by andy-rinaldiWednesday, 19. November 2014, 16:05 6 years ago
    Hi all,
    I have the following function:
    function checkPrivateLeft()
    if getObject("Values[str_private_left]"):getStr(VValueString) == getObject("Values[RandomCode]"):getStr(VValueString) then
    getObject("Values[str_private_left]"):setValue(VValueString, getObject("Values[RandomCode]"):getStr(VValueString) .. "(OK)")
    getObject("Conditions[PrivateLeftCodeOK?]"):setValue(VConditionValue, true)
    else
    getObject("Values[str_private_left]"):setValue(VValueString, "WRONG CODE")
    end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9


    the if comparison is not working, in my variable str_private_left is printed Wrong code.
    The RandomCode is a random number.

    This code is working:

    function checkPrivateLeft()
    strRandomCode = "10041"
    if getObject("Values[str_private_left]"):getStr(VValueString) == strRandomCode then
    getObject("Values[str_private_left]"):setValue(VValueString, strRandomCode .. "(OK)")
    getObject("Conditions[PrivateLeftCodeOK?]"):setValue(VConditionValue, true)
    else
    getObject("Values[str_private_left]"):setValue(VValueString, "WRONG CODE")
    end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


    Thanks guys.

    Forum Fan

    160 Posts


  • #2, by SimonSWednesday, 19. November 2014, 16:16 6 years ago
    Hi, are you sure it is saved as a string ? If not you need to get the int value of the value.
    Values["RandomCode"].Int

    Thread Captain

    1333 Posts

  • #3, by afrlmeWednesday, 19. November 2014, 16:35 6 years ago
    hmm was there a question? or did you just post an edit with a working version?

    Only thing I will say is that you really should give the thread titles a more unique name. "lua question" is not very informative.

    "lua: string/value comparison query" is more informative.

    strRandomCode = math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9) -- global randomized 5 digit code on game start. (can access it via any script).

    -- print( strRandomCode) -- uncomment to find out what the code is.

    function checkPrivateLeft()
    if Values["str_private_left"].String == strRandomCode then
    Values["str_private_left"].String = strRandomCode .. " (OK)"
    Conditions["PrivateLeftCodeOK?"].ConditionValue = true
    else
    Values["str_private_left"].String = strRandomCode .. " (WRONG CODE)"
    end
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    ...untested.

    @ Simon: it's string because he want to add text to it. I believe he's using them to display messages via displayed text action part on a console screen or something.

    Imperator

    6812 Posts

  • #4, by andy-rinaldiWednesday, 19. November 2014, 19:48 6 years ago
    Sorry for the thread title...

    @ Simon: it's string because he want to add text to it. I believe he's using them to display messages via displayed text action part on a console screen or something.

    AFRLme is right.

    I use a variable value named RandomCode (see image 1) that use a random number from 10000 to 50000.
    I want to compare the RandomCode variable with the str_private_left string type variable (see image 2) in order to know if the player find the access code.

    @AFRLme
    I'll try your code and then I tell you. Thanks.

    Forum Fan

    160 Posts

  • #5, by afrlmeWednesday, 19. November 2014, 20:18 6 years ago
    Looks good. Did you animate the keypad button presses? I also see you opted to add extra 10ms to the displayed text pause time to prevent flickering. smile

    I don't really think you need the 2 display texts for a keypad display? I don't remember seeing blinking _underscores on keypad displays. It's more of a computer thing I think. I could be wrong though.

    I see one issue with generating random code the way you have with a random value between 10,000 & 50,000 it means the initial value will always be at least 1 to 5. My method concatenates 5 random values between 0 & 9 - or at least it should do.

    The rest of the script I wrote was shorthand version of what you wrote except I also added a space between currently typed words & status message.

    Imperator

    6812 Posts

  • #6, by andy-rinaldiWednesday, 19. November 2014, 22:17 6 years ago
    Looks good. Did you animate the keypad button presses? I also see you opted to add extra 10ms to the displayed text pause time to prevent flickering.

    No, I don't have animated the keypad button pressed...

    @AFRLme
    I can't use your strRandomCode variable because I need to use a variable in another display text in a scene (see image) or maybe I can use a variable declared in lua in a scene...
    In your script, you use == instead of = as assignment operator grin

    Values["str_private_left"].String == strRandomCode .. " (OK)"
    Values["str_private_left"].String == strRandomCode .. " (WRONG CODE)"
    1
    2
    3

    Forum Fan

    160 Posts

  • #7, by afrlmeWednesday, 19. November 2014, 22:46 6 years ago
    haha yeah my bad grin

    should have been = instead ==.

    This is what happens when I write code off the top of my head & without writing it in sublime text. There's no color syntax here or debugging or anything & I tend to type fast without checking what I'm writing too much when replying on the forum. (excuses, excuses) razz

    edit: fixed my code above. You could create a function which allows you to randomly generate a code & add it to a value whenever it is called it will overwrite the value with a new randomly generated code.

    function (definition)
    function setRndCode(val, amt)
    Values[val].String = ""
    for i = 1, amt do Values[val].String = Values[val].String .. math.random(0,9) end
    end
    1
    2
    3
    4
    5

    execute example
    setRndCode("v_rand_code", 5) -- create 5 digit random combination inside of value "v_rand_code"
    1
    2

    ...iterates loop by amt value & creates random combination inside of the linked value. Only needs to be called once as generated combination will be stored inside of value which will be stored inside of save games. So ideally this should only be triggered on "new game".

    Imperator

    6812 Posts

  • #8, by andy-rinaldiWednesday, 19. November 2014, 22:52 6 years ago
    How can I use in a scene a variable declared in lua? shock
    Thanks.

    Forum Fan

    160 Posts

  • #9, by afrlmeWednesday, 19. November 2014, 22:58 6 years ago
    what do you mean? & use it for what? I need more specifics.

    Imperator

    6812 Posts

  • #10, by andy-rinaldiThursday, 20. November 2014, 10:29 6 years ago
    I create a variable with lua in a definition script and then assign a value:
    local strRandomCode
    strRandomCode = math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9)
    1
    2
    3


    Now I want to show the variable strRandomCode in my display text.
    Also I want insert the variable in a if statement in an action tab.... for example. How can do it?
    I don't know how to use the code in the Visionaire GUI.

    My main problem in Visionaire is to show the variables created in lua script into graphic interface.
    You know what I mean?
    Thanks.

    Forum Fan

    160 Posts

  • #11, by afrlmeThursday, 20. November 2014, 13:14 6 years ago
    well you would have to assign the generated number to the Int part of a value. So you can technically use the same value.

    I've taken the code block you just posted & I've shown 2 examples below it in which I'm setting the integer & string values of a vs value. Obviously you have to change "value_name".
    local strRandomCode = math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9) .. math.random(0,9)
    Values["value_name"].Int = strRandomCode -- set integer value of "value_name" to strRandomCode value
    Values["value_name"].String = strRandomCode -- set string value of "value_name" to strRandomCode value
    1
    2
    3
    4

    ...now you can use the Int value in a query in vs. String values are not available as queries with action parts.

    Does it make any more sense now?

    Imperator

    6812 Posts

Write post