BECOME PART OF THE COMMUNITY - Sign up here
  • Hi Hive Mind,

    I can see in the _G of the LUA API that there are some commands called, Getvar, Delvar and Setvar.

    What is the syntax to set / use these commands?

    i thought it could be Setvar("variable", value) but that doesn't work.

    thanks a lot for the help.

  • These functions requires you to pass a pointer to the variable collection in question, as first argument.

    e.g current user

    SetVar(UserVars(),'model','T-1000')

    e.g. userprofile "Default"

    SetVar(ShowData().UserProfiles['Default'].Variables, 'manufacturer','Cyberdyne')

    e.g. globalvars

    SetVar(GlobalVars(),'mission','World domination')


    remember that Lua is case sensitive, your example Setvar would not work no matter what arguments you try to pass.

  • Hi,

    thanks for your super answer Andreas, it helped me a lot making some steps.

    i was just wondering because of your answer if it is possible to use our local lua variables to set the global or user variables in the desk. I can't seem to escape the , (comma) that is in the Setvar command.

    example: string.format("SetVar(GlobalVars(),'%s','%s'", var1, var2)

    and var1 and var2 would be created in a table or array or other way inside the LUA plugin.

    thank you for your help so far.

  • Not sure I understand what you are trying to do, but maybe the plugin example below can get you going..

    return function()

    local mytable = {question = 'Not sure', answer = 42}

    local vars = GlobalVars()

    for key,value in pairs(mytable) do

    SetVar(vars, key, value)

    end

    Cmd('GetGlobalVar *')

    end

  • no

    commandline variables can contain strings, integers or floats

    there is no way to use a table in the commandline

    I didn't write my true need, I would like one variable readable on every lua pluggings, so I didn't understand about one variable not declared on "local" is automaticaly global.

    So

    PLUGIN1

    local function main()
    t={}
    table.insert(t,{ ["apple"]="green", ["orange"]="orange", ["banana"]="yellow" })
    table.insert(t,{ ["apple"]="vert", ["orange"]="zerzer", ["banana"]="zerzer" })

     Printf( t[1]["apple"] )--green

    end

    return main

    PLUGIN2 Works !

    local function main()

     Printf( t[1]["apple"] )--green

    end

    return main

  • I don't believe that LUA globals persist across restarts / moving your showfiles between consoles.

    You can look at Table Serialization as a way to convert a table to and from a string. I have used this to take the contents of a table and convert it to a GMA variable for persistence.

  • And this is the plugin to record to the grandma variable one array

    function tableToString(table)

    return "return"..serializeTable(table)

    end

    function stringToTable(str)

    local f = load(str)

    return f()

    end

    function serializeTable(val, name, skipnewlines, depth)

    skipnewlines = skipnewlines or false

    depth = depth or 0

    local tmp = string.rep(" ", depth)

    if name then

    if not string.match(name, '^[a-zA-z_][a-zA-Z0-9_]*$') then

    name = string.gsub(name, "'", "\\'")

    -- name = "['".. name .. "']"

    name= "[".. name .. "]"

    end

    tmp = tmp .. name .. " = "

    end

    if type(val) == "table" then

    tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")

    for k, v in pairs(val) do

    tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")

    end

    tmp = tmp .. string.rep(" ", depth) .. "}"

    elseif type(val) == "number" then

    tmp = tmp .. tostring(val)

    elseif type(val) == "string" then

    tmp = tmp .. string.format("%q", val)

    elseif type(val) == "boolean" then

    tmp = tmp .. (val and "true" or "false")

    else

    tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""

    end

    return tmp

    end

    function main()

    --[[ local vartable=tableToString(TabSong)

    Printf(TabSong[1].name)

    local tabsong2 = stringToTable(vartable)

    Printf(tabsong2[1].name) ]]

    SetVar(GlobalVars(), "TabSong", tableToString(TabSong))


    end

    return main

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!