Goregonzola Posted March 21, 2021 Share Posted March 21, 2021 (edited) Hey there! I'm thinking about a perk to my custom survivor: The final goal would be that every third time he eats something, it would restore some extra Hunger to him. This is quite a big hurdle I set to myself (I have to get better in coding somehow), I need some kind of reference, to pull this out. The only similar concept that comes to my mind is Warly's memory perk with eating. Could/should I use that as a reference? Do you know something else that resembles this idea more? I don't expect a polished coding from you, I would be absolutely fulfilled with a pointing to the right direction (, though, if there's a coding bit on the tip of your tounge, I wouldn't refuse it either). Thanks in advance! Have a great day! Edited March 21, 2021 by C_Thun Link to comment Share on other sites More sharing options...
QuartzBeam Posted March 21, 2021 Share Posted March 21, 2021 (edited) If I understand correctly, you can use the OnEat function and introduce a simple integer value. Anything that can eat has the "eater" component and that component has an OnEat function you can set. So you wanna make the OnEat function be something like: local function MyCharacterOnEat(inst) if inst.food_counter ~= nil then inst.food_counter = 0 --this creates and initializes the food_counter variable if there isn't one already end inst.food_counter = inst.food_counter + 1 --we increase the counter by one cause we just ate (otherwise this function would not have been executed) if inst.food_counter == 3 then -- once we hit 3, we need to apply the bonus -- code that gives extra hunger; haven't looked into how this stuff works and you also haven't mentioned whether you want a flat bonus or a percentage-based one inst.food_counter = 0 --we reset the counter afterwards end end Edited March 21, 2021 by QuartzBeam 1 Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 (edited) @QuartzBeam Thanks for the help! Yep, I would like this perk to be a percentage-based one (namely a 150% one). I've checked Wurt's coding about his veggie bonus, it looks like this: Spoiler inst.components.foodaffinity:AddFoodtypeAffinity(FOODTYPE.VEGGIE, 1.33) I'm not sure if this could be used a bit edited for every foodtype. If I reconstruct it to this, would it ever work for me? Spoiler inst.components.foodaffinity:AddFoodAffinity(1.5) Also, is this counter strategy resets the hunger-buff to default after the counter goes back to zero? I've never used this approach before. Finally, a rookie question: This function needs a ListenForEvent as well, right? Spoiler inst:ListenForEvent("oneat", MyCharacterOnEat) EDIT: I've tried the coding above, but it crashed with this error: Spoiler [string "../mods/devourer/scripts/prefabs/devourer.l..."]:43: attempt to perform arithmetic on field 'food_counter' (a nil value) This is line 43: Spoiler inst.food_counter = inst.food_counter + 1 Edited March 21, 2021 by C_Thun Link to comment Share on other sites More sharing options...
Combustiblemon Posted March 21, 2021 Share Posted March 21, 2021 1. idk anything about foodaffinity, but if you use foodaffinity with upper codes, you will have effect on fourth food, because function(that adds foodaffinity) will called after you actually eat and affected by something. so you'd better use: local food = data.food local foodhunger = food.components.edible:GetHunger(inst)/2 inst.components.hunger:DoDelta(foodhunger) on that code, because it will add extra 50% hunger of original food to your character. 2. Counter does resets, if inst.food_counter == 3 then -- once we hit 3, we need to apply the bonus When this line is called(=means it is third food you eat), inst.food_counter = 0 --we reset the counter afterwards this line will reset food_counter to zero (@QuartzBeam kindly added remarks each line), and if you follow my lines wrote in upper section, it would work without problems because bonus hunger will 'added' when only food_counter is 3. But if you really want to use foodaffinity, you would want to make another line that removes foodaffinity on next eat so bonus will not work everytime you eat. 3. Instead of listenforevent, you can just add this line. inst.components.eater.oneatfn = MyCharacterOnEat 1 Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 @Combustiblemon Many thanks! I've puzzled together the codings you suggested, but I think I've messed up something with it's structure. The game crashes when my character consumes something. I attach my lua file and the servel_log: devourer.lua server_log.txt Link to comment Share on other sites More sharing options...
Combustiblemon Posted March 21, 2021 Share Posted March 21, 2021 You have to add "inst.food_counter = 0" in your character's common_postinit. Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 (edited) @Combustiblemon I feel sillier and sillier: I added "inst.food_counter = 0", but it seems like it always sets the counter to zero because the buff do not applies. Do I have to specify somehow the "inst.food_counter = 0" to only when the game loads? Edited March 21, 2021 by C_Thun Link to comment Share on other sites More sharing options...
Combustiblemon Posted March 21, 2021 Share Posted March 21, 2021 adding "inst.food_counter = 0" on local function onload should work then. Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 @Combustiblemon I've tried the coding with cooked meat and with carrots, but the buffs still do not apply. I'll do some further testings, but I don't really know what's messy. 1 Link to comment Share on other sites More sharing options...
Combustiblemon Posted March 21, 2021 Share Posted March 21, 2021 local function WelnyelOnEat(inst) This line, should be: local function WelnyelOnEat(inst, data) this one. if inst.food_counter ~= nil then inst.food_counter = 0 --this creates and initializes the food_counter variable if there isn't one already end Also this one keeps initializing if food_counter exists. I think that's typo. if inst.food_counter == nil then inst.food_counter = 0 --this creates and initializes the food_counter variable if there isn't one already end This will work. I didn't knew function does initialize counter, you can just delete "inst.food_counter = 0" in onload or common_postinit. 1 Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 @Combustiblemon It doesn't seem to surrender so easily. It crashes with this message: attempt to index local 'food' (a nil value) In this line: local foodhunger = food.components.edible:GetHunger(inst)/2 Link to comment Share on other sites More sharing options...
Goregonzola Posted March 21, 2021 Author Share Posted March 21, 2021 EDIT: I've made it work with a helping hand from another platform. This is the final version: Spoiler local function WelnyelOnEat(inst, data) if inst.food_counter == nil then inst.food_counter = 0 --this creates and initializes the food_counter variable if there isn't one already end inst.food_counter = inst.food_counter + 1 --we increase the counter by one cause we just ate (otherwise this function would not have been executed) if inst.food_counter == 2 then -- once we hit 3, we need to apply the bonus inst.components.eater:SetAbsorptionModifiers(1, 2, 1) end if inst.food_counter == 3 then -- once we hit 3, we need to apply the bonus inst.components.eater:SetAbsorptionModifiers(1, 1, 1) inst.food_counter = 0 --we reset the counter afterwards end end Thanks for your help @Combustiblemon and @QuartzBeam as well! Have a nice day everyone! Link to comment 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