• self is nil in ENT: function
    9 replies, posted
I'm trying to do a whole lot of vector calculations based on this entity's current position, and a set target position. But whenever I try to save its current position as a variable, I just get this error: [CODE][ERROR] gamemodes/scpgarrybreach/entities/entities/statuescp.lua:80: attempt to index local 'self' (a nil value)[/CODE] It is run in this function: [CODE]function ENT:StepForward( dir ) -- Format: dir [Vector] if dir == nil then dir = player.GetAll()[1]:GetPos() end local directionVec = dir:GetNormal() local curPos = self:GetPos() -- This is the line local distance = dir:Sub(curPos):Lenght2D() if distance > self.MaxMoveDistance then distance = self.MaxMoveDistance end print("directionVec is: "..tostring(directionVec.x)..", "..tostring(directionVec.y)..", "..tostring(directionVec.z)) print("distance is: "..distance) local targetpos = Vector(0,0,0) -- Placeholder if distance <= self.MaxMoveDistance then targetpos = dir else end end[/CODE] The function is not quite finished yet as you can see in the bottom, but ignore that. Right now I just can't tell why 'self' is nil D:
Who calls the function? Maybe you do something like ENT.StepForward(nil, dir)?
Just a note, you can just do 'if not dir' instead of 'if dir == nil'
This function is returning an error because it is being supplied with a nil value. Can we see where the function is being called from?
The function is called from shared.lua every set timer. So every 10 seconds this is called via a hook. I spawned it first using "scp = ents.Create("statuescp")" and a hook is then added which runs "scp:StepForward()". That hook is a custom one called ever time the timer fires. The dir is just for testing purposes, which is why if it isn't supplied that it is just player 1's position. Essentially, it is supposed to be run every synchronized blink which is basically just a global timer.
[QUOTE=Zet0r;45884390]The function is called from shared.lua every set timer. So every 10 seconds this is called via a hook. I spawned it first using "scp = ents.Create("statuescp")" and a hook is then added which runs "scp:StepForward()". That hook is a custom one called ever time the timer fires. The dir is just for testing purposes, which is why if it isn't supplied that it is just player 1's position. Essentially, it is supposed to be run every synchronized blink which is basically just a global timer.[/QUOTE] Can you show us post parts of the code? The creation of the entity, and the hook you spoke of?
-snip-
[CODE]function SpawnSCP(commander, command, args) scp = ents.Create("statuescp") scp:SetPos(commander:GetPos()) scp:Activate() scp:SetModel( "models/player/kleiner.mdl" ) hook.Add("PlayersBlinking", "SCP173move", function() print("NEVER") scp.StepForward( nil ) end) end concommand.Add("spawnscp", SpawnSCP)[/CODE] The hook is called every 10 seconds with a timer. No arguments other than that it was called: [CODE] blinking = false timer.Create("Blinking", 10 + 0.5, 0, function() blinking = true if SERVER then PrintMessage( HUD_PRINTTALK, "Blinking...") end -- Custom Hook to run every time players blink! hook.Call("PlayersBlinking") if CLIENT then fadenum = 0 timer.Create("BlinkFade", 0.0001, 4, function() fadenum = fadenum + (255/4) end) hook.Add("HUDPaint", "BlindPlayer", function() --print(fadenum) surface.SetDrawColor(0,0,0,fadenum) surface.DrawRect(0,0, ScrW(), ScrH() ) --chat.AddText("HEY") end) -- Fade out timer.Simple( 0.5 - 0.2, function() timer.Create("BlinkFadeOut", 0.0001, 4, function() fadenum = fadenum - (255/4) end) end) end timer.Simple( 0.5, function() blinking = false if CLIENT then hook.Remove("HUDPaint", "BlindPlayer") end if SERVER then PrintMessage( HUD_PRINTTALK, "Done blinking") end end) end)[/CODE]
[CODE] hook.Add("PlayersBlinking", "SCP173move", function() print("NEVER") scp:StepForward() end) [/CODE]
[QUOTE=_nonSENSE;45889558][CODE] hook.Add("PlayersBlinking", "SCP173move", function() print("NEVER") scp:StepForward() end) [/CODE][/QUOTE] omg xD I read that through multiple times and was like "what about it?", even launched the game and tried with removing nil. Only now I see that it was a dot and not colon! xD Thanks, I suppose it's solved :P
Sorry, you need to Log In to post a reply to this thread.