Skip to content

Instantly share code, notes, and snippets.

@tinkerer-red
Created October 12, 2023 10:25
Show Gist options
  • Save tinkerer-red/f95ed7d625d8edc8c374fd387b4bc2d4 to your computer and use it in GitHub Desktop.
Save tinkerer-red/f95ed7d625d8edc8c374fd387b4bc2d4 to your computer and use it in GitHub Desktop.
Example of super using a self contained callstack
function log(_str) {return show_debug_message(_str)};
function A() constructor {
static example = function(){
log("Calling A");
}
}
function B() : A() constructor {
static example = function() {
log("Calling B");
super.example();
}
}
function C() : B() constructor {
static example = function() {
log("Calling C");
super.example();
//note that if at any point you dont use the defined super, you will cause an infinate loop,
// comment out the line above, and uncomment the lines below to test this.
//var _func = B.example;
//_func();
}
}
function D() : C() constructor {
static example = function() {
log("Calling D");
super.example();
}
}
function E() : D() constructor {
static example = function() {
log("Calling E");
super.example();
//var _func = D.example;
//_func();
}
}
function F() : E() constructor {
static example = function() {
log("Calling F");
super.example();
}
}
var _test = new A();
_test.example();
log("\n")
//make sure to test these out of order
var _test = new C();
_test.example();
log("\n")
var _test = new B();
_test.example();
log("\n")
//done
var _test = new D();
_test.example();
log("\n")
var _test = new E();
_test.example();
log("\n")
var _test = new F();
_test.example();
log("\n")
#macro super static __super__ = new __super(_GMFUNCTION_); __super.__data__.__self__ = self; __super__
function __super(_func_name) constructor {
static __data__ = {
__depth__ : 0,
__self__ : 0,
}
//start instantly by grabbing the parent
var _statics = static_get(static_get(other));
repeat(__data__.__depth__) {
_statics = static_get(_statics);
}
var _parent_statics = _statics;
#region Find all parent structs
//this is used as a massive work around for html5 lacking support for `instanceof(_statics)`
// in the future uncomment out the parent struct name != object lines
static __blank_constructor__ = function() constructor {};
static __blank_object__ = new __blank_constructor__();
static __object_statics__ = static_get(static_get(__blank_object__));
///////////////////////////////////////////////////////////////////////////////////////////
///////while (_parent_struct_name != "Object") {
while (__object_statics__ != _parent_statics) {
var _key, _val;
var _names = variable_struct_get_names(_parent_statics);
var _size = array_length(_names);
var _i=0; repeat(_size) {
_key = _names[_i]
//skip internal keys and already included keys
if (_key == "<unknown built-in variable>")
|| (variable_struct_exists(self, _key)) {
_i+=1;
continue;
}
_val = _parent_statics[$ _key]
self[$ _key] = method({func: _val}, function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) {
__super.__data__.__depth__ += 1;
//this is a safety check to ensure testing continues even if we hit one fatal error.
if (__super.__data__.__depth__ > 10) exit;
var _return = method(__super.__data__.__self__, func)(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
__super.__data__.__depth__ -= 1;
return _return
})
_i+=1;}//end repeat loop
//continue to the next struct
//var _parent_struct_name = instanceof(_parent_statics)
_parent_statics = static_get(_parent_statics);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment