- Home /
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
Well my questions are related to your problem, I just don't think you're using Inheritance or Singletons properly.
I guess what I really should've asked is: Do these objects really need to be shared across the entire program or are they specific to a Scene? If they do need to be shared across the entire program then the Singleton pattern is a good option. If they're scene specific then consider using a game object. Dragging relationships in the editor may be annoying, but it really does help with larger projects by making bugs easier to trace.
Do all these singletons need to inherit from a single parent? Do they share state or behavior in any way, or do you really want to define some common interface to all of these singletons? If the latter you should look up something called "Duck Typing", its much better suited to Javascript. In both cases actually, Duck Typing may be better.
Answer by whydoidoit · Apr 04, 2013 at 10:58 AM
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...)
Sure that would look something like this:
using System.Collections;
public class $$anonymous$$onoSingleton<T> : $$anonymous$$onoBehaviour where T: $$anonymous$$onoSingleton<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 usine AwakeEx and OnDestroyEx in the classes derived from $$anonymous$$onoSingleton - 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 $$anonymous$$onoCollection<T> : $$anonymous$$onoBehaviour where T : $$anonymous$$onoCollection<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();
}
}
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
Answer by MorphingDragon · Apr 03, 2013 at 09:11 AM
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 http://stackoverflow.com/questions/12762550/example-of-javascript-duck-typing/12763070#12763070
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 program$$anonymous$$g 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.
Your answer
Follow this Question
Related Questions
MonoBehaviour Inheritance, Singletons and Generics 1 Answer
C# Generics not constraining correctly? 1 Answer
Calling coroutines through pointers/"Function" class? 2 Answers
Using functions of Inherited classes 3 Answers
C# Inheriting from MonoBehaviour but still be able to instantiate with new()? How should I do this? 2 Answers