diff --git a/Manual/contents/GameMaker_Language/GML_Overview/Method_Variables.htm b/Manual/contents/GameMaker_Language/GML_Overview/Method_Variables.htm index 2bef0c16d..1b5398f9d 100644 --- a/Manual/contents/GameMaker_Language/GML_Overview/Method_Variables.htm +++ b/Manual/contents/GameMaker_Language/GML_Overview/Method_Variables.htm @@ -35,9 +35,9 @@

Creating Methods

}

 To create a function in GML Visual, use Declare A New Function.

-

In general, however, you would use the first form for methods, and the second form for defining script functions, since the second form will also assign a script index to the function while the first form will be a "true" method.

+

It is recommended that you should use the first form for methods, and the second form for defining script functions, since the second form will also assign a script index to the function while the first form will be a "true" method. Using the second form introduces differences that are covered in Overridden Script Functions.

+

Defining a function in an Object's event using either syntax will make the function only available in the context of its instance (i.e. using the script function syntax will not make the function globally available unless it is in a Script asset).

 You can check this by using both forms in a script and then calling the runtime function typeof on each of them. One will be classed as a "ref" - since it returns a script function reference - and the other will be classed as a "method".

-

 Defining a function in an Object's event using either syntax will make the function only available in the context of its instance (i.e. using the script function syntax will not make the function globally available unless it is in a Script asset).

 A function defined in an Object's event using the script function syntax will be available to its instances throughout their lifetime, even when the event has not run yet. For example, defining a script function in the Alarm 0 event will make the function available to call in the Create event, regardless of when that alarm event runs (or whether it runs at all).

So, keep in mind that - in general - we will always be referring to functions that have not been defined with a script index when we are talking about methods and method variables.

The syntax for defining a method inside a struct literal is similar to the first syntax above, however the = is replaced by a : as required by struct literals:

@@ -94,6 +94,24 @@

Optional Arguments

The above function takes three arguments, where the last two are optional. If they are not passed in when the function is called, then they'll use the instance's x and y coordinates by default.

Note that if an argument is not passed in, and it does not have a default value, then it will be equal to undefined.

Methods can also make use of static variables, which maintain their values throughout every function call. Please read this page for more information.

+

Method Overriding

+

Methods created inside instances or structs can be overridden later by assigning a new function to the same variable, e.g. in the same event in a child object:

+

// obj_parent Create event
+ say_hello = function()
+ {
+    show_message("I am the parent!");
+ }
+
+ // obj_child Create event
+ event_inherited();
+
+ say_hello = function()
+ {
+    show_message("I am the child!");
+ } +

+

All calls to say_hello in obj_child will print "I am the child!".

+

Also see: Overridden Script Functions

Methods Are Structs

GameMaker stores methods as structs, where each "method struct" holds a reference to the script function to call and the struct or instance to which it is bound. You can get the script function behind a method by calling method_get_index and its bound struct or instance with method_get_self.

Methods being structs is irrelevant in most cases as you would call them as functions and pass them into other functions that take methods - you would not use them as structs. However this detail can be important in a few edge cases, for example with the Static Struct, as each function in GameMaker has a static struct and each struct can have its own static struct that forms the Static Chain. However the static struct functionality for method structs is disabled so you can directly access the static struct of the function behind it. Calling static_get will give you the static struct for the function behind the method and calling static_set on a method will do nothing.

@@ -117,7 +135,7 @@

Function Reference

Next: Script Functions vs. Methods
-
© Copyright YoYo Games Ltd. 2024 All Rights Reserved
+
© Copyright YoYo Games Ltd. 2025 All Rights Reserved