Jump to content

[Solved] Custom Survivor: Make every third food restore more Hunger


Recommended Posts

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 by C_Thun
Link to comment
Share on other sites

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 by QuartzBeam
  • Like 1
Link to comment
Share on other sites

@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 by C_Thun
Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

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
 Share

×
×
  • Create New...