Download

Simple hunger/eating system

Hi.
This question is rather theoretical, because I’m trying to understand how to do stuff correctly, rather than writing actual code.
So, my question is:
I started a 3rd person C++ project and I wish to create a hunger zone.
I created my own player controller, and I created a class called Apple which has a property that determines how much hunger will be reduced when eaten. And of course, a hunger property on player character.
When I click on the apple, my click is detected by the Apple class.
Now, I wonder how I should go about it?
Should I create an Eat function on the player, and call it from the apple class?
or perhaps I shouldn’t even detect any clicks on the apple, but on the player, and then get the properties from the apple class, but do most of the logic on the player?

Any input on the issue will be much appreciated.

Create a base class called “Food”. This has all of the properties you’d want all of your food types to inherit.

If you have something unique to apples which no other food can do (such as “make_cider()”), you can make an apple class which derives from the food class.

You’d probably have a character which has a “food amount” value, which decreases over time. If the food amount reaches 0, the character maybe starts taking damage?
Eating food causes the character food amount to increase by the eaten food amount value. Maybe an apple is worth 5, but a loaf of bread is worth 20?

Only the character should know how to “eat” a piece of food. The apple doesn’t know how to eat, unless it becomes alive and animated, grows a mouth and learns how to eat. The character should not know how to eat an apple specifically, it should know how to eat food.

Here’s how I’d architect this out:



class AFood : public AActor
{
   int32 Calories;
   bool edible = true;
   //... more data ....
};

class AYourCharacter : public ACharacter
{
    int32 StoredCalories;
    int32 MaxCalories = 100;
    void EatFood(AFood* FoodItemToEat);
};

class AApple : AFood
{
   AFood* MakeIntoCider();
};


I also wouldn’t worry about the UI yet. Figure out how this works on the backend first. Create a test scenario where you call a function to eat a particular peice of food. once it works the way you want it, THEN you start building out your UI interactions. The danger is that if you build your UI first, and then figure out how the backend should work, you may find that the backend behaves differently from how the UI was designed, and you’d waste a bunch of time rebuilding the UI to meet the backend architecture.

Ok, I’ll try this.
Thanks!