Making it easier to access singletons.

I’m using a singleton to store various game data.
However, accessing the singleton is slightly cumbersome because I have to write something like this:

GS.Singleton.characterList[GS.Singleton.characterIndex]…etc…

I suppose I could rename Singleton to something shorter, but is it possible to somehow “remap” this so I don’t have to type so much?

I hope I don’t seem lazy :slight_smile:

Kjetil

One thing you could do is make static accessor functions or properties that directly use the internal singleton. You could do something like the following…

public class GS
{
   public static List<Character> CharacterList
   {
        get { return Singleton.characterList; }
   }

   //...rest of the class below
}

Then you just call the following…

GS.CharacterList[...]....

You could create a reference to the singleton.

Something like
GS gs = GS.Singleton;

From that point on, you can simply use gs.characterList[gs.characterIndex];

Small side note : There is a lot of controversy around the (proper) use of a singleton because it’s supposed purpose is to make sure a class can only be instantiated once, but is often used to have public access to the instance of said class (often considered a bad thing).

Thanks to both of you! both methods seems to be possible for me. I’ll try both and see what works best.
I need to read up on singleton usage I guess.

I originally had a static class, but I needed the class properties to be visible and editable in the inspector.

This is to be used for player stats, and I’m still messing around with the structure.

I have a list of characters which I want to be easily available.

I’ll probably go for having a singleton which contains a list of the characters (+ more stuff), and during character selection, I’ll pick the id, and instanciate that selected character, and set it not to be destroyed on load.

It’s not so easy coming up with a clean and simple system. heh :slight_smile: