Running a lua function in a hook, within a c++ model

This done in cpp.

[lua]hook.Add(“PlayerInitialSpawn”, “hookname”, function(ply)


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 )”);

ILuaObject *ha = gLua->GetGlobal("hook")->GetMember("Add");
gLua->Call(3, 1);

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



ILuaObject *ha = gLua->GetGlobal("hook")->GetMember("Add");
gLua->Call(3, 1);

[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.

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.

#undef _UNICODE
#include "GMLuaModule.h"

GMOD_MODULE(Init , ShutDown);


	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;

That’s my (awful) code to replicate the RunString function in the menu environment.

Shouldn’t be defined somewhere already?

It should be - in the headers.

#include <windows.h>
#include <winsock2.h>

#include <stdlib.h>
#include <stdio.h>
#include “common/GMLuaModule.h”

Hm. I’ve no idea why that’s happening then. Best waiting for someone who knows more about what they’re saying than me to reply.

Okay then.

Thanks anyways bro ;-).

The version of ILuaInterface that you can download from the wiki doesn’t have a RunString IIRC.

I defined RunString myself - Works now…

If you want to do it the “proper” way in a module I’ve written some example code.