Sign in to follow this  
mexican geek

Help with a hunger mechanic

Recommended Posts

So my character is almost ready but i want to implement only one last mechanic in order to make it balanced.
My character is able to eat the elemental food type (like rocks), I want to make it so that if the character doesn't eat at least one item of the elemental food type its hunger rate doubles until he does, will yall help me figure out how that can be done?...
Because I have no idea
hehe

 

Thanks in advance

Share this post


Link to post
Share on other sites
Nelzanger    33

I don't know how your mod works but I'll try to give you a way that should work.

I think what you would need in that case is a custom event your character needs to listen to. When eating a rock, you should send an event that tells that your character has eaten a rock. When listening to that event, and receiving one, it would reset a timer to 0 and set the hunger rate to "normal". This timer would be updated in a task, and if the timer goes beyond the specified threshold, then you double the hunger drain rate.

  • Like 1

Share this post


Link to post
Share on other sites
On 9/20/2021 at 9:25 PM, Nelzanger said:

I don't know how your mod works but I'll try to give you a way that should work.

I think what you would need in that case is a custom event your character needs to listen to. When eating a rock, you should send an event that tells that your character has eaten a rock. When listening to that event, and receiving one, it would reset a timer to 0 and set the hunger rate to "normal". This timer would be updated in a task, and if the timer goes beyond the specified threshold, then you double the hunger drain rate.

What you're suggesting sounds logical how can I write that tho? code-wise

Share this post


Link to post
Share on other sites
Nelzanger    33

Well, I don't know how you have implemented your character, and how the eating system works for it, so all I can do is put bribes of code that would manage the timer thing, and the linked effects. You should post your code, or parts of it with context for people to help you better.

The following code has not been tested and will probably crash. You will need to adapt the elements to your own context. I strongly suggest that you look on the Antlion and Rock lobster files, since they both eat rocks : it could help you find how to listen on eating such objects.

-- Constants
local EATING_TASK_REFRESH = 1.0  -- Do the periodic task every x seconds
local EATING_THRESHOLD   = 10.0  -- If the character have not eaten up to x seconds, trigger effects
-- Adding a periodic task
myCharacter.eatingTask = myCharacter:DoPeriodicTask(EATING_TASK_REFRESH, CheckEatTask)
myCharacter.eatingTimer = 0
-- Checking and updating your eating timer. You also should avoid reapplying the effect at every evaluation so adding a boolean would be nice
local function CheckEatTask(inst)
    if (inst.eatingTimer < EATING_THRESHOLD) then
    	inst.eatingTimer = inst.eatingTimer + EATING_TASK_REFRESH -- Update your timer
    else
       inst.components.hunger:SetRate(your_default_hunger_rate * 2.0) -- Double the hunger rate
    end
end
-- Adding an event. You need to find yourself if "oneating" event exists in the game, else you will need to implement your own with PushEvent
myCharacter:ListenForEvent("oneating", OnElementAte, owner)
-- The function called when something has been eaten
local function OnElementAte(inst, data)
  
  if ( [YourCondition] ) then
    -- If your element that was eaten is a rock, reset the timer and apply the effect
    -- Be warned, I don't know what is inst or data here. I'm assuming inst is your character in that case, so you better check this by yourself
    inst.components.hunger:SetRate(your_default_hunger_rate) -- Set the hunger rate to default
    inst.eatingTimer = 0
  end
end

 

Edited by Nelzanger

Share this post


Link to post
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
Sign in to follow this