Created
October 30, 2024 21:37
-
-
Save tinkerer-red/2bb127fc1888a56a96bb543a06497bf4 to your computer and use it in GitHub Desktop.
a macro for `parent_constructor`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #macro parent_constructor __parent_constructor(asset_get_index(_GMFUNCTION_)) | |
| /// @ignore | |
| function __parent_constructor(_ref) { | |
| if (struct_exists(static_get(_ref), "__parent_constructor__")) return static_get(_ref).__parent_constructor__; | |
| var _constructor = undefined; | |
| //check to see if this is the constructor | |
| var _tags = asset_get_tags(_ref, asset_script) | |
| if (array_contains(_tags, "@@constructor")) { | |
| //assume it's the root function, and not any static methods because currently anon constructors do not get tags | |
| _constructor = _ref; | |
| static_get(_ref).__parent_constructor__ = static_get(static_get(_ref)); | |
| } | |
| else { | |
| //parse the static tree until we have a match | |
| var _static = static_get(self); | |
| var _break = false; | |
| var _parent = static_get(_static); | |
| //check each static parent | |
| while (_static != undefined) { | |
| var _names = struct_get_names(_static) | |
| var _i=0; repeat(array_length(_names)) { | |
| var _value = _static[$ _names[_i]] | |
| //prevent recursion checks | |
| if (_value == _parent) { | |
| _i++ | |
| continue; | |
| } | |
| //check each value for a match | |
| if (is_method(_value)) | |
| && (method_get_index(_value) == _ref) { | |
| _constructor = _static | |
| _break = true; | |
| break; | |
| } | |
| _i++} | |
| if (_break) break; | |
| _static = _parent | |
| _parent = static_get(_static); | |
| } | |
| if (_static == undefined) { | |
| throw "Calling `parent_constructor` inside a constructor which has no parent" | |
| } | |
| static_get(_ref).__parent_constructor__ = static_get(_static); | |
| } | |
| return static_get(_ref).__parent_constructor__; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function A() : B() constructor { | |
| var _super = parent_constructor.fun; | |
| _super() | |
| static fun = function() { | |
| show_debug_message("A's fun") | |
| var _super = parent_constructor.fun; | |
| _super() | |
| } | |
| } | |
| function B() : C() constructor { | |
| static fun = function() { | |
| show_debug_message("B's fun") | |
| var _super = parent_constructor.fun; | |
| _super() | |
| } | |
| } | |
| function C() constructor { | |
| static fun = function() { | |
| show_debug_message("C's fun") | |
| } | |
| } | |
| var t = new A(); | |
| t.fun(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment