• Creating a metatable for a specific entity class
    5 replies, posted
Is it possible to have a metatable that inherits from the base entity and has specific functions for a class? Or do I just have to have an Entity parameter in my metatable?
I think you can do this by overriding __index or something. I was thinking about doing this for some methods that only work for specific classes.
[code]t = setmetatable( definition, { __index = entity meta table } )[/code]
[QUOTE=Kogitsune;49613558][code]t = setmetatable( definition, { __index = entity meta table } )[/code][/QUOTE] And would writing new functions to that metatable write it to the BaseEntity table as well, or just that specific object? I don't want to add the functions for ALL entities
It should default to definition - you'd have to set __newindex to the entity meta to cause that.
Thanks! [code]local SMOKE = {} local ENTITY = debug.getregistry().Entity -- Inherit base entity functions function SMOKE.__index(t, k) local val = rawget(t, k) or rawget(SMOKE, k) or ENTITY[k] return isfunction(val) and function(_, ...) return val(t.m_pEntity, ...) end or val end function Smoke() local pEntity = ents.Create("env_particlesmokegrenade") pEntity.m_flSpawnTime = CurTime() pEntity:Spawn() return setmetatable({m_pEntity = pEntity}, SMOKE) end[/code]
Sorry, you need to Log In to post a reply to this thread.