I have been looking around for a pretty long time, but haven't found an answer anywhere yet. How can I get an entity, which is passed as an argument to the function in Lua, from Lua C Api and, then, push it into the stack for the future use?
Use LUA->ReferenceCreate and LUA->ReferencePush. LUA->ReferenceCreate pops the value at the top of the stack and returns a number that "points" to it. To your program, it's just a number, but lua will take that number and know what you are pointing to.
[cpp]int entity = LUA->ReferenceCreate();
// Push it later on...
LUA->ReferencePush(entity);[/cpp]
You should use LUA->ReferenceFree(entity) when you are done with it. You can use this with any value, not just entities.
Thank you for an answer, will try it out.
[editline]16th July 2016[/editline]
I am probably doing something wrong, but it doesn't work for me.
Here is the part of the code:
[CODE]int Test(lua_State *state)
{
int ply = LUA->ReferenceCreate();
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "FindMetaTable");
LUA->PushString("Player");
LUA->Call(1, 1);
LUA->GetField(-1, "SendLua");
LUA->ReferencePush(ply);
LUA->PushString("print('sendlua working')");
LUA->Call(2, 0);
LUA->Pop(3);
LUA->ReferenceFree(ply);
return 0;
}
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->GetField(-1, "hook");
LUA->GetField(-1, "Add");
LUA->PushString("PlayerInitialSpawn");
LUA->PushString("testtest");
LUA->PushCFunction(Test);
LUA->Call(3, 0);
LUA->Pop(2);[/CODE]
You're doing some weird thing with that FindMetaTable call. You don't need that. You don't even need references. Your whole function should look something like this
[cpp]int Test(lua_State *state)
{
LUA->GetField(1, "SendLua");
LUA->Push(1);
LUA->PushString("print('sendlua working')");
LUA->Call(2, 0);
return 0;
}[/cpp]
Been a while since I've done anything with the c api, so that might not work. I'm not even sure if SendLua works in the PlayerInitialSpawn hook, tbh.
It worked, thank you.
Sorry, you need to Log In to post a reply to this thread.