Search Unity

Keeping a variable over scripts

Discussion in 'Scripting' started by Braveheart, Sep 11, 2012.

  1. Braveheart

    Braveheart

    Joined:
    Sep 6, 2012
    Posts:
    17
    I have finally set-up a user login screen which checks the information against a database. Should the user enter correct account details they'll then be taken to the Character scene where they would either chose an existing character or create a new one. What I need to do is hold onto the username (or id from the database) across the menu script to the characters script and of course into the main game logic scripts so I can read and write to the correct database account entry when needed.

    Can someone help me out with this? I'm coding in C#. I had thought about saving the username locally to a player's computer, but that's probably not a great idea. What's the best way to go about persistent accounts in a game?
     
    Last edited: Sep 11, 2012
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Create a singleton object to access special global game objects from...

    something like so:

    (note it does NOT inherite from MonoBehavior)
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Game
    7. {
    8.  
    9.     #region Singleton Interface
    10.  
    11.     private static Game _instance;
    12.  
    13.     public static Game Instance
    14.     {
    15.         get
    16.         {
    17.             if (_instance == null) _instance = new Game();
    18.             return _instance;
    19.         }
    20.     }
    21.  
    22.     #endregion
    23.  
    24.     #region CONSTRUCTOR
    25.  
    26.     private Game()
    27.     {
    28.         //block constructor so only singleton
    29.     }
    30.  
    31.     #endregion
    32.  
    33.     #region Properties
    34.  
    35.     public string Username
    36.     {
    37.         get;
    38.         set;
    39.     }
    40.  
    41.     #endregion
    42.  
    43. }
    44.  
    Then from any scripts you write you can say:

    Code (csharp):
    1.  
    2. Game.Instance.Username = "someusername";
    3.  
    4. if(Game.Instance.Username == "joe")
    5. {
    6.     //do stuff
    7. }
    8.  
    I use this to store many global entry values for the game. Like an object that represents the player, inventory, etc.

    And yes, it'll persist from level to level.
     
  3. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Why not simply using a static class instead of a singleton? This saves the call over Instance:

    Code (csharp):
    1.  
    2. public static class Game
    3. {
    4.   public static string Username; // optional: make it as a property when code should be performed when accessing or setting values
    5. }
    6.  
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    because more can be done than in a static.

    static classes don't lend toward many design patterns out there.

    Nor does it let me do the simple stuff like implementing interfaces, referencing my singleton (could be used as parameter this way), or having a simple upgrade path to multiton (which could help in multi-player mode).

    Furthermore I don't have to type 'static' everywhere.
     
    Last edited: Sep 11, 2012
  5. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    For the simple use case the OP had its enough, no need to create an extra singleton when all you want to do is to store plain data ;)
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    That can be true, and worth your posting a static class as a solution.

    You just asked why I use singleton, so I gave an answer.

    Neither is more correct than the other, and there are actually other solutions as well, so we can leave it up to OP to decide what they need. Obviously they're in the beginning of structuring their game... who knows what else they'll need to jam in there, so who knows how "simple use" the OP requires.
     
    Last edited: Sep 11, 2012
  7. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    It was not my intend to mock you. Let the OP do what he wants :)
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I wasn't criticizing an attempt to mock. I was criticizing your over simplification of the scenario.

    ... smiley
     
  9. Braveheart

    Braveheart

    Joined:
    Sep 6, 2012
    Posts:
    17
    Thanks a lot for the feedback. I tried both methods and the public class seems to work best (the singleton worked but it kept throwing errors when trying to use the value). Is this going to be a safe method to use to store username and password (internally) for a multi-player online game?
     
    Last edited: Sep 13, 2012