Last active
April 14, 2025 03:06
-
-
Save tabularelf/2068ddc8d3e75f6674e8de487f7e9174 to your computer and use it in GitHub Desktop.
GameMaker function/method scope rules
This file contains 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
/* | |
Scope Rules: | |
Caller == Whoever calls this method/function, is scoped to the caller. This is dependent on if there's an lvalue (lvalue.func()) or not. (for methods, the scope will be undefined). | |
Instance == Regardless of whoever calls this method/function, it's scoped to the instance that declared it and not the caller. (Or if using method, whatever instance that was passed in the scope that's not "undefined") | |
*/ | |
// Declared in a script | |
// Scope == Caller, global (Will work no matter where you call it) | |
function myFunction() { | |
} | |
// Declared in a script | |
// Scope == Caller, global (but only if used via global. and the script isn't the same name as the variable) | |
myFunction = function() { | |
} | |
// Declared in an object | |
// Scope == Instance (CE1: Will highlight as if it's global, it is not global) | |
// As of 2024.13, this actually breaks in hilarious ways that you should NEVER do this in an object. | |
function myFunction() { | |
} | |
// Declared in an Object | |
// Scope == Instance (Won't highlight) | |
myFunction = function() { | |
} | |
// Declared in an Object, with method | |
// Scope == Instance | |
myFunction = method(self, OtherObject.myFunction); | |
// OR | |
myFunction = method(self, myGlobalFunction); | |
// OR | |
myFunction = method(self, function() {}); | |
// Declared in an Object, with method and scope set to undefined | |
// Scope == Caller | |
myFunction = method(undefined, OtherObject.myFunction); | |
// OR | |
myFunction = method(undefined, myGlobalFunction); | |
// OR | |
myFunction = method(undefined, function() {}); | |
// Declared in a constructor | |
// Scope == Instance | |
function myConstructor() constructor { | |
myFunction = function() { | |
} | |
} | |
// Declared in a constructor, but with static | |
// Scope == Caller | |
function myConstructor() constructor { | |
static myFunction = function() { | |
} | |
} | |
// Declared in an object, but assigned to another object | |
// Scope == Insstance (declared by objectA) | |
// objectA create event | |
objectB.func = function() { | |
return object_get_name(object_index); | |
} | |
show_message(objectB.func()); // Prints out "objectA" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment