Jump to content

Recommended Posts

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 by Ultroman
  • Like 1

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)

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 by Ultroman
  • Like 1

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!

  • Like 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...