jimmosio Posted January 12, 2015 Share Posted January 12, 2015 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 More sharing options...
goldfish911 Posted January 21, 2015 Share Posted January 21, 2015 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 endendFirst 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), falseThis 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.ownerif owner thenlocal hunger = owner.components.hungerif hunger thenlocal drainage = 50hunger:DoDelta(-drainage, false)endendend or local function OnTakeDamage(inst, damage_amount, absorbed, leftover)local owner = inst.components.inventoryitem.ownerif owner thenlocal hunger = owner.components.hungerif hunger thenhunger:DoDelta(-50, false)endendend 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 More sharing options...
Recommended Posts
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.