Fahakar Posted January 15, 2020 Share Posted January 15, 2020 (edited) Hi, is there a way to check what killed a creature? I'm making a character who uses traps and benefits from killing specifically with them but i can't progress in this part. Thanks for the attention! Edited January 15, 2020 by Fahakar Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/ Share on other sites More sharing options...
Ultroman Posted January 15, 2020 Share Posted January 15, 2020 (edited) Look at the combat component. Specifically, search for "PushEvent" and you should soon find the Attack (or DoAttack) function and the GetAttacked function. Study those. In GetAttacked it pushes the "killed" event on the attacker when the combatant (entity with the combat component having GetAttacked called on it) dies. It should get you the information you need, by simply listening to "killed" on your player. Edited January 15, 2020 by Ultroman 1 Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/#findComment-1299147 Share on other sites More sharing options...
Fahakar Posted January 16, 2020 Author Share Posted January 16, 2020 I got right the part to tell what my character killed, as he regains sanity when he kills mobs, but less than with traps( changed the idea around the character). I still can't get the information if it was a spear or a trap that reduced a creature hp to 0. Here is what i have( took references from wathgrithr.lua): local function onkilled(inst, data) local victim = data.victim local delta = 0 if IsValidVictim(victim) then if victim:HasTag("monster") then delta = 5 if victim:HasTag("smallcreature") then delta = delta + 5 else if victim:HasTag("largecreature") then delta = delta + 10 end end end end inst.components.sanity:DoDelta(delta) end And i listen for the the event "killed" in master_postinit. inst:ListenForEvent("killed",onkilled) Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/#findComment-1299443 Share on other sites More sharing options...
Ultroman Posted January 17, 2020 Share Posted January 17, 2020 (edited) For that information, you probably need to make the combatants tell you this, by extending the GetAttacked function of all combatants, in order to simply push a custom event with the information you need, if the combatant died. Put this at the bottom of your modmain.lua AddComponentPostInit("combat", function(comp) local oldGetAttacked = comp.GetAttacked comp.GetAttacked = function(self, attacker, damage, weapon, stimuli) local result = oldGetAttacked(self, attacker, damage, weapon, stimuli) -- If the entity no longer exists or its health component has registered it as dead, we push our custom event. if not self or not self.inst or not self.inst:IsValid() or not self.inst.components.health or self.inst.components.health:IsDead() then attacker:PushEvent("mycustomevent", { victim = self.inst, attacker = attacker, damage = damage, weapon = weapon, stimuli = stimuli }) end return result end end) Then you can instead listen to "mycustomevent" which gives you all the data you want. Edited January 18, 2020 by Ultroman 1 Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/#findComment-1299512 Share on other sites More sharing options...
Fahakar Posted January 17, 2020 Author Share Posted January 17, 2020 I got the solution! I used your logic of extending a function to change the OnExplode function in mine.lua for when the trap "explodes", it checks if the target died, and if it did, the character with the tag i created gains sanity. Thanks for the help! 1 Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/#findComment-1299672 Share on other sites More sharing options...
Ultroman Posted January 18, 2020 Share Posted January 18, 2020 You're welcome Great solution! I thought things also had to happen when something other than the trap killed the mob, so I went for a solution that would let you react to both Link to comment https://forums.kleientertainment.com/forums/topic/114986-get-cause-of-creature-death/#findComment-1299801 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now