The Pet Rules system is responsible for managing stat decay over time and determining the pet's current mood based on stat thresholds. This setup allows pets to reflect their emotional state (happy, sad, tired, etc.) and evolve their needs during gameplay—even while offline.
📁 Creating a PetRules asset
Right-click in the Project window → Create → PetCareGame → Rules → Pet Rules
Fill in the fields:
statUpdateInterval: How often stats update (e.g., every 60 seconds)
timeScaleType: Select Seconds, Minutes, or Hours
statDecay: List of statId and how much it increases or decreases over time (negative values = decay)
moodConditions: A list of mood rules based on stats and thresholds
Note: Only one PetRules asset is used in the game.
🧩 Assign it in GameInstance
You must assign your new PetRules to the petRules field in the GameInstance prefab.
GameInstance.Instance.petRules
This ensures the game will apply stat decay and evaluate mood using this configuration.
⚙️ How Stat Decay Works
When the game starts, PetModel initializes the stat decay logic:
The system:
Checks how much time has passed since the last update.
Applies changeAmount for each stat rule based on elapsed time (even offline).
Starts a coroutine that updates stats every X seconds based on statUpdateInterval.
Example of decay setup:
Stat ID
Change Amount
Meaning
"Hunger"
-1
Hunger decreases by 1 every interval
"Energy"
-2
Energy drops faster over time
💬 Mood Evaluation
Each mood in the rules has:
A moodId (e.g., "sad", "happy")
A priority (higher value = higher priority)
One or more conditions:
Stat ID (e.g., "Hunger")
Threshold (e.g., 0.2 means "less than 20%")
Invert (trigger when above instead of below threshold)
Logical type: AND or OR across conditions
The mood with the highest priority that meets its conditions is applied.
😺 How PetModel Uses Mood
When the pet is initialized or after a delay from mood expression, it calls:
And shows the appropriate facial expression from the expressions list by matching moodId.
Mood expressions are auto-handled:
On game start
After an animation or mood change
Based on current stat values
⏳ Offline Decay Handling
The system uses PlayerSave.GetLastTimestamp() to determine how many intervals passed since the game was last closed. All missed decay cycles are applied instantly when reopening the game, simulating real-time evolution.