Heh, nice -- using ThinkerIterator to get the only thinker of this type.
This is the sort of thing that would be super-useful in the eventual library, so here's my (UNTESTED, MAY NOT WORK) first pass at this:
Code: Select all
/* -- base library class -- */
class SzlGlobal : Thinker
{
SzlGlobal _InitBase()
{
SetStatNum(STAT_INFO); // this is merely to let the thinker iterator find it quicker.
self.Init();
return self;
}
virtual SzlGlobal Init()
{
// override to set custom variables.
}
static SzlGlobal _Get(class<SzlGlobal> type)
{
ThinkerIterator it = ThinkerIterator.Create(type);
let p = SzlGlobal(it.Next());
if (p == null)
{
p = new(type)._InitBase();
}
return p;
}
}
Code: Select all
/* -- custom child class -- */
class CustomGlobal : SzlGlobal
{
int customVar;
override SzlGlobal Init()
{
customVar = 1337;
}
// [XA] just a convenience function, technically; one can
// call SzlGlobal._Get("CustomGlobal") directly and
// cast it, but I quite dislike the possibility of
// modder-side typos in the child class name.
static CustomGlobal Get()
{
return CustomGlobal(SzlGlobal._Get("CustomGlobal"));
}
}
Suggestions welcome -- this is a rough top-of-head draft (and there are probably errors). At the least, please forgive the pre-underscores -- that's a Javascript-ism for "you can technically call this function, but shouldn't." Felt it'd be appropriate here.
Related naming question: "SzlGlobal" or "SzlSingleton"? It's technically the latter, but modders will be accustomed to the former nomenclature.