• Logging damage done to an NPC
    4 replies, posted
Hi, I need some help figuring this out. I've been searching facepunch and have come up with this [CODE]local function EntityTakeDamage( ent, info ) local pl = info:GetAttacker( ) if ent:IsNPC( ) and pl:IsPlayer( ) then ent.DamageLog = ent.DamageLog or { } ent.DamageLog[ pl ] = ( ent.DamageLog[ pl ] or 0 ) + info:GetDamage( ) ) end end hook.Add( "EntityTakeDamage", "Log NPC Damage", EntityTakeDamage )[/CODE] I am at a loss with this code, I haven't had much experience with tables so I am not sure how to sort or access the damage logged. Also my goal right now is to print damage done to an NPC either in console, or in my chat for debugging. Any help to achieve this would be greatly appreciated!
This works and may make more sense: [CODE] local damageTable = {} local function TakeDamage( ent, info ) local attacker = info:GetAttacker() if ent:IsNPC() and attacker:IsPlayer() then damageTable[ ent ] = info -- this adds a new key to the table with the info as the value end end [/CODE] What this does is whenever an entity is damaged, a new key in the table 'damageTable' is created. The key is the entity and the value is the damage info. If this part doesn't make sense, look for a guide on Lua tables, they're really simple once you understand them. If you want to access the damage of an entity, you just need to do damageTable[ some_entity ] and that will return any damage info recorded previously. If you want to print the damage done, just add a print statement: print( info:GetDamage() ) [editline]25th November 2016[/editline] Actually, here's a version that does what I think you're trying to do in the first place: [CODE] local function TakeDamage( ent, info ) local attacker = info:GetAttacker( ) if ent:IsNPC( ) and attacker:IsPlayer( ) then ent.DamageLog = ent.DamageLog or { } ent.DamageLog[ attacker ] = ent.DamageLog[ attacker ] or {} table.insert( ent.DamageLog[ attacker ], info:GetDamage( ) ) end end hook.Add( "EntityTakeDamage", "Log NPC Damage", TakeDamage ) [/CODE] [editline]25th November 2016[/editline] That makes it so when an entity takes damage, a new table called DamageLog is created on it if it isn't created already. Then, in that table, a new key for the attacker is made if it isn't already, which is then used to insert the damage amount into. If you want to print all the damage a player did to an entity, you just need to use a loop: [CODE] for k,v in pairs( SOME_ENTITY.DamageLog[ SOME_PLAYER ] ) do print( v ) end [/CODE]
Thanks for you reply! I made a module for it called sv_logs and put the code in there. I may be placing the k,v loop wrong but here's the code [CODE]local function TakeDamage( ent, info ) local attacker = info:GetAttacker( ) if ent:IsNPC( ) and attacker:IsPlayer( ) then ent.DamageLog = ent.DamageLog or { } ent.DamageLog[ attacker ] = ent.DamageLog[ attacker ] or {} table.insert( ent.DamageLog[ attacker ], info:GetDamage( ) ) for k,v in pairs( SOME_ENTITY.DamageLog[ SOME_PLAYER ] ) do print( v ) end end end hook.Add( "EntityTakeDamage", "Log NPC Damage", TakeDamage ) [/CODE] and here is the return, not sure if I am suppose to specify the entity/npc? The table should store it through getdamage [CODE]lua:8: attempt to index global 'SOME_ENTITY' (a nil value)[/CODE]
You're meant to set SOME_ENTITY and SOME_PLAYER to be a actual entities... I thought that would've been fairly obvious from the name. Anyway, I think this would work: [CODE] for k,v in pairs( ent.DamageLog[ attacker ] ) do print( v ) end [/CODE]
I apologize, i'm fairly stupid when it comes to lua, it's working now though! Thanks for your help and replies!
Sorry, you need to Log In to post a reply to this thread.