1. Post #1
    Gold Member
    Silverlan's Avatar
    October 2005
    735 Posts
    I'm having a bit of a problem that I just can't find a solution for.
    Code:
    #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.

  2. Post #2
    danielga's Avatar
    July 2007
    346 Posts
    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.

  3. Post #3
    Gold Member
    Silverlan's Avatar
    October 2005
    735 Posts
    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.

  4. Post #4
    IS AWESOME@@@@@@@@@@

    January 2009
    967 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Informative Informative x 1 (list)

  5. Post #5
    danielga's Avatar
    July 2007
    346 Posts
    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.

  6. Post #6
    Gold Member
    Silverlan's Avatar
    October 2005
    735 Posts
    The CBaseEntity class in the public SDK is outdated, so most of the virtual function offsets are wrong and won't work.
    I was afraid it would be something like that.
    I'm guessing there are no proper headers for garry's mod then?

  7. Post #7
    IS AWESOME@@@@@@@@@@

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

    Isn't it because Garry added some functions for Lua on the most basic entity interface or something? I noticed something like that.
    I haven't seen any functions like that, but it could be possible too.

    I was afraid it would be something like that.
    I'm guessing there are no proper headers for garry's mod then?
    http://hg.alliedmods.net/hl2sdks/

    You could try one of these.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Friendly Friendly x 1 (list)