Overriding Default GMod LUA Functions


Not sure if its even possible but I was wondering if you can override a default GMod LUA function. Therefore returning what you want it to return, not what the function would normally return.

Specifically, I want to override E:GetPos() so rather than returning the pos of the entity (E) it returns a position that I have defined.

So yeah, not sure if it can even be done but if it can how would I go about doing it? Thanks.

function _R.Entity:GetPos()
return self:GetNWVector(“position”)

You can do it, yes - an example is the above post.

Should you do it? Absolutely not. Unless you are recalling the old function and essentially adding to it you really shouldn’t override functions.
The reason being is it will more than likely break any other addons that you have that use the function you overwrote.

Think of it this way - if you overwrite GetPos() to return the position you have stored some other way, every other addon will try to look for the position you stored, but chances are won’t be able to find it because it wont be defined for it.

You should just either use an entity variable, a NWVector, a DTVar, or something along those lines to store the variable and return it using those functions.

ENT.MyDefinedPos = Vector() --Empty vectors like this return 1,1,1
function ENT:Initialize()
–Your other init crap here
self.MyDefinedPos = Vector(Where,You,Want) --Of course change this to whatever vector you want)

function ENT:SomeOtherMetaFunction()
local effect = EffectData()
effect:SetOrigin(self.MyDefinedPos) --Just an example of how you can use it
util.Effect(“ManhackSparks”, effect)

_R.Entity.GtRealPos = _R.Entity.GtPos --Back up the old function as Entity:GetRealPosition()

function _R.Entity.GtPos()
return Vector(0,0,0) --Return what you want