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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question City Builder System/Prefab programming architecture

Discussion in 'General Discussion' started by aloxuhik, Feb 21, 2023.

  1. aloxuhik

    aloxuhik

    Joined:
    Dec 19, 2015
    Posts:
    3
    Hello,
    I am currently building City Building game, but I am sure about architecture of systems and prefabs.

    I started creating Building class, which would hold data about that building. Like power, size, price etc. But not every building needs all of that. Some will not use power, some will not produce etc. Which led me to component architecture.

    Is it good approach to have MonoBehaviour for each system that will only hold data and implement an IBuildable interface that will invoke method when building is built, so each MonoBeahviour can register itself in appropriate system?

    So for example PowerSystem will be singleton. When you build PowerPlant OnBuild will be called on Power component, which will tell something like PowerSystem.Instance.AddPower(5).
    architecture.drawio.png
    I know it will work in this simple example, but if I build whole game around this approach will there be some problems with performance or coding? I know singletons are spawns of hell, but I am not sure how to replace them here.

    Also ScriptableObjects should be used when using same data for multiple prefabs. So should I create SO for every building and component? Will it help enough to justify having 5 SO for every 100+ buildings type?

    Or if I am completely wrong can you point me in right direction?

    Thank you in advance
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    In a city builder you'll be dealing with optimization. If you place monobehaviors on each building, and several ones on top of that, you WILL run into performance problem, or your city will be tiny.

    In case of city builder game, it is possible, for example, to have a single mono behavior for the entire city. Everything else will be represented with usual C# classes, and when necessary the city will spawn geometry to r eflect its internal state in a visual way.

    Also, the way I would approach is to try would be t he way you rejected.
    A single class for all types of city building. "not every building has that parameter" means that parameter can be set to zero. You also don't gain anything from introducing interfaces, so there's no point.

    Also keep in mind, that, for example, cities skylines was popular due to ability to mod it. If your buildings rely on custom classes for every functionality, that reduces player ability to mod, and it is more convenient to have a single data type for every building type.

    Basically, I would recommend to take a look at KISS principle. You don't need to strictly use OOP, you don't need to strictly follow component architecture, but it is a good idea to use a simple or even the simplest solution that gets the job done. Right now you're moving away from simple solution and toward complicating things.
     
    aloxuhik likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    Unity's MonoBehaviour approach is excellent for small scales but once you start going beyond that you start having to apply all sorts of optimizations to make it scale and run well. Unity's DOTS framework on the other hand provides most of that performance and scalability right out of the box.
     
    Last edited: Feb 21, 2023
    aloxuhik likes this.
  4. aloxuhik

    aloxuhik

    Joined:
    Dec 19, 2015
    Posts:
    3
    Thats years of learning 1 script 1 functionality :D

    So If I understand correctly, best approach is to have few systems as MonoBehaviours and building will be gameobject with only MeshRenderer?

    So in broad strokes it will be something like this:
    BuildingSystem will get command to build Hospital. So it will create an instance of class Building into which it will save SO of Hospital, instantiate gameobject(or get it from pool) with corresponding model and save its reference. So every calculation will be done with List of Buildings in "bakcground" and gameobject with renderer will only sit there?

    Isn't DOTS still in development and not suitable for commercial projects?
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    DOTS consists of three components: ECS, Jobs, and Burst. Jobs and Burst were released some time ago and ECS is currently in what is basically a release candidate stage. Commercial games made with them have been coming out for a while now. V Rising for example was released May 2022.

    https://store.steampowered.com/app/1604030/V_Rising/
     
    Last edited: Feb 23, 2023
    aloxuhik likes this.
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Yes, but only if you want a huge number of objects.

    https://blog.unity.com/technology/1k-update-calls
     
    aloxuhik likes this.
  7. aloxuhik

    aloxuhik

    Joined:
    Dec 19, 2015
    Posts:
    3
    Great, thank you for pointing me in the right direction and for the article.


    Nice. The last time I tried DOTS, it was nowhere near for use. But I found out it was several years ago... Time surely flies :oops: Thank you for your answer!
     
  8. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,788
    V Rising team has written custom engines for longer than a decade. It's an example for sure, but is not really relevant to most people looking for help here. They are not using the default preview version of Entities.
     
    aloxuhik likes this.
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    Most people won't be trying to make SimCity either. ;)
     
    MadeFromPolygons likes this.