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. Dismiss Notice

Question Scripting organizing (architecture?) help needed.

Discussion in 'Scripting' started by BuyAnElefant, May 27, 2021.

  1. BuyAnElefant

    BuyAnElefant

    Joined:
    Mar 9, 2021
    Posts:
    8
    Hello! I'm developing a tycoon game and struggling with its structure.
    For example i have a script Time that contains current in game time (not same as in real world), hours, days, AM\PM data, it is incrementing itself in coroutine.
    I assume it should be don't destroy on load cos if i change scene to minigame for example and next i'm back to a main scene, this data should persist and continue incrementing during minigame (while not visible to a player).

    So my UI objects (clock) and game state managers should have access to time data.

    Next i need to save its state in the moment when player quit game and be able to load it next time game will be started.

    Should Time be able to serialize itself (contain seriaize method) or should it be saved by another class?

    Should Time be singleton?

    What else am i missing here and what difficulties might appear here during further development?

    I have a bunch of similar classes as Money, players Motivation, progress of the development of a project player's company is being worked on etc.

    I guess those things should be organized similar way as Time class? Or am I missing here something?
     
  2. BuyAnElefant

    BuyAnElefant

    Joined:
    Mar 9, 2021
    Posts:
    8
    Any help, please? Still struggling with it
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The first thing you need is to create bags for your data.

    Identify the lifecycle of data that needs to persist through the entire game and put that all in one place.

    - transient, doesn't matter
    - length of game (score, time)
    - forever (total game wins, losses, etc.)

    Singletons or statics are fine solutions here, and this also gives you a perfect place to start a save game system.

    As with everything, you will NEVER stand up all the data and structures all at once. It will be an iterative process. For instance if you are wondering where to store the time, that's a great first item to put into this new bag of game-lifetime data.

    Get every part of resetting, using, loading, saving, changing, stopping, starting that time going, and do so in a way that makes only one tiny part of the code care about the details of it. Make a Time API to transact against everything related to this Time variable.

    Once all corners of the Time data are properly handled, move onto the next piece that needs to go in. Conveniently you will now know a lot more about what to consider.
     
    BuyAnElefant likes this.
  4. BuyAnElefant

    BuyAnElefant

    Joined:
    Mar 9, 2021
    Posts:
    8
    Thank you so much for your help!