Skip to content

Instantly share code, notes, and snippets.

@tabularelf
Last active May 16, 2024 10:41
Show Gist options
  • Save tabularelf/2068ddc8d3e75f6674e8de487f7e9174 to your computer and use it in GitHub Desktop.
Save tabularelf/2068ddc8d3e75f6674e8de487f7e9174 to your computer and use it in GitHub Desktop.
GameMaker function/method scope rules
/*
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 (Will highlight as if it's global, it is not global)
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