Debugging my code

I get this error:


[lua\includes\util.lua:178] attempt to call method 'IsValid' (a nil value)(Hook: EntityTakeDamage)

I already checked every EntityTakeDamage hook for an IsValid that might be causing it, but all I could find was this:
[lua]hook.Add(“EntityTakeDamage”, “ScaleDamageToTerritoryDamage”, function(ply, inflictor, attacker, amount, dmginfo)
if( ply and IsValid(ply) and ply:IsPlayer() and ply:Alive() )then[/lua]

And I can’t find where it’s coming from… Is there a way to find out fast, I’ve got over 100 parts checking IsValid(smthing) :confused:

Thanks.

I believe its ply:IsValid() and not IsValid(ply)

(correct me if im wrong)

IsValid does exactly the same, but checks if the given value is nil first. (Making me checking if ply redundant)

ValidEntity(ply) instead of if ply and IsValid(ply)…

[lua]
local func = IsValid

function IsValid(e)
local s,e = pcall(func,e)
if !s then
ErrorNoHalt(e)
debug.Trace()
end
return e
end
[/lua]

[editline]11th January 2011[/editline]

No:rolleyes:

At first I thought this could work, then I tried it and found that everything broke. :stuck_out_tongue:

ValidEntity() is just a redundant function. All it does is call IsValid(). IsValid() is the best/fastest way to check, unless you need to make your own custom checking.

Here is how ValidEntity() is defined:
[LUA]
/---------------------------------------------------------
Returns true if object is valid (is not nil and IsValid)
---------------------------------------------------------
/
function IsValid( object )

if (!object) then return false end
return object:IsValid()	

end

/---------------------------------------------------------
Returns true if entity is valid
---------------------------------------------------------
/
ValidEntity = IsValid
[/LUA]

Oops, forgot to return something when it didn’t error, try it now.

Oh sorry, I forgot to even read the OP. I have actually had this happen alot. I can’t remember how I fixed it, but it usually was something I did to mess up calling the function, like having my arguments of my function wrong. You might want to print your ply and print the class of ply just to see what your arguments are coming in as. It could be something that is calling the hook but sending the wrong arguments.

I´ll try it later, but I will rename the e variable, seeing how it´s used in the pcall too :stuck_out_tongue_winking_eye:

Actually lua’s syntax allows that:kiddo:
themoreyouknow.jpg

Whenever you see something called from a lua file that’s not yours, if you can’t find it in your directory.

Get this program called GCFScape. And open the garrysmod.gcf file, and find that file. And look at the line it says. Try taking out ply and IsValid(ply), just put IsValid(ply).

And -TB-, ValidEntity is not redundant, nor does it call IsValid, instead it IS IsValid. When they put a function to equal another function, it literally equals it, not calls it.

You can use Foszor’s Lua dump and/or Overv’s Lua dump to view Lua files that are located inside the GCF. It is faster and much easier.

Having two functions that do the same thing is redundant, why don’t we just give all the global functions several different names that way we get things like this.

There’s no point in ValidEntity() existing unless ValidEntity() was an old function that did something different than IsValid() and is now just kept for compatability.

It doesn’t hurt to have both of them. I use ValidEntity because I’ve just always used it I don’t know.