Singleton : Fields vs Globals

BlitzMax Forums/BlitzMax Programming/Singleton : Fields vs Globals

It's kind of offtopic but that's the way I learnd how to use a singleton:


Type TSingleton
  ...
End Type

Global FMySingleton: TSingleton

Function MySingleton: TSingleton
    If FMySingleton = Null Then
        FMySingleton = New TSingleton
    End If
    Return FMySingleton
End Function

'Somewhere in your code
MySingleton.SomeValue = 1
MySingleton.DoSomething()


You do not have to care about if your object was created or not. Also you always have just one instance and it will only created if you really need it.