I've been having some strange issues with a for loop- I'll go ahead and explain.
I've setup [B]ply.followers[/B] as a table each player owns. [B]ply.followers[/B] simply contains NPCs. I modified the GM:PlayerDeath hook so that when a player dies, a simple for loop is run to destroy each of the victim's followers.
Inside GM:PlayerDeath
[CODE] if vic.followers then
killfollowers( vic )
end[/CODE]
The killfollowers function.
[CODE]function killfollowers( ply )
for k, v in pairs( ply.followers ) do
print( k )
if v:IsNPC() then
v:TakeDamage( (v:GetMaxHealth() + 100), attacker, object )
v:Remove()
print( k, v:GetClass(), v:EntIndex() )
else
print( "Not NPC" )
end
end
end[/CODE]
As you can see, I've set it up for debugging- the script will first print the index of ANY value in the table, and then- if it's a NPC, it will print the table index, class, and Entindex
So before I kill myself via "kill" in console, I set up a lua_run in console to print all of the followers in my table. ( manhacks whenever I ran it ). As you can see, it prints all 3. Console output:
[CODE]1 NPC [225][npc_manhack]
2 NPC [239][npc_manhack]
3 NPC [248][npc_manhack][/CODE]
Everything seems to be in order. But now let's type kill in console. I die, and 2/3 of the manhacks explode. And thanks to our killfollowers "debug prints", we can see which two got destroyed:
[CODE]1
1 npc_manhack 225
2
2 npc_manhack 248[/CODE]
For some reason, it seemed to just skip 239 entirely. If I respawn and type "kill" again, I'll die and the last manhack will be destroyed. But why? Why not all at once?
[B]If it's of any help, this might be a clue: occasionally I'll have to type kill in console more than once to get the command to actually work, I'm unsure why this is.[/B]
At this point, I just don't see what's going wrong, code wise. Could there be some deeper factor I'm unaware of? Maybe something's being enacted that stops my for loop mid-value.
Help is, as always, appreciated.
Could you post the code where you add the manhacks to the ply.followers table? I'm not really sure of the problem at the moment
I have a feeling that it skips it because the table indices are changed when a previous manhack is killed.
For example in your loop:
1) Loop gets to the first entry and deletes it
2) An entity gets removed from the table on the first position and all the table keys/indexes are all shifted 1 up ( so every key has 1 subtracted from them)
3) Loop goes to the next entry #2, which is now the third entity instead of the first, and deletes it
4) Loop ends as it has reached the end of the table at 2 elements.
Try more manhacks and see more get left behind.
Fix? Use a while loop.
But he never removed the manhack from the table? He only removed the manhack ENTITY from the game?
[QUOTE=MPan1;50718967]But he never removed the manhack from the table? He only removed the manhack ENTITY from the game?[/QUOTE]
Yeah that's the thing, that's why I said I have a feeling this is the case, not that this is in fact the case.
[editline]16th July 2016[/editline]
proof of concept:
[code]local a = {"fuck","shit", "Tets"}
for k,v in pairs(a )do
print(k,v)
--v = nil
table.remove(a, k)
end [/code]
[editline]16th July 2016[/editline]
The thing is, the entities are supposed to be removed only by the next frame with ent.Remove(), I am not entirely sure why the elements are getting directly removed from the table, but I am sure there's gotta be an explanation.
Wait, wait, I do have a thing setup to where when an NPC dies, it gets removed from the table. Maybe this is causing it, I'll update.
There you have it. You now know the cause and a suggested fix.
Fk yeah. Shit works like a charm. Thanks, Robotboy, for making the inference.
[CODE]NPC [113][npc_manhack]
npc_manhack 113
NPC [116][npc_manhack]
npc_manhack 116
NPC [117][npc_manhack]
npc_manhack 117[/CODE]
Here's my updated killfollowers function that uses a while loop if anyone is interested.
[CODE]function killfollowers( ply )
if ply.followers[1] then
while ( IsValid( ply.followers[1] ) ) do
print( ply.followers[1] )
local follow1 = ply.followers[1]
if follow1:IsNPC() then
follow1:TakeDamage( (follow1:GetMaxHealth() + 100), attacker, object )
follow1:Remove()
print( follow1:GetClass(), follow1:EntIndex() )
else
print( "Not NPC" )
end
end
end
end[/CODE]
Sorry, you need to Log In to post a reply to this thread.