We can have OOP support in SQF with simply 1 new command (createObjectFromClass), 1 command overload (new call), and slight change in internal hashmap data structure:
_class = [ ["constructor", { _thisObj call ["SomeMethod", _this] } ], ["SomeMethod", { _someObj = createVehicle ["bla", [0,0,0]]; _thisObj set ["mVehicle", _someObj]; } ], ["Method", {...}], ["mVehicle", objNull], ["destructor", {deleteVehicle (_thisObj get "mVehicle");} ]; _obj = createObjectFromClass [_class, [_constArg1, _constArg2]]; _obj call ["Method", _args];
Under the hood, _obj is just a hashmap (typeName _obj == "HASHMAP"). createObjectFromClass simply does this:
- createHashmapFromArray _class
- calls the "constructor" (see below for call behavior)
- sets a flag on the hashmap in the game data: map.treatAsObject = true
We also need an overload of call: _hashmap call ["method", args]. the only difference between this and normal call is that it also defines a magic variable _thisObj when it calls the method
And finally, when the game wants to destroy the hashmap, if map.treatAsObject is true, it calls the destructor (if it exists)
This might look a bit clunky at first but it'll definitely open up so many new possibilities and makes many things more convenient.