Search Unity

Playerprefs

Discussion in 'Scripting' started by Pandzas, Jul 13, 2021.

  1. Pandzas

    Pandzas

    Joined:
    Sep 30, 2020
    Posts:
    10
    So i have a script that has a timer set up like this
    Code (CSharp):
    1. if (timeRemaining > 0)
    2.                 {
    3.                     timeRemaining -= Time.deltaTime;
    4.                     DisplayTime(timeRemaining);
    5.                  }
    6.  
    7. void DisplayTime(float timeToDisplay)
    8.     {
    9.         timeToDisplay += 1;
    10.  
    11.         float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    12.         float seconds = Mathf.FloorToInt(timeToDisplay % 60);
    13.  
    14.         timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    15.     }
    and i have a playerpref like this
    Code (CSharp):
    1. void OnDestroy()
    2.     {
    3.         PlayerPrefs.SetString(TimeLeft, timeText.text);
    4.     }
    my problem is that i have no clue on how to save the timer when i load a scene, since the timer is a text i dont know how to save it in the playerpref. any tips
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    359
    You shouldn't save text. You should save timeToDisplay and generate text on load.

    I don't want to talk about clever patterns and architecture, but always try to separate your game logic (so called "Model") and your "View".
    Saving data should be called from your game logic, and it shouldn't rely on view, it shouldn't care about how your simple number will be formatted and shown to player. Today it's minutes and hours, next day it's something else, your saved data shouldn't become invalid because of view changes.
     
    Pandzas and Nefisto like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Every single bit that @Tekrel says above is 100% spot-on: understand the difference between the value (floating point) versus the presentation to the player (which could be text, string, UI, button, lever, arrow, image, clock dial, whatever).

    Only save/load the value.

    Here is a handy way to wrap up playerprefs to save stuff without having to sprinkle playerprefs calls all over your codebase:

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
    Pandzas and Nefisto like this.
  4. Pandzas

    Pandzas

    Joined:
    Sep 30, 2020
    Posts:
    10
    Okay i am trying to save the timeToDisplay and make text at start but i have no clue what isnt working here can you pls help me
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Timer : MonoBehaviour
    7. {
    8.     public float timeRemaining = 1800;
    9.     public float timeReset = 1800;
    10.     public bool timerIsRunning = false;
    11.     public Text timeText;
    12.     public Text counterText;
    13.     float number = 0;
    14.  
    15.  
    16.     string TimeLeft = "TimeOnLeave";
    17.     string TimeDisplayTime = "TimeInDisplayTime";
    18.     string HeartLeave = "HeartsOnLeave";
    19.  
    20.     void Start()
    21.     {
    22.         // Starts the timer automatically
    23.         timerIsRunning = true;
    24.         number = PlayerPrefs.GetFloat(HeartLeave);
    25.         Debug.Log(PlayerPrefs.GetFloat(TimeLeft));
    26.  
    27.         float minutes = Mathf.FloorToInt(PlayerPrefs.GetFloat(TimeLeft) / 60);
    28.         float seconds = Mathf.FloorToInt(PlayerPrefs.GetFloat(TimeLeft) % 60);
    29.  
    30.         timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         if (number <= 4)
    36.         {
    37.             if (timerIsRunning)
    38.             {
    39.                 if (timeRemaining > 0)
    40.                 {
    41.                     timeRemaining -= Time.deltaTime;
    42.                     DisplayTime(timeRemaining);
    43.                 }
    44.                 else
    45.                 {
    46.                     Debug.Log("Time has run out!");
    47.                     timeRemaining = timeReset;
    48.                     timerIsRunning = true;
    49.                     number += 1;
    50.                 }
    51.             }
    52.  
    53.             counterText.text = string.Format("{0}", number);
    54.         }
    55.         if(number == 5)
    56.         {
    57.             counterText.text = string.Format("FULL");
    58.         }
    59.     }
    60.  
    61.     public void DisplayTime(float timeToDisplay)
    62.     {
    63.         timeToDisplay += 1;
    64.  
    65.         float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    66.         float seconds = Mathf.FloorToInt(timeToDisplay % 60);
    67.  
    68.         timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    69.  
    70.         PlayerPrefs.SetFloat(TimeDisplayTime, timeToDisplay);
    71.     }
    72.  
    73.     public void OnDestroy()
    74.     {
    75.         PlayerPrefs.SetFloat(TimeLeft, PlayerPrefs.GetFloat(TimeDisplayTime));
    76.         PlayerPrefs.SetFloat(HeartLeave, number);
    77.     }
     
  5. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    359
    Your class seems pretty small and simple. You can use debug.log or put breakpoint and walk step by step on every instruction checking all arguments.
    Get used to figuring it out on your own, nobody will really help you with "this isn't working, I don't know why", this is programming, almost nobody will do your work for you.

    I can only give you some more tips. Make smaller functions, give them better names, also you can separate Timer main logic from visual part and text operations, it will help you focus on algorithm/view separately. Sometimes it's really helpful, good refactoring and class separation unload your brain and make you think better.
     
    Pandzas likes this.