lua: string/value comparison query

  • #10, by afrlmeThursday, 20. November 2014, 13:14 10 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
    

    ...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

    7278 Posts


  • #11, by andy-rinaldiThursday, 20. November 2014, 15:50 10 years ago
    I solved the problem. It was a convert issue. The following code is working:

    function checkPrivateLeft()
    	intRandomCode = getObject("Values[RandomCode]"):getInt(VValueInt)
    	strRandomCode = tostring(intRandomCode) 
    	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
    



    Forum Fan

    160 Posts

  • #12, by afrlmeThursday, 20. November 2014, 19:02 10 years ago
    hmm I forgot about tostring & tonumber functions.

    Why bother creating the strRandom & intRandom variables at all? Or the longhand versions for getting/setting data structure tables?

    Are you using the latest build of Visionaire Studio? I forget.

    shorthand: vs 4.1)
    local rand -- empty variable
    
    function checkPrivateLeft()
     rand = tostring( Values["RandomCode"].Int )
     -- + --
     if Values["str_private_left"].String == rand then
      Values["str_private_left"].String = rand.." (OK)"
      Conditions["PrivateLeftCodeOK?"].ConditionValue = true
     else
      Values["str_private_left"].String = rand.." (WRONG CODE)"
     end
    end
    


    longhand (pre vs 4.1)
    local rand -- empty variable
    
    function checkPrivateLeft()
            rand = tostring( getObject("Values[RandomCode]"):getInt(VValueInt) )
            -- + --
    if getObject("Values[str_private_left]"):getStr(VValueString) == rand then
    getObject("Values[str_private_left]"):setValue(VValueString, rand .. " (OK)")
    getObject("Conditions[PrivateLeftCodeOK?]"):setValue(VConditionValue, true)
    else
    getObject("Values[str_private_left]"):setValue(VValueString, rand ..  " (WRONG CODE)")
    end
    end
    

    Imperator

    7278 Posts

  • #13, by andy-rinaldiThursday, 20. November 2014, 19:26 10 years ago
    I use 4.1 version.
    I always used the longhand... I did not know that the shorthand version was working (great!).
    Also I can write this code (without create more variables) and it works:
    function checkPrivateLeft()
    if Values["str_private_left"].String == tostring(Values["RandomCode"].Int) then
    Values["str_private_left"].String = tostring(Values["RandomCode"].Int).." (OK)"
    Conditions["PrivateLeftCodeOK?"].ConditionValue = true
    else
    Values["str_private_left"].String = " WRONG CODE"
    end
    end
    


    or

    function checkPrivateLeft()
    if Values["str_private_left"].String == tostring(Values["RandomCode"].Int) then
    Values["str_private_left"].String = Values["str_private_left"].String.." (OK)"
    Conditions["PrivateLeftCodeOK?"].ConditionValue = true
    else
    Values["str_private_left"].String = " WRONG CODE"
    end
    end
    

    Forum Fan

    160 Posts

  • #14, by afrlmeThursday, 20. November 2014, 20:09 10 years ago
    indeed. I meant to add the example you added in second version to wrong code bit, not the actual answer value.

    It's best to do it without using variables but variables do save time.

    local rand -- created outside the function, allows us to define rand anywhere else in the same script without having to create new variables each time.

    In regards to the shorthand code. A lot of things can be accessed with it, though not everything & some things as you've seen in this require different table names than the longhand versions.

    .String instead of VValueString & .Int instead of VValueInt etc.

    Here's a fun one though... say I wanted to get position of an object or character I can just use .Position instead of ObjectPosition or CharacterPosition but for animation position I still have to use AnimationCurrentPosition.

    Also with shorthand you can't do direct access.
    getObject("Scenes[scene_name].SceneObjects[object_name]") -- will not work with
    Scenes["scene_name"].SceneObjects["object_name"].

    Imperator

    7278 Posts