• Think() not running every frame on entity
    11 replies, posted
I have an entity that I'm making where the hook "Think()" only runs like once every 20 or so frames. I have a piece of code that requires Think to run every frame for it to be smooth. I've confirmed that it isn't running at full speed by using print and checking the console. Why would that be? [code] function ENT:Think() SDist = (self.Entity:GetPos():Distance(plya:GetPos()))/3; if (SDist<333)and(SDist>0)then local args = SDist/333; local scale = args; print(scale) -- Change hull size, default 16 16 72, 36 when ducked local Sc = 16*scale plya:SetHull(Vector(-Sc,-Sc,0), Vector(Sc,Sc,72*scale)) plya:SetHullDuck(Vector(-Sc,-Sc,0), Vector(Sc,Sc,36*scale)) -- Change step size, default 18 plya:SetStepSize(18*scale) plya:SetFOV(90*scale) -- Change view offset, default 68, 32? when ducked plya:SetViewOffset(Vector(0,0,68*scale)) plya:SetViewOffsetDucked(Vector(0,0,32*scale)) plya:SetWalkSpeed(250*scale) plya:SetRunSpeed(500*scale) end [/code] This is my think function. The purpose is to make the player larger or smaller given the distance from my entity, but right now it is really choppy. Should I include this in the draw function or what?
[QUOTE=zalo;26473151]I have an entity that I'm making where the hook "Think()" only runs like once every 20 or so frames. I have a piece of code that requires Think to run every frame for it to be smooth. I've confirmed that it isn't running at full speed by using print and checking the console. Why would that be? [code] function ENT:Think() SDist = (self.Entity:GetPos():Distance(plya:GetPos()))/3; if (SDist<333)and(SDist>0)then local args = SDist/333; local scale = args; print(scale) -- Change hull size, default 16 16 72, 36 when ducked local Sc = 16*scale plya:SetHull(Vector(-Sc,-Sc,0), Vector(Sc,Sc,72*scale)) plya:SetHullDuck(Vector(-Sc,-Sc,0), Vector(Sc,Sc,36*scale)) -- Change step size, default 18 plya:SetStepSize(18*scale) plya:SetFOV(90*scale) -- Change view offset, default 68, 32? when ducked plya:SetViewOffset(Vector(0,0,68*scale)) plya:SetViewOffsetDucked(Vector(0,0,32*scale)) plya:SetWalkSpeed(250*scale) plya:SetRunSpeed(500*scale) end [/code] This is my think function. The purpose is to make the player larger or smaller given the distance from my entity, but right now it is really choppy. Should I include this in the draw function or what?[/QUOTE] Have you tried adding this to your think function? [lua] self:NextThink( CurTime() ) [/lua]
[QUOTE=Jcw87;26473930]Have you tried adding this to your think function? self:NextThink( CurTime() ) [/QUOTE] No dice. Runs at the same speed as before. EDIT: It turns out that there's no way to get think to run every frame because listen servers and stuff. And the fact that view functions are server-side. There will always be a delay. Why, Garry, must you do this to me in single player?
[QUOTE=zalo;26474380]No dice. Runs at the same speed as before. EDIT: It turns out that there's no way to get think to run every frame because listen servers and stuff. And the fact that view functions are server-side. There will always be a delay. Why, Garry, must you do this to me in single player?[/QUOTE] You could try doing the view related stuff in [b][url=wiki.garrysmod.com/?title=Gamemode.CalcView]Gamemode.CalcView [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b], and leave the rest in Think.
You need to return true after setting NextThink.
[QUOTE=maurits150;26474893]You need to return true after setting NextThink.[/QUOTE] How the hell is that going to help? Think hooks return nil.
And yet it works. Thanks maurits150.
[lua] function ENT:Think() Msg( "Entity thinks...\n" ) self:NextThink( CurTime() + 1 ) return true // Note: You need to return true to override the default next think time end [/lua] Taken from the wiki, you do need to return true.
I know this is already solved, but you could use the draw hook maybe? Thats drawn every frame atleast
It only gets called when the entity is visible.
CreateMove / SetupMove / Move hooks?
[QUOTE=Willox;26479475][lua] function ENT:Think() Msg( "Entity thinks...\n" ) self:NextThink( CurTime() + 1 ) return true // Note: You need to return true to override the default next think time end [/lua] Taken from the wiki, you do need to return true.[/QUOTE] Well that's just retarded design. It makes no logical sense. If you don't want to override the default next think time, just don't call the function. There shouldn't be a need to "confirm" that you want to change the next think time.
Sorry, you need to Log In to post a reply to this thread.