LuaScript Xojo and Real Studio Plugin

LuaScript.CallFunction Method (console safe)

Calls a function that has been previously loaded by the LoadFunction method.

CallFunction(
   args as Integer,
   results as Integer) as Boolean

Parameters

args
The number of parameters to send
results
The number of results that you expect back, or the LuaScript.LUA_MULTRET constant.

Returns

Boolean
True if calling the function was successful, else false. Use GetLastErrorMessage function to get the error message if the function returned false.

Remarks

Args is the number of arguments that you pushed onto the stack. All arguments and the function value are popped from the stack, and the function results are pushed. The number of results are adjusted to results, unless results is LuaScript.LUA_MULTRET. In that case, all results from the function.

Basically it means that in the CallFunction if specifying 1 then you expect exactly one return value, if you pass LuaScript.LUA_MULTRET then you don't know how many results you expect to get back. If you specify one and the function sends two back then you only get one.

if lua.LoadFunction("test") then
    lua.Push(5) // We want to send 5 down as parameter to the function so we push it to the stack.
   
    // Then we just call the function and we specify that we have one parameter and one return value.
    if lua.CallFunction(1,1) then
       MsgBox "The result is: " + Str(lua.GetInteger(-1,true))
    else
       MsgBox lua.GetLastErrorMessage()
    end if
else
    MsgBox "Could not load function called test"
end if

See Also

LuaScript Class