Custom metatable functions affecting other addons

Hello all, I have defined a whole slew of new functions on the entity and the player metatables using the _R.Entity and _R.Player syntax in my custom addon, and they appear to be affecting other addons, such as the Wire Expression 2 editor and Player Appearance Customiser. Here is an example of some of those functions that have been defined as an example.

function _R.Entity:isRagdoll()
	if (self:GetClass() == "prop_ragdoll") then
		return true
		return false

function _R.Entity:isPhysProp()
	if (string.match(self:GetClass(), "^prop.*")
	&&	self:GetClass() != "prop_ragdoll") then
		return true
		return false

It is causing Player Appearance Customiser to sometimes spew the following error:
[pac\menu\property_setup.lua:1137] attempt to index field ‘modelbones’ (a nil value)

It is also causing the Wire Expression 2 to somehow malfunction.
I would not normally get those messages in the red bar at the bottom of the editor.

These problems do not occur when I relocate my addon folder to outside of the main addons directory and re-test with SRCDS. Does any body have an inkling as to what might be going wrong?

Sounds like you’re overwriting a function that the mods need in order to work correctly. Either narrow it down yourself to what file (or, if you can, what line of code in that file) is causing the problem, and post that, or post your entire code.

That could be it, is there a way of checking if functions are already defined?


if _R.Entity.FuncName ~= nil then print("oh noes") end

Thanks :smiley:

[editline]2nd September 2012[/editline]

What difference is there between using . or : syntax?

When defining functions, these two are exactly the same

local t = {}
function t:FuncName()

function t.FuncName( self )

Basically, Lua automatically creates a variable named “self” when you use “:”

When calling functions, these are the same:

t.FuncName( t )

When using “:” to call functions, Lua automatically uses the table (or object) itself as the first argument sent to the function.

“:” can only be used when defining or calling functions, and to get a function from a table without calling it, you must use “.” (and leave out the brackets “()”)

Ninja’d :suicide:

: Is sugar syntax.

[Lua]function MyTable.Function( MyTable , argument1, argument2 ) end [/Lua]
[Lua]function MyTable:Function( argument1, argument2 ) end [/Lua]

in the second function, you have access to the “self” variable. If you were to use the first syntax, and both the MyTable in which the “Function” function is in and the MyTable argument are the same, you could consider the argument the same as self.


[Lua]function _R.Player.Function( PlayerObject, argument ) end[/Lua]
Seeing as all Player Objects inherit the functions and variables from _R.Player, you could use the sugar syntax

[Lua]function _R.Player:Function( argument ) end[/lua]
in the function scope, you would have access to the “self” variable, which would be the same as the PlayerObject variable.

Once again thank you for clearing that up.

Solved. The problem was being caused by a number of gamemode hook functions returning values when they shouldn’t have been.