Jump to content

Recommended Posts

So, I've got a character fleshed out pretty well, but there is one thing I can't figure out.  How do I make a players' stats (hunger, health, sanity) increase when said character kills a creature.  In addition, is it possible to set a different stat increase for different creatures?

Likely. You'd have to establish a function that activates whenever a creature dies. Of course, first having it check to make sure that it died because of the player, and then it checks to see what the dead entity's name is, and then increase the health, hunger or sanity appropriately according to what gets returned.

  • Developer

You'll probably want to do something similar to this.

--in your component's constructorself.inst:ListenForEvent("entity_death", function(world, data) self:OnEntityDied(data) end, TheWorld)function YourComponent:OnEntityDied(data)	if (		   data.inst:HasTag("monster") 			or data.inst:HasTag("killer") 			or data.inst:HasTag("animal") 		)			and data.afflicter:HasTag("player") then		                if data.afflicter == self.inst then		        self.inst.components.health:DoDelta( 5 )                end	endend	

This is easy, I did this myself today:

local ZARKI_MIN_HUNGER = 150local ZARKI_MAX_HUNGER = 150local ZARKI_MIN_SANITY = 150local ZARKI_MAX_SANITY = 150local ZARKI_MIN_HEALTH = 125local ZARKI_MAX_HEALTH = 200local ZARKI_EXPERIENCE_GAIN_PERCENT = 0.10local ZARKI_MAX_EXPERIENCE = 75local function applyexperience(inst)    inst.level = math.min(inst.level, ZARKI_MAX_EXPERIENCE)    local hunger_percent = inst.components.hunger:GetPercent()    local health_percent = inst.components.health:GetPercent()    local sanity_percent = inst.components.sanity:GetPercent()--    inst.components.hunger.max = math.ceil(ZARKI_MIN_HUNGER + inst.level * (ZARKI_MAX_HUNGER - ZARKI_MIN_HUNGER) / ZARKI_MAX_EXPERIENCE)    inst.components.health:SetMaxHealth(math.ceil(ZARKI_MIN_HEALTH + inst.level * (ZARKI_MAX_HEALTH - ZARKI_MIN_HEALTH) / ZARKI_MAX_EXPERIENCE))--    inst.components.sanity.max = math.ceil(ZARKI_MIN_SANITY + inst.level * (ZARKI_MAX_SANITY - ZARKI_MIN_SANITY) / ZARKI_MAX_EXPERIENCE)    inst.components.hunger:SetPercent(hunger_percent)    inst.components.health:SetPercent(health_percent)    inst.components.sanity:SetPercent(sanity_percent)endlocal function onkill(inst, data)    if data.cause == inst.prefab        and not data.inst:HasTag("prey")        and not data.inst:HasTag("veggie")        and not data.inst:HasTag("structure") then        local delta = (data.inst.components.combat.defaultdamage) * 0.25        inst.components.sanity:DoDelta(delta)                local expdelta = (data.inst.components.combat.defaultdamage) * ZARKI_EXPERIENCE_GAIN_PERCENT        inst.level = inst.level + expdelta        applyexperience(inst)    endend

Its similar to the code of the robot guy :-)

Just look at his code, and see what is done there.

The exp gained depends on the attacking creature's attack damage.

Edited by Ridder Geel

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...