Getting a randomly set Value

  • #1, by brut69Saturday, 22. June 2013, 10:40 7 years ago
    Hi,

    I set a Value to be random between 1 and 10 and I want my script to get that value and print it
    Value name 'sec'

    I did this

    ------------
    function start()

    local action= getObject("Values[sec]")

    if Values[sec]==1 then
    print('1')
    end
    if Values[sec]==2 then
    print('2')
    end
    if Values[sec]==3 then
    print('3')
    end
    if Values[sec]==4 then
    print('4')
    if Values[sec]==5 then
    print('5')
    end
    if Values[sec]==6 then
    print('6')
    end
    if Values[sec]==7 then
    print('7')
    end
    if Values[sec]==8 then
    print('8')
    end
    if Values[sec]==9 then
    print('9')
    end
    if Values[sec]==10 then
    print('10')
    end


    end

    ----------------

    For the life of me it doesn't work and I can't figure out why since this is so simple.
    Any clues?

    Great Poster

    263 Posts


  • #2, by brut69Saturday, 22. June 2013, 10:41 7 years ago
    ') = ' )

    Great Poster

    263 Posts

  • #3, by afrlmeSaturday, 22. June 2013, 13:19 7 years ago
    it randomly seems to parse smilies in code blocks too.

    -- let's set the initial value of sec variable
    local sec = 0

    -- let's create the function for checking random value of sec value
    function checkRandVal()
    sec = getObject("Values[sec]"):getInt(VValueInt)

    if sec == 1 then
    -- do some action!
    elseif sec == 2 then
    -- do some action!
    elseif sec == 3 then
    -- do some action!
    elseif sec == 4 then
    -- do some action!
    elseif sec == 5 then
    -- do some action!
    elseif sec == 6 then
    -- do some action!
    elseif sec == 7 then
    -- do some action!
    elseif sec == 8 then
    -- do some action!
    elseif sec == 9 then
    -- do some action!
    elseif sec == 10 then
    -- do some action!
    end
    print('value of sec =' .. sec)
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31


    you don't actually need to use a value in the editor...
    you could also create a variable with a random value.

    -- let's set the initial value of sec variable
    local sec = 0

    -- let's create the function for checking random value of sec value
    function checkRandVal()
    sec = math.random(1,10)

    if sec == 1 then
    -- do some action!
    elseif sec == 2 then
    -- do some action!
    elseif sec == 3 then
    -- do some action!
    elseif sec == 4 then
    -- do some action!
    elseif sec == 5 then
    -- do some action!
    elseif sec == 6 then
    -- do some action!
    elseif sec == 7 then
    -- do some action!
    elseif sec == 8 then
    -- do some action!
    elseif sec == 9 then
    -- do some action!
    elseif sec == 10 then
    -- do some action!
    end
    print('value of sec =' .. sec)
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31

    Imperator

    6842 Posts

  • #4, by brut69Saturday, 22. June 2013, 13:31 7 years ago
    Works perfect

    Thank you

    Great Poster

    263 Posts

  • #5, by afrlmeSaturday, 22. June 2013, 14:25 7 years ago
    welcome smile

    couple things to take note of...

    be careful when naming variables & functions: don't use names like start, end, action, run etc - in case they turn out to be actual Lua functions/commands already - use abbreviated versions instead.

    Also make sure you don't use same names for functions/variables multiple times - could create issues if you have more than one instance that isn't local to x script or function.

    using "elseif" is better than "else if" as you only need to add one "end" instead of one for each "if" used.

    final note: when using the short getObject method rather than full path method, you have to make sure you don't have multiple instances of whatever you are trying to retrieve.
    -- cleaner method: we are directly linking to the object condition/value or whatever
    getObject("Scenes[scene_name].SceneObjects[object_name].SceneConditions[condition_name]"):getBool(VConditionValue)
    -- or
    getObject("Scenes[scene_name].SceneConditions[condition_name]"):getBool(VConditionValue)
    -- etc...
    1
    2
    3
    4
    5
    6

    Imperator

    6842 Posts

Write post