I want to make a "hunger shield" for a character. How can I do that?


jimmosio

Recommended Posts

Let's say I want my character to passively absorb an amount of damage taken (let's say 25%) and deal the absorbed damage to the hunger bar.


Here's an example:


A source attacks my character for 40 base damage.


My character loses 30 Health and 10 Hunger.


Since Night Armor has a similar effect I don't think it should be too hard, but I have no experience in LUA, so I'm asking for help.


Any ideas on how to code it?


Link to comment
Share on other sites

 

Let's say I want my character to passively absorb an amount of damage taken (let's say 25%) and deal the absorbed damage to the hunger bar.

Here's an example:

A source attacks my character for 40 base damage.

My character loses 30 Health and 10 Hunger.

Since Night Armor has a similar effect I don't think it should be too hard, but I have no experience in LUA, so I'm asking for help.

Any ideas on how to code it?

 

 

First of all, a really good strategy for doing things like this is looking at the game code such as the nightmare sword prefab.

 

But, I'll get to the point.

This is some code I modified from batbat.lua to drain hunger when damage is taken.

local function OnTakeDamage(inst, damage_amount, absorbed, leftover)    local owner = inst.components.inventoryitem.owner    if owner then        local hunger = owner.components.hunger        if hunger then            local drainage = damage_amount/10 * TUNING.HUNGER_DRAIN            hunger:DoDelta(-drainage, false)        end    endend

First of all, OnTakeDamage is a function called in any armor prefab when damage is taken.

 But just pasting this code in your custom armor prefab won't help.

you must call the function "OnTakeDamage" using a component like below:

 

 inst.components.armor.ontakedamage = OnTakeDamage

 

(this must be pasted in the prefab file for the item AFTER and BELOW the above function)

 

"local drainage" is a variable that takes the amount of damage you take in-game and runs it through a little equation:

damage_amount/10 * TUNING.HUNGER_DRAIN

 

This divides the damage you take by 10, then multiplies it by a TUNING variable in modmain.lua called "TUNING.HUNGER_DRAIN", which can be any number, but for this case, let's say 2.

 

Then, after this little equation runs, you see this:

hunger:DoDelta(-drainage), false

This takes the value the code just created for "drainage" and subtracts it from the player's hunger.

 

DoDelta can be used for other values besides hunger, like health and sanity. Just make sure to switch "hunger" in the code way at the top with "sanity" or "health" in all the places.

 

 If you just want to subtract a set amount of hunger for every hit that doesn't scale with damage, you only need

 

local function OnTakeDamage(inst, damage_amount, absorbed, leftover)

local owner = inst.components.inventoryitem.owner

if owner then

local hunger = owner.components.hunger

if hunger then

local drainage = 50

hunger:DoDelta(-drainage, false)

end

end

end

 

or

 

local function OnTakeDamage(inst, damage_amount, absorbed, leftover)

local owner = inst.components.inventoryitem.owner

if owner then

local hunger = owner.components.hunger

if hunger then

hunger:DoDelta(-50, false)

end

end

end

 

which will drain 50 hunger every time a hit is taken.

 

Phew! Hopefully this is some use, you need to delve into the coding guides on here and stuff yourself if you want to get somewhere, I already got scolded once for posting too many questions about code because I was too lazy to figure it out or read any of the awesome guides.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.