Hello community, has anyone been able to save the date of Unistorm into ORK ? I'm pretty bad at scripting and don't know how to integrate it, if anyone could point me into the right direction ?
  • I'm utilizing ORK's ISaveData interface to save UniStorm data and load them backup when I load a saved game. This is a basic version of my script as I have some game specific code in there. I made this script to avoid adding code to the UniStorm code base. Made it a pain to add my custom code everytime UniStorm put out an update. :)

    Put this script on a gameobject in your starting scene and you should be good to go.
    Note: I have set the Time in UniStorm to static (I forget the actual checkbox name) so UniStorm no longer keeps track of the time. This script does that for me.


    using UnityEngine;
    using System.Collections;
    using ORKFramework;

    public class GameTime : MonoBehaviour,ISaveData
    {
    #region fields
    public int hour;
    public int minute;
    public int seconds;

    public int month;
    public int day;
    public int year;
    public System.DayOfWeek dayOfWeek = System.DayOfWeek.Sunday;

    public float timeSpeed = 0.0001f;
    public float time;


    UniStormWeatherSystem_C weather;
    bool weatherFound = false;

    #endregion

    #region inits
    // Use this for initialization
    void Awake()
    {
    ORK.SaveGame.RegisterCustomData("GameTime", this, false);
    DontDestroyOnLoad(this.gameObject);
    }
    void Start ()
    {
    //load these from a file
    hour = 8;
    minute = 0;
    seconds = 0;

    month = 1;
    day = 1;
    year = 368;
    }
    void OnLevelWasLoaded()
    {
    weatherFound = false;

    if(!ReferenceEquals(zoneInfo,null))
    {
    if(!zoneInfo.IsIndoors())
    {
    GameObject UniStorm = GameObject.Find("UniStormSystemEditor");
    if(!ReferenceEquals (UniStorm,null))
    {
    weather = UniStorm.GetComponent<UniStormWeatherSystem_C>();
    if(!ReferenceEquals(weather,null))
    {
    weatherFound = true;
    }
    else
    {
    weatherFound = false;
    }
    }
    }
    else
    {
    weatherFound = false;
    }
    }
    }
    #endregion

    #region update methods
    public void StartTimer()
    {
    InvokeRepeating("TimeTick", 0, 6); //fires 6 seconds giving us 72 minutes = 1 game day
    }
    // Update is called once per frame
    void TimeTick()
    {
    //seconds += 60;

    minute++;
    if(minute > 60)
    {
    minute = 0;
    hour++;
    if(hour > 24)
    {
    hour = 0;
    seconds = 0;
    day++;
    this.dayOfWeek += 1;
    if(day > 30)
    {
    day = 1;
    month++;
    UpdateZoneResets();
    if(month > 12)
    {
    month = 1;
    year++;
    }
    }
    }
    }

    //this populates UniStorm with the time from this script
    if(weatherFound)
    {
    weather.realStartTime = hour;
    weather.realStartTimeMinutes = minute;
    weather.startTime = hour;
    weather.LoadTime();
    }
    }
    #endregion

    #region helpers
    public string CurrentDay
    {
    get { return dayOfWeek.ToString(); }
    }
    public string GetMonth()
    {
    //returns the name of the month
    switch (month)
    {
    case 1:
    return "Deepwinter";
    case 2:
    return "Wintersdusk";
    case 3:
    return "Newspring";
    case 4:
    return "Midspring";
    case 5:
    return "Springsebb";
    case 6:
    return "Summersdawn";
    case 7:
    return "Midsummer";
    case 8:
    return "Summersend";
    case 9:
    return "Autumnturn";
    case 10:
    return "Autumncrest";
    case 11:
    return "Autumunfade";
    case 12:
    return "Deepwinter";
    default:
    return "Deepwinter";
    }
    }
    public string GetDayOfWeek()
    {
    return dayOfWeek.ToString();
    }
    public string GetTime()
    {
    string AMPM = " AM";
    if (this.hour >= 12 && this.hour < 24)
    AMPM = " PM";
    else
    AMPM = " AM";
    return CalendarHelper.ConvertMilitaryTime(this.hour.ToString()) + ":" + this.minute.ToString("00") + AMPM;

    }
    public string GetDate()
    {
    return this.day + " - " + this.dayOfWeek.ToString();
    }
    public string GetYear()
    {
    return this.year + "ce";
    }
    #endregion




    public void SaveGameTime()
    {
    PlayerPrefs.SetInt("minute", this.minute);
    PlayerPrefs.SetInt("hour", this.hour);
    PlayerPrefs.SetInt("day", this.day);
    PlayerPrefs.SetInt("month", this.month);
    PlayerPrefs.SetInt("year", this.year);
    PlayerPrefs.SetInt("dayOfWeek", (int)this.dayOfWeek);
    }
    public void LoadGameTime()
    {
    int min = PlayerPrefs.GetInt("minute");
    int hour = PlayerPrefs.GetInt("hour");
    int day = PlayerPrefs.GetInt("day");
    int m = PlayerPrefs.GetInt("month");
    int y = PlayerPrefs.GetInt("year");
    int dow = PlayerPrefs.GetInt("dayOfWeek");

    this.minute = min;
    this.hour = hour;
    this.day = day;
    this.month = m;
    this.year = y;
    SetDayOfWeek(dow);
    }
    void SetDayOfWeek(int dow)
    {
    switch(dow)
    {
    case 0:
    this.dayOfWeek = System.DayOfWeek.Sunday;
    break;
    case 1:
    this.dayOfWeek = System.DayOfWeek.Monday;
    break;
    case 2:
    this.dayOfWeek = System.DayOfWeek.Tuesday;
    break;
    case 3:
    this.dayOfWeek = System.DayOfWeek.Wednesday;
    break;
    case 4:
    this.dayOfWeek = System.DayOfWeek.Thursday;
    break;
    case 5:
    this.dayOfWeek = System.DayOfWeek.Friday;
    break;
    case 6:
    this.dayOfWeek = System.DayOfWeek.Saturday;
    break;
    default:
    this.dayOfWeek = System.DayOfWeek.Sunday;
    break;
    }
    }
    #endregion

    #region ORK save/load methods
    public DataObject SaveGame()
    {
    DataObject data = new DataObject();
    data.Set("hour", this.hour);
    data.Set("minute", this.minute);

    data.Set("year", this.year);
    data.Set("month", this.month);
    data.Set("day", this.day);
    data.Set("dayOfWeek", (int)this.dayOfWeek);
    return data;
    }
    public void LoadGame(DataObject data)
    {
    if (data != null)
    {
    int dow = 0;
    data.Get("hour", ref this.hour);
    data.Get("minute", ref this.minute);

    data.Get("year", ref this.year);
    data.Get("month", ref this.month);
    data.Get("day", ref this.day);
    data.Get("dayOfWeek", ref dow);

    SetDayOfWeek(dow);
    PlayerPrefs.SetInt("IsLoadedGame", 1);
    }
    StartTimer();
    }
    #endregion
    }

  • Thanks for the script, but there are some unknown references like zoneinfo etc..
  • Oops, I thought I removed all of those :)

    They are not needed for what you are trying to do. That is game specific.
  • So, how the script should look like ? i'm sorry but scripting is not my cup of tea, i tried to remove some things that caused errors but that triggered more and more errors.. What should ineed to keep in this script to only have the data to be saved to ORK ?
  • edited April 2016
    Here is a completely stripped down version of my GameTime script.


    using UnityEngine;
    using System.Collections;
    using ORKFramework;

    public class GameTime : MonoBehaviour,ISaveData
    {
    #region fields
    public int hour;
    public int minute;
    public int seconds;

    public int month;
    public int day;
    public int year;
    public System.DayOfWeek dayOfWeek = System.DayOfWeek.Sunday;

    public float timeSpeed = 0.0001f;
    public float time;

    UniStormWeatherSystem_C weather;
    bool weatherFound = false;

    #endregion

    #region inits
    // Use this for initialization
    void Awake()
    {
    ORK.SaveGame.RegisterCustomData("GameTime", this, false);
    DontDestroyOnLoad(this.gameObject);
    }
    void Start ()
    {
    //load these from a file
    hour = 8;
    minute = 0;
    seconds = 0;

    month = 1;
    day = 1;
    year = 368;
    }
    void OnLevelWasLoaded()
    {
    weatherFound = false;
    GameObject UniStorm = GameObject.Find("UniStormSystemEditor");
    if(!ReferenceEquals (UniStorm,null))
    {
    weather = UniStorm.GetComponent<UniStormWeatherSystem_C>();
    if(!ReferenceEquals(weather,null))
    {
    weatherFound = true;
    }
    else
    {
    weatherFound = false;
    }
    }
    else
    {
    weatherFound = false;
    }
    }
    #endregion

    #region update methods
    public void StartTimer()
    {
    InvokeRepeating("TimeTick", 0, 6); //fires 6 seconds giving us 72 minutes = 1 game day
    }
    // Update is called once per frame
    void TimeTick()
    {
    //seconds += 60;

    minute++;
    if(minute > 60)
    {
    minute = 0;
    hour++;
    if(hour > 24)
    {
    hour = 0;
    seconds = 0;
    day++;
    this.dayOfWeek += 1;
    if(day > 30)
    {
    day = 1;
    month++;
    if(month > 12)
    {
    month = 1;
    year++;
    }
    }
    }
    }

    if(weatherFound)
    {
    weather.startTimeHour= hour;
    weather.startTimeMinute= minute;
    weather.startTime = hour;
    weather.LoadTime();
    }
    }
    #endregion



    #region temp save/load methods
    //call these if you want to change scenes and maintain the time between them
    public void SaveGameTime()
    {
    PlayerPrefs.SetInt("minute", this.minute);
    PlayerPrefs.SetInt("hour", this.hour);
    PlayerPrefs.SetInt("day", this.day);
    PlayerPrefs.SetInt("month", this.month);
    PlayerPrefs.SetInt("year", this.year);
    PlayerPrefs.SetInt("dayOfWeek", (int)this.dayOfWeek);
    }
    public void LoadGameTime()
    {
    int min = PlayerPrefs.GetInt("minute");
    int hour = PlayerPrefs.GetInt("hour");
    int day = PlayerPrefs.GetInt("day");
    int m = PlayerPrefs.GetInt("month");
    int y = PlayerPrefs.GetInt("year");
    int dow = PlayerPrefs.GetInt("dayOfWeek");

    this.minute = min;
    this.hour = hour;
    this.day = day;
    this.month = m;
    this.year = y;
    SetDayOfWeek(dow);
    }
    void SetDayOfWeek(int dow)
    {
    switch(dow)
    {
    case 0:
    this.dayOfWeek = System.DayOfWeek.Sunday;
    break;
    case 1:
    this.dayOfWeek = System.DayOfWeek.Monday;
    break;
    case 2:
    this.dayOfWeek = System.DayOfWeek.Tuesday;
    break;
    case 3:
    this.dayOfWeek = System.DayOfWeek.Wednesday;
    break;
    case 4:
    this.dayOfWeek = System.DayOfWeek.Thursday;
    break;
    case 5:
    this.dayOfWeek = System.DayOfWeek.Friday;
    break;
    case 6:
    this.dayOfWeek = System.DayOfWeek.Saturday;
    break;
    default:
    this.dayOfWeek = System.DayOfWeek.Sunday;
    break;
    }
    }
    #endregion

    #region ORK save/load methods
    public DataObject SaveGame()
    {
    DataObject data = new DataObject();
    data.Set("hour", this.hour);
    data.Set("minute", this.minute);

    data.Set("year", this.year);
    data.Set("month", this.month);
    data.Set("day", this.day);
    data.Set("dayOfWeek", (int)this.dayOfWeek);
    return data;
    }
    public void LoadGame(DataObject data)
    {
    if (data != null)
    {
    int dow = 0;
    data.Get("hour", ref this.hour);
    data.Get("minute", ref this.minute);

    data.Get("year", ref this.year);
    data.Get("month", ref this.month);
    data.Get("day", ref this.day);
    data.Get("dayOfWeek", ref dow);

    SetDayOfWeek(dow);
    PlayerPrefs.SetInt("IsLoadedGame", 1);
    }
    StartTimer();
    }
    #endregion
    }


    Post edited by keyboardcowboy on
  • Reviving an old thread, tried your script but it doesn't work.. even when i stop unistorm time, but i think we didn't understand the same thing.. what i wanted to achieve is to save the current state of unistorm like sun and moon rotation, hours etc..
  • I finally managed to make the save system on Unistorm ! For those interested i post my code, with this you can save your game and when you load it back, you will have the last hour, minute and day loaded ! :D

    using UnityEngine;
    using ORKFramework;

    public class UnistormSaveData : MonoBehaviour, ISaveData
    {
    public UniStormSystem Unistorm;
    public int StartingMinute = 0;
    public int StartingHour = 0;
    public int Minute = 1;
    public int Hour = 0;
    public int Day = 0;
    public int Month = 0;
    public int Year = 0;

    // register to the ORK save game handler
    void Start()
    {
    ORK.SaveGame.RegisterCustomData("test", this, false);
    Unistorm = GameObject.FindWithTag("Unistorm").GetComponent<UniStormSystem>();
    GameObject.DontDestroyOnLoad(this.gameObject);
    }

    void Update()
    {
    this.Minute = Unistorm.Minute;
    this.Hour = Unistorm.Hour;
    this.Day = Unistorm.Day;
    }

    // called when a game is saved
    public DataObject SaveGame()
    {
    DataObject data = new DataObject();

    data.Set("minute", this.Minute);
    data.Set("hour", this.Hour);
    data.Set("day", this.Day);

    return data;
    }

    // called when a game is loaded
    // depending on the 'beforeSceneLoad' parameter of the registration,
    // the function will be called either before or after loading the scene.
    public void LoadGame(DataObject data)
    {
    if (data != null)
    {
    data.Get("minute", ref this.Minute);
    data.Get("hour", ref this.Hour);
    data.Get("day", ref this.Day);
    Unistorm.Minute = Minute;
    Unistorm.Hour = Hour;
    Unistorm.Day = Day;
    }
    }
    }
Sign In or Register to comment.