Search Unity

Best practice - saving/loading with the help of Scriptable Objects

Discussion in 'Scripting' started by mrCharli3, Oct 19, 2019.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I am making a city builder.
    In my game I have Jobs, for example Farmer. Each building can contain a Job, which looks something like this:

    Code (CSharp):
    1. public class JobData
    2. {
    3.     public enum Type
    4.     {
    5.         NONE,
    6.         FARMER
    7.     }
    8.  
    9.     public Type type;
    10.     public int startHour;
    11.     public int endHour;
    12.     public Vector3 position;
    13. }
    The data in JobData above is all constant, except for position, which is assigned when I place the building, i.e the position of the Job.

    When I place a building with a Job, I automatically assign the JobData to a Villager:

    Code (CSharp):
    1. DataHelper.GetVillagerDataList()[i].job = job; //job is JobData
    So what is bugging me is that most of the data in the job would be perfect to store in a Scriptable object, but since I need to also store and save/load its position, that is not possible.

    What would be the best way to approach something like this? I guess in a perfect world I would be able to save an object that looks like:

    Code (CSharp):
    1. public class JobData
    2. {
    3.     public JobConstants jobConstants //Scriptable object
    4.     public Vector3 position
    5. }
    But AFAIK thats not possible, can someone smarter than me help me out with a clever way to manage this? because I can see this issue popping up for most my objects.
     
    Last edited: Oct 19, 2019