Page 1 of 1

Array and plugin functions

Posted: Sat Mar 16, 2019 2:27 pm
by msu
Hello!
Is it possible to pass an array to a plugin function?
Can a plugin function also return an array to Hollywood?

A simple C ++ example would be nice.

Greetings, Michael

Re: Array and plugin functions

Posted: Sat Mar 16, 2019 6:32 pm
by airsoftsoftwair
Sure, this is possible, e.g something like this.

Code: Select all

static int MyFunc(lua_State *L)
{
	int n, done;
	
	for(n = 0, done = 0; !done; n++) {	
		lua_rawgeti(L, 1, n);
		if(lua_isnil(L, -1)) done = 1; else printf("Value at index %d is %.14g\n", n, lua_tonumber(L, -1));	
		lua_pop(L, 1);
	}
	
	return 0;
}
MyFunc() expects a table containing only numbers (in linear order) in argument 1. It will print all those numbers to the console.

Re: Array and plugin functions

Posted: Sun Mar 17, 2019 9:09 am
by msu
How do I declare lua_isnil, lua_tonumber, lua_pop in the C++ Header?

They do not seem to belong to LuaBase.

It was easy with lua_rawgeti.
The following entry in the header worked:

Code: Select all

#define lua_rawgeti hwcl->LuaBase->lua_rawgeti

Re: Array and plugin functions

Posted: Sun Mar 17, 2019 11:29 am
by airsoftsoftwair
lua_tonumber() is part of LuaBase. The other two are macros and are defined like this in Lua 5.0.2:

Code: Select all

#define lua_isnil(L,n)          (lua_type(L,n) == LUA_TNIL)
#define lua_pop(L,n)            lua_settop(L, -(n)-1)
Be careful to use Lua 5.0.2 when importing macros because that's what Hollywood uses. Newer versions can be incompatible with 5.0.2.