Virtual Pet Game Template
  • VIRTUAL PET GAME TEMPLATE
  • Getting Started
    • 🎯 Project Setup Guide
    • 🧩 Home Scene Overview
    • 🗺️ UI Main Menu
    • 🗺️ LoadingManager
    • 🗺️ GameInstance
    • 🗺️ ToolsManager
    • 🗺️ AudioManager
    • 🗺️ LanguageManager
    • 🗺️ MonetizationManager
    • 🗺️ PlayerSave
  • Modifying the project
    • 🐾 How to Add a New Pet
    • 📜How Add a New Pet Rules
    • 💰 How to Add a New Currency
    • 🍎 How to Add a New Food Item
    • 🧢 Adding a New Accessory
    • 🎮 How to Add a New Minigame
  • Modifying Minigames
    • 🌤️ Minigame "Jump Sky"
    • 🍔 Minigame "Food Flicker"
    • 🐦 Minigame “Flappy”
    • 💎 Minigame “Gem Hunter”
Powered by GitBook
On this page
  • 📁 Creating a PetRules asset
  • 🧩 Assign it in GameInstance
  • ⚙️ How Stat Decay Works
  • 💬 Mood Evaluation
  • 😺 How PetModel Uses Mood
  • ⏳ Offline Decay Handling
  • 🧪 Example Stat Rule
  • 🧪 Example Mood Rule
  1. Modifying the project

📜How Add a New Pet Rules

Configuring Pet Rules (Stat Decay & Mood System)

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

  1. Right-click in the Project window → Create → PetCareGame → Rules → Pet Rules

  2. 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:

InitStatDecay();

The system:

  1. Checks how much time has passed since the last update.

  2. Applies changeAmount for each stat rule based on elapsed time (even offline).

  3. 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:

SetExpressions(currentMood);

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.


🧪 Example Stat Rule

statId: "Hygiene"
changeAmount: -3

🡆 Hygiene stat will decrease by 3 every interval.


🧪 Example Mood Rule

armoodId: "tired"
priority: 1
conditions:
  - statId: "Energy"
    threshold: 0.3
    invert: false
useAndLogic: true

🡆 The pet becomes tired when Energy < 30%

Previous🐾 How to Add a New PetNext💰 How to Add a New Currency

Last updated 28 days ago