• ENT:Think Problem
    8 replies, posted
I currently have a ENT:Think function that repositions an entity when the owner dies, but when the owner dies the entity stays at the same spot. Here is the code: [CODE]function ENT:Think() local owner = self:GetOwner() if ( IsValid(owner) ) and owner:Alive() == false then for k,v in ipairs(ents.FindByClass("info_player_blue")) do if !IsValid(v) then continue end self:SetOwner(v) self:SetPos(v:GetPos() + Vector ( 40, 5, 0)) end end self:SetupPosition() return true end[/CODE]
First of all, why are you doing it inside Think hook? It's extremely unefficient, could be done in a non-meta function hooked to "PlayerDeath". Second of all: [code]if !IsValid(v) then continue end[/code] ???????????
[QUOTE=Netheous;44884182]First of all, why are you doing it inside Think hook? It's extremely unefficient, could be done in a non-meta function hooked to "PlayerDeath". Second of all: [code]if !IsValid(v) then continue end[/code] ???????????[/QUOTE] Maybe he means return end?
[QUOTE=Apple_Bloom;44884985]Maybe he means return end?[/QUOTE] But it's pointless because v will always be valid in that case.
[QUOTE=SixthSauce;44883833]I currently have a ENT:Think function that repositions an entity when the owner dies, but when the owner dies the entity stays at the same spot. Here is the code: [CODE]function ENT:Think() local owner = self:GetOwner() if ( IsValid(owner) ) and owner:Alive() == false then for k,v in ipairs(ents.FindByClass("info_player_blue")) do if !IsValid(v) then continue end self:SetOwner(v) self:SetPos(v:GetPos() + Vector ( 40, 5, 0)) end end self:SetupPosition() return true end[/CODE][/QUOTE] For you, PlayerDeath would be the right hook. So, when the player dies, it creates the entity and copies the bones; PlayerDeathThink is where you can limit respawning, and by using PlayerDeath you save yourself using a costly ents.Find* function in a Think hook... Here's a spectator system, using something like this will show you how to limit respawning if you're going for that: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/spectating_players_system.lua.html[/url] Look at corpse.lua from TTT; it'll show you how to copy the bones from the dead player to a ragdoll. You could freeze the ragdoll in place, or have it fall. Alternatively you could just spawn the entity there. Additionally; instead of using ents.Find* each time a death occurs, search once and keep the results cached. If you're not creating or removing them, this works great. Here's a select-spawn system which shows how to "cache" a point entity: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/playerselectspawn_system.lua.html[/url] [QUOTE=Apple_Bloom;44884985]Maybe he means return end?[/QUOTE] Why return within a search loop?
[QUOTE=Apple_Bloom;44884985]Maybe he means return end?[/QUOTE] If its not a valid entity then skip to the next iteration in the loop. That's what the continue statement does, it skips to the next iteration. If you put a return statement in there it would completely stop the loop and make it start over again, skipping over entities which could create bugs.
[QUOTE=brandonj4;44885616]If its not a valid entity then skip to the next iteration in the loop. That's what the continue statement does, it skips to the next iteration. If you put a return statement in there it would completely stop the loop and make it start over again, skipping over entities which could create bugs.[/QUOTE] Exactly; That is why I was asking why he wanted him to change it to return instead of leaving it as continue.
Okay, changed it to: [CODE]function GM:PlayerDeath( victim, inflictor, attacker ) entspawn = ents.FindByClass( 'info_player_blue' )[ 1 ] flagent = ents.FindByClass( 'ctf_flag' )[ 1 ] if ( victim == owner ) then flagent:SetOwner( entspawn ) flagent:SetPos( entspawn ) end end[/CODE] But still doesn't change the entity position or owner.
owner is undefined. You are supposed to set position to a vector, not an entity. [editline]24th May 2014[/editline] Also, localize your variables.
Sorry, you need to Log In to post a reply to this thread.