How to load lua files and call runstring directly from lua_State

Posts 1–12 of 12 · Page 1 of 1
How to load lua files and call runstring directly from lua_State
Loading lua files directly from ILuaInterface is shitty and has a lot of bullshit you don't need to deal with. There's a much better method.
Get your lua state however you normally would.

Code:
lua_State* state = g_Lua->GetLuaState();
Then make sure nothing is on the stack so you don't fuck anything up. Using loadstring and loadfile only puts the chunk on the stack, then you have to manually call it using call or pcall.

Note: pcall is extremely recommended. The difference is pcall is just protected call and will stop if there is an error.

Code:
if (!lua_gettop( state ))
{
 luaL_loadstring( state, "print(\"My message\")" );
 lua_pcall( state, 0, 0, 0 );

 luaL_loadfile( state, "file.lua" );
 lua_pcall( state, 0, 0, 0 );
}
It pops the chunk off the stack, so you don't need to worry with that.

Enjoy having tons of swag.
Hmm, whenever I tried this the lua state got destroyed and never got called. I'm probably being an idiot and doing it wrong, though.
i'm a c++ baby, so excuse my stupid questions

what headers do you need to compile this? how do you get it into the game? injection or require?
Quote Originally Posted by Azrius View Post
thanks, i'll assume that it's require then?
Sry, can't help with that. I tried making modules for gmod but I just can't get my head around all this "stack based development".
But this might help you getting round having to run a lua file with require http://www.mpgh.net/forum/713-garrys...s-require.html.
Quote Originally Posted by LennyPenny View Post
Sry, can't help with that. I tried making modules for gmod but I just can't get my head around all this "stack based development".
But this might help you getting round having to run a lua file with require http://www.mpgh.net/forum/713-garrys...s-require.html.
thanks for trying to help anyway, i'll look into it
Quote Originally Posted by Azrius View Post
thanks for trying to help anyway, i'll look into it
this is injection based not require based
(at least that's how i do it)
Quote Originally Posted by Azrius View Post
i'm a c++ baby, so excuse my stupid questions

what headers do you need to compile this? how do you get it into the game? injection or require?
Either one works, but I use injection. You need to use GetProcAddress for the lua functions.
Lenny is an idiot, get the SDK from AlliedModders and compile it with BlackAwps backwards compat headers.
thanks guys, i really appreciate it
Quote Originally Posted by Azrius View Post
thanks guys, i really appreciate it
No problem. I'm always willing to teach those that want to learn.
Posts 1–12 of 12 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us