• attempt to index field 'Entity' error help
    10 replies, posted
[b]Objective: [/b]I'm trying to destroy my sent when it has lived for 30 seconds but I don't get what is wrong with my code. [b]Error: [/b]Timer Error: entities/sent_raketti/init.lua:23: attempt to index field 'Entity' (a nil value) [b]Code: [/b] (I commented line 23) [lua]function ENT:Initialize() self.Entity:SetModel( "models/items/ar2_grenade.mdl" ) self.Entity:PhysicsInit( SOLID_VPHYSICS ) -- Make us work with physics, self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) -- after all, gmod is a physics self.Entity:SetSolid( SOLID_VPHYSICS ) local phys = self.Entity:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end self.Entity:SpawnTrail() timer.Simple(30, function() if (self.Entity:IsValid()) then --THIS IS LINE 23 local parent = self.Entity:GetParent() if ( parent:IsValid() ) then parent:Remove() end self.Entity:Explode(self.Entity) self.Entity:Remove() end end) end[/lua] [b]Notes: [/b] The code that's inside timer works fine in my PhysicsCollide function. I wanted to make sure it's removed after 30 seconds by making that timer but it's giving that error after 30 seconds.
Try saying: [code]if self.Entity and self.Entity:IsValid() then[/code] You can't call IsValid on an entity that doesn't exist sometimes (which is weird).
Thanks! That worked.
As a side-note, you can do ValidEntity(some_entity) which is functionally the same as writing Entoros' check. I'm not saying you [i]should[/i] use ValidEntity, I'm just putting another option forward.
And in scripted entities self is the same as self.Entity
[QUOTE=MegaJohnny;19441824]As a side-note, you can do ValidEntity(some_entity) which is functionally the same as writing Entoros' check. I'm not saying you [i]should[/i] use ValidEntity, I'm just putting another option forward.[/QUOTE] It's better to do ValidEntity == false with variables that change from nil to entities to other types of variables.
I'm bumping this because I have new, somewhat similar problem. [b]Objective: [/b]I'm trying to explode and remove my entity after 30 seconds. [b]Error: [/b]The entity doesn't explode after 30 seconds, it just gets removed. Same code works fine on my PhysicsCollide function and it actually explodes and removes itself. [b]Code: [/b] This works perfectly. [lua] function ENT:PhysicsCollide( data, physobj ) if (data.HitEntity:GetClass() == "sent_raketti") then return end local parent = self.Entity:GetParent() if ( parent:IsValid() ) then parent:Remove() end self.Entity:Explode(self.Entity) self.Entity:Remove() end [/lua] This works except it doesn't explode like it should, just gets removed :( (Part of my ENT:Initialize function) [lua] timer.Simple(30, function() if (self.Entity and self.Entity:IsValid()) then local parent = self.Entity:GetParent() if ( parent:IsValid() ) then parent:Remove() end self.Entity:Explode(self.Entity) self.Entity:Remove() end end) [/lua] My ENT:Explode function: [lua] function ENT:Explode( ent ) local splode = ents.Create("env_explosion") splode:SetPos( ent:GetPos() ) splode:SetOwner( self.Owner ) splode:Spawn() splode:SetKeyValue( "iMagnitude", "110" ) splode:Fire( "Explode", 0, 0 ) --splode:EmitSound( "weapon_AWP.Single", 400, 400 ) splode:Remove() end [/lua]
that happens probbably becuse you are doing splode:Remove(). the PhysicsCollide function seems to call before anything is drawn, timers are a bit late, use a timer to remove it
I think the problem may be with your timer code. I don't think you can put things like self.Entity and expect that to work. Try making the function have an argument, and pass the SENT as the argument via your timer.Simple. [lua] timer.Simple(30, function(xent) if (xent and xent:IsValid()) then local parent = xent:GetParent() if ( parent:IsValid() ) then parent:Remove() end xent:Explode(xent) xent:Remove() end end, self.Entity) [/lua] [editline]08:19PM[/editline] [QUOTE=Tobba;19449107]that happens probbably becuse you are doing splode:Remove(). the PhysicsCollide function seems to call before anything is drawn, timers are a bit late, use a timer to remove it[/QUOTE] Seems unlikely - he did say his ENT:Explode worked fine when it was called in PhysicsCollide.
Instead of creating an env_explosion, you should use [b][url=wiki.garrysmod.com/?title=Util.BlastDamage]Util.BlastDamage [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b].
[QUOTE=Tobba;19449107]that happens probbably becuse you are doing splode:Remove(). the PhysicsCollide function seems to call before anything is drawn, timers are a bit late, use a timer to remove it[/QUOTE] Thanks! That seemed to be the problem. It works now.
Sorry, you need to Log In to post a reply to this thread.