Array and plugin functions

Discuss questions about plugin development with the Hollywood SDK here
Post Reply
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Array and plugin functions

Post 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
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Array and plugin functions

Post 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.
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: Array and plugin functions

Post 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
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Array and plugin functions

Post 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.
Post Reply