Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Code patterns for a game that simulates systems in detail?

Discussion in 'Scripting' started by GeorgeCH, Oct 18, 2018.

  1. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Hi there -

    Has anyone come across/could recommend any code architecture examples for games that deal with simulating individual behaviors and systems at a very fine level (e.g., CK2, EU4 complexity?)

    For example:
    • John is an adventurer. Because his courage stat is sufficiently high, he decides to go an adventure. Since has enough gold, he goes to the local store and buys goods, equipment and food for the journey. He sets out and, after some time, encounters a monster. He defeats him, but not until he injures his arm, which makes it more difficult for him to swing his sword in future encounters. As the night falls, John decides to rest and eats some food to recuperate from the day's events. When he returns to his home town, he sells off the loot he got from the adventure and goes to see a doctor about that crippled arm.
    Now, that specific scenario can be easily coded, sure, no worries. But what if, later on, I also want to add John having a morale system and running away if things get too grim? Or John having stomach issues because the food he bought spoiled? Or John slowly dying from hunger because there wasn't enough food for the journey home? And, come to think about it, this shouldn't just be about John, but should apply to every living creature in the game?

    Sure, I could code all of the above and make use of inheritance to cascade the aforementioned logic. But that would result in the base "Creature" class slowly but surely becoming a monstrosity with thousands of lines of code keeping track of every conceivable variable under the sun - stamina, hunger, hitpoints, condition of the leg, eyesight, stomach poisoning, morale. That's just not sustainable in the long run.

    What I'm looking for are examples, if any, of a more "plug-and-play" solution - where new systems, such as hunger, morale, etc - can be added relatively painlessly and in a fairly decoupled manner (meaning, my health system shouldn't rely on the hunger system existing). The goal is to enable an "incremental depth" approach to developing the product, allowing me to flesh individual systems in more and more detail as the project progresses.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Usually, this incremental depth is implemented using an object-oriented approach. You first define a finite set of objects, and how they interrelate in principle (e.g. Physis, Morale, Reputation, Mental health) and build a first system consisting of these main blocks. Once it works, you start subclassing the main blocks to reflect finer control inside the blocks. This can be applied to characters in an adventure, but works (perhaps even better) in other simulations. I used this approach for a vehicle sim, where I first defined the vehicle as a system of modules - energy providers and -consumers, and defined the rules by which they interacted (in my case I was lucky to have providers and consumes descend from an abstract 'module' class because I found out that I also needed to be able to create modules that both consumed and produce resources - wich much better reflects reality: all modules consume some resources and produce other resources).

    Then I subclassed the modules into less abstract classes (e.g. power plants) and finally into instances that could be used in the game: different engine types (as energy providers), different lights and power trains as energy consumers etc. This works very well, as you can refine a class indiviually, and have guaranteed backward compatibility with older, less refined items.
     
    GeorgeCH likes this.
  3. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    This is incredibly helpful, thank you.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    What you're talking about is often referred to as a "Systematic Game" or "Systems driven game". So that's a great googling term.

    There's a very good talk here about how to build those, by a lead gameplay programmer at Ubisoft.

    Note that the "systems" term here isn't exactly same as systems in ECS, it's a much more abstract idea. That doesn't mean that something like ECS isn't ideal for this kind of game, just that the ideas are separate.
     
    GeorgeCH likes this.
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,335
    Unity started an official (It's actually Board to Bits Games) tutorial series on the subject (small bits over the course of several videos). I don't know if they've finished yet.



    Warning: Evidently if you watch one, single, solitary Unity-related video, you'll see the exact same Udemy ad (where the guy says "coding your own games is easier than you think") every time you watch a youtube video for the rest of your life.
     
    Last edited: Oct 18, 2018
    GeorgeCH likes this.
  6. GeorgeCH

    GeorgeCH

    Joined:
    Oct 5, 2016
    Posts:
    222
    Thank you for suggesting the term! I honestly wasn't being lazy, just one of those cases where answers are easier to come by when you know the question to ask.
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @kdgalla

    "Unity started an official tutorial series on the subject"

    Sorry if I understood you incorrectly, but at least the video you linked is by Ben from "Board to Bits Games" YouTube channel.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,335
    Oops! my mistake.