Running a lua function in a hook, within a c++ model
19 replies, posted
This done in cpp.
[lua]hook.Add("PlayerInitialSpawn", "hookname", function(ply)
ply:metafunc(arg)
end);[/lua]
What?
He wants to add a hook via a module.
This is a shitty way to do it but I can't be arsed thinking through the ->Push method.
Lua()->RunString("hook.Add([[PlayerInitialSpawn]],[[hookname]],function(ply) ply:MetaFunc(arg) end )");
I can't compile right now. So I'll respond whenever I get home ;).
CombineGuru. How would i go and run that on a player with a certain steamid?
You create a global function and do all the stuff inside that.
[lua]function PlayerInitCPPHook(pl)
if pl:SteamID() == "STEAM_" then
end
end[/lua]
[code]ILuaObject *ha = gLua->GetGlobal("hook")->GetMember("Add");
gLua->Push(ha);
gLua->Push("PlayerInitialSpawn");
gLua->Push("hookname");
gLua->Push(gLua->GetGlobal("PlayerInitCPPHook"));
gLua->Call(3, 1);[/code]
[editline]7th January 2011[/editline]
I'm not 100% sure if you could hook it to a C++ function.
Yours would work too, right?
Mine would work, but it's a worse method.
[QUOTE=CombineGuru;27250694][php]
ILuaObject *ha = gLua->GetGlobal("hook")->GetMember("Add");
gLua->Push(ha);
gLua->Push("PlayerInitialSpawn");
gLua->Push("hookname");
gLua->Push(gLua->GetGlobal("func"));
gLua->Call(3, 1);
[/php][/QUOTE]
what's with the return value
g_Lua->RunString("hook.Add([[PlayerInitialSpawn]],[[hookname]],function(ply) if ply:SteamID() == [[STEAM_0:0:36262603]] then ply:function([[argument]]) end end);");
That won't work for some reason.
[editline]7th January 2011[/editline]
1>c:\users\freze\desktop\mysql.cpp(640): error C2039: 'RunString' : is not a member of 'ILuaInterface'
You're missing an include or a definition.
[code]#undef _UNICODE
#include "GMLuaModule.h"
GMOD_MODULE(Init , ShutDown);
LUA_FUNCTION(RunString)
{
Lua()->CheckType(1 , GLua::TYPE_STRING);
Lua()->RunString("" , "" , Lua()->GetString(1) , true , true);
return 0;
}
int Init(lua_State *L)
{
Lua()->SetGlobal("RunString" , RunString);
return 0;
}
int ShutDown( lua_State *L )
{
return 0;
}[/code]
That's my (awful) code to replicate the RunString function in the menu environment.