Accessing entity methods?

I’m having a bit of a problem that I just can’t find a solution for.



#define _RETAIL
#define GAME_DLL
#define WIN32_LEAN_AND_MEAN

#include <GMLuaModule.h>
#include <cbase.h>

ILuaInterface *gLua = NULL;

GMOD_MODULE(Start, Close)
LUA_FUNCTION(EntityTest)
{
	if(gLua->GetType(1) != GLua::TYPE_ENTITY) {gLua->TypeError("Entity",1);}
	edict_t *pEdict = (edict_t *)gLua->GetUserData(1);
	if(!pEdict || pEdict->IsFree()) {return 0;}
	IServerUnknown *pUnkn = pEdict->GetUnknown();
	if(!pUnkn) {return 0;}
	CBaseEntity *pEnt = pUnkn->GetBaseEntity();
	if(!pEnt) {return 0;}
	float hp = pEnt->GetHealth(); // Crashes the game
	gLua->Push(hp);
	return 1;
}
[...]


Well, as you can see I’m trying to access an entity passed to the function, but calling any function of the entity whatsoever crashes the game.
I’ve tried different approaches, but nothing has worked so far.
Perhaps I’m missing something, I’d be grateful for any ideas.

Have you tried casting the Lua userdata to CBaseEntity (or C_BaseEntity, depending if you’re on the client or server)? I’m almost sure that’s what’s wrong.

Strange. I thought I tried that before, but appearantly not. It still doesn’t work as it should however.
I get an incorrent value for the health and when trying to call some other functions it still crashes.

The CBaseEntity class in the public SDK is outdated, so most of the virtual function offsets are wrong and won’t work.

Isn’t it because Garry added some functions for Lua on the most basic entity interface or something? I noticed something like that.

I was afraid it would be something like that.
I’m guessing there are no proper headers for garry’s mod then?

The SDK headers are about 5 years out of date and a lot of class structures have changed quite a bit over that time.

I haven’t seen any functions like that, but it could be possible too.

You could try one of these.