Original Post
Hi, I have a function with variable arguments that calls a Lua function. However, at the Lua end the arguments are wrong - it seems they are in the wrong order. If I do: I'm afraid I don't have the original article that showed how to write the function, but it should be pretty self explanatory. At this stage I'm trying to narrow it down to whether it's a C++ va_arg problem or a Lua problem.
CallFunction("FunctionName", "ssd", "Hello", "There", 12.3);
Lua sees it as:
FunctionName("Hello", "FunctionName", Garbage float)
Here is the function:
void SYM_SCRIPT::CallFunction(string Function, const char *ArgSignature, ...)
{
//Check Lua function exists
lua_pushstring(LuaState, Function.c_str());
lua_rawget(LuaState, LUA_GLOBALSINDEX);
bool Exists = lua_isfunction(LuaState, -1);
lua_pop(LuaState, 1);
if(Exists)
{
//Number of arguments
int NumArgs = 0;
va_list VAList;
va_start(VAList, ArgSignature);
//Get function
lua_getglobal(LuaState, Function.c_str());
//Push arguments
while (*ArgSignature)
{
switch (*ArgSignature++)
{
case 'd':
//Double argument
lua_pushnumber(LuaState, va_arg(VAList, double));
break;
case 'i':
//Int argument
lua_pushnumber(LuaState, va_arg(VAList, int));
break;
case 's':
//String argument
lua_pushstring(LuaState, va_arg(VAList, char *));
break;
default: break;
}
NumArgs++;
}
//Do the call
int Error = lua_pcall(LuaState, NumArgs, LUA_MULTRET, 0);
//Check for errors
LuaError(LuaState, Error);
va_end(VAList);
}
}