Lua Module Help

Not really too sure where to post this, but here goes:

I was wondering how to extract a table from a table inside a table.
The function i plan to use works like this inside lua:

AddClass(string, string, array{array{string, number, string, string}, array{string, number, string, string}, array{string, number, string, string}}, number…ect.

Anyway, i was wondering how i would extract that data from inside C++ (the data inside the arrays)

Any help is appreciated.

This is for Lua Scripting, you need to post on the Programming forum for max results.

Here is a little example I scrapped up.

[cpp]
//
// ReadFromLuaTableExample will loop through the table passed in the first argument, and print them
//

int ReadFromLuaTableExample( lua_State* state )
{
if( LUA->GetType( -1 ) != Type::TABLE )
{
LUA->Pop( 1 ); // Remove everything from the virtual stack
LUA->PushNumber( 88.93 ); // Push a nil value onto the stack
return 1; // Return the nil value on the stack
}

LUA->PushNil(); // Push a nil value onto the stack, used for lua_next- will be to new key

while( LUA->Next( -2 ) != 0 ) // Loop through the tables arguments
{
	if( LUA->GetType( -1 ) == Type::TABLE ) // If its another table, enable recursion
	{
		LUA->Push( -1 ); // We're going to push a refrence of the table, as ReadFromLuaTableExample will deallocate whatever is passed
		ReadFromLuaTableExample( state );
	}else if( LUA->GetType( -1 ) == Type::STRING ) { // If its a string, then print it,
		const char* Value = LUA->GetString( -1 );

		LUA->PushSpecial( GarrysMod::Lua::SPECIAL_GLOB ); // Print the value
			LUA->GetField( -1, "print" );
			LUA->PushString( Value );
			LUA->Call( 1, 0 );
		LUA->Pop();
	}

	LUA->Pop(); // Lets pop the value from the table
}

LUA->Pop(); // This will pop the key we passed off the stack

return 0; // We're not returning any arguments; We're returning 0 arguments

}
[/cpp]

You could then add that function into _G by doing

[cpp]
GMOD_MODULE_OPEN()
{
LUA->PushSpecial( GarrysMod::Lua::SPECIAL_GLOB );
LUA->PushString( “testie” );
LUA->PushCFunction( ReadFromLuaTableExample );
LUA->SetTable( -3 );

return 0;

}
[/cpp]

Which would hook _G[‘testie’] to ReadFromLuaTableExample

Then from lua you could do

[lua]
require( ‘testie’ );

local tab = {
“sup”,
“cool”,
{
“this is inside”,
{
“wow”,
“pressure”,
},
},
“phew, relief”,
};

testie( tab );
[/lua]

And it would print



sup
cool
this is inside
wow
pressure
phew, relief


I added alot of comments in the ReadFromLuaTableExample function to help demonstrate exactly what it does, step by step, but if you have any more specific questions, feel free to ask.

[editline]16th August 2012[/editline]

I added recursion to it- I misread your initial question.

Zack, you are retarded.

LUA_FUNCTION( ReadFromLuaTable );

Lua->SetGlobal( ReadFromLuaTable );

garry handles all stack management for you in the ILuaInterface class.

What if somebody doesn’t want to use the ILuaInterface class?

For Garry’s Mod 13

Also, just iterate the table yourself using Lua->GetAllTableMembers(). You can also do object type checking in C, so run a quick type check and iterate again.

[editline]17th August 2012[/editline]

Why wouldn’t they? You HAVE to use it at some point for your garrysmod module to even work. No point in writing more code then you have to

[editline]17th August 2012[/editline]

broken automerge.

I wrote the code for GMod 13, not for GMod 12-

There are tutorials on the wiki for creating modules for GMod 13, yet little documentation within garrysmod for how to do such in GMod 13.

Also, Lua->GetAllTableMembers() is obsolete in GMod 13-

If you’re going to act like a douche, can you think first?

Garry removed all of that in 13,

this is all we have now.

And if for whatever reason you still want to use ILuaInterface, I made backwards compatibility files that you can add to your project which will allow you to compile old code for the new headers.

http://blackawps-glua-modules.googlecode.com/svn/trunk/backwards_headers/

I just fixed a ton of stuff with it so it should be mostly stable. :v:

You should try the ILuaBase interface at least. It’s rather complicated but the knowledge you gain from using it can later be ported to the Lua C API because they’re very similar (Garry’s interface is just a bridge to the Lua C API).

I’m surprised garry hasn’t released the old files as it can really help people with specific questions.

Recreating the old interfaces was really just to help me learn how the new stuff works. Was never sure exactly how the stack worked in Lua, and now I do!

You should compile a lua engine, it really helped me learn how to manage the stack and work with lua states. After compiling a lua engine myself, now scripting in lua just clicks a bit more in my brain (if that makes sense).