Extending a Singleton base class (UJS) (?)

So I have a lot of singletons in my game as it seems and want to make a base class for all of them to derive from. Now, is that even possible in unity javascript since it doesn’t support generics(?). The C# way should be:

class MySingleton : MonoSingleton<MySingleton>{

}

is something like this possible or some other alternative?

Thanks

singletons should be used sparingly. Is there a reason why you need so many.

many = 5 btw. I prefer to have a singleton for a unique persistant object that performs a job, rather than: A) having a singleton tha does all jobs B) having a singleton than has reference to the other objects that perform a single job Even with 5 or 6, I would like to go the good way and use inheritance rather than copy pasting code

Wouldn't it be better then to use Unity's builtin messaging dispatch? Or have a game object than can be referenced by a tag?

I've build my own messaging/event system. Unity's didn't cut it. I dont want to use tags because then that will either mean that I have a reference of the "manager" into the class that uses it, or have to search for it every time the class needs it which is not good. heh it became a discussion about if singletons are good or not, rather than a Q&A :P

unity's messaging dispatch is useless the op is asking is something like this possible?

2 Answers

2

UnityScript (Javascript in Unity) is not Javascript and absolutely does have classes!

However UnityScript cannot define a generic class - it can however inherit from one…

If you defined your base MonoSingleton class in C# in a Plugins folder then you could use:

  class TestInherit extends MonoSingleton.<TestInherit>
  {
  }

The base class would look like this:

using System.Collections;

public class MonoSingleton<T> : MonoBehaviour where T: MonoSingleton<T> { 
	
	public static T instance;
	
	void Awake()
	{
		instance = this as T;
		AwakeEx();	
	}
	
	protected virtual void AwakeEx()
	{
	}
	
	protected virtual void OnDestroyEx()
	{
	}
	
	void OnDestroy()
	{
		OnDestroyEx();
		if(instance == this) instance = null;
	}
}

Note to avoid problems you should use AwakeEx and OnDestroyEx in the classes derived from MonoSingleton - they work just the same way but ensure the singleton is handled properly

While I’m at it, here’s a base class that creates a static collection of all the enabled derivatives:

using System.Collections.Generic;
using Unity.Engine;

public class MonoCollection<T> : MonoBehaviour where T : MonoCollection<T>
{
    public static List<T> all = new List<T>();
	
	protected virtual void OnEnableEx()
	{
	}
	
	protected virtual void OnDisableEx()
	{
	}
	
    void OnEnable()
    {
        all.Add(this as T);
	    OnEnableEx();
    }
	
    void OnDisable()
    {
        all.Remove(this as T);
        OnDisableEx();
    }
}

i elevated that to an answer because (of course) it rocks. it's so obvious once you say it - put the base in c#

Since you have to do everything else around here I was thinking perhaps you could paste in an awesome example of the c# sgtn base, for programmers who are more JS oriented. (Err not me of course, I'm thinking of a friend who it could help...)

So in a nutshell, UJS doesnt support declaring generics, does support inheriting generics. Thanks. I will use my own singleton though :P but thanks for the code to make clear on the generics

See my comments above, but this website should have explanations on the type of inheritance and interfaces in Javascript. But the simple answer is Javascript doesn’t have classes at all technically, but you can program it like it does. A more Javascript friendly way would be to use something called Duck Typing.

[Edit: whydoidoit the following link is relevant to real Javascript and not the Javascript which is present in Unity - Javascript in Unity is a .NET language more like ActionScript than it is like real Javascript. Many real Javascript techniques do not apply or are flawed in their by their implementation in Unity. Not to say UnityScript isn’t good - it just isn’t Javascript]

http://www.crockford.com/javascript/inheritance.html

Yes, as stated in my second comment ("..unique persistant object..") they need to be along the whole program. They do share a behaviour and that is of being a singleton and I hate copy pasting code. By the way UNITYscript, does have inheritance all fine, interfaces and all. Thanks for answer and your time :)

Oh by the way I will look into Duck Typing, although I think that doesnt answer if UnityScript support Generics :)

Also Duck typing is a very bad idea in UnityScript/Javascript (in Unity).

Generally speaking it's actually a bad solution in most cases in a lot of languages; A result of trying to get the language to do something it wasn't designed for. It is there if needed, it's a tool like any other programming technique or design pattern.

Yeah agreed - I just wish they hadn't called UnityScript damn Javascript - screwed me up as a JS programmer when I first started and I know @Fattie and I have discussed the differences at length.