Skip to content

Commit

Permalink
docs(feature): Document that methods are structs and their static str…
Browse files Browse the repository at this point in the history
…ucts cannot be accessed

YoYoGames/GameMaker-Bugs#7934
  • Loading branch information
gurpreetsinghmatharoo committed Oct 15, 2024
1 parent f7b9d9b commit eef14b0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ <h2>Creating Methods</h2>
<p>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&#39;ll use the instance&#39;s <span class="inline">x</span> and <span class="inline">y</span> coordinates by default.</p>
<p>Note that if an argument is not passed in, and it does not have a default value, then it will be equal to <span class="inline">undefined</span>.</p>
<p>Methods can also make use of static variables, which maintain their values throughout every function call. Please <a href="Functions/Static_Variables.htm">read this page</a> for more information.</p>
<h3 id="h">Methods Are Structs</h3>
<p><span data-keyref="GameMaker Name">GameMaker</span> stores methods as structs, where each &quot;method struct&quot; 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 <span class="inline3_func"><a href="../GML_Reference/Variable_Functions/method_get_index.htm">method_get_index</a></span> and its bound struct or instance with <span class="inline3_func"><a href="../GML_Reference/Variable_Functions/method_get_self.htm">method_get_self</a></span>.</p>
<p><strong>Methods being structs is irrelevant in most cases</strong> 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 <a data-xref="{title}" href="Structs/Static_Structs.htm">Static Struct</a>, as each function in <span data-keyref="GameMaker Name">GameMaker</span> has a static struct and each struct can have its own static struct that forms the <a data-xref="{text}" href="Structs/Static_Structs.htm#h">Static Chain</a>. However the static struct functionality for method structs is <strong>disabled</strong> so you can directly access the static struct of the function behind it. Calling <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Variable_Functions/static_get.htm">static_get</a></span> will give you the static struct for the function behind the method and calling <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Variable_Functions/static_set.htm">static_set</a></span> on a method will do nothing.</p>
<h2>Function Reference</h2>
<p>Below we list a few helper functions associated with method variables:</p>
<ul class="colour">
<li><a href="../GML_Reference/Variable_Functions/is_method.htm">is_method</a></li>
Expand All @@ -94,6 +98,7 @@ <h2>Creating Methods</h2>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ <h2>Checking Inheritance</h2>
<p>This is done by checking if your struct has the given constructor&#39;s static struct anywhere in its static chain.</p>
<div data-conref="../../../assets/snippets/is_instanceof_example.hts"> </div>
<h2 id="h1">Changing The Static Struct</h2>
<p>The function <span class="inline3_func"><a data-xref="{title}" href="../../GML_Reference/Variable_Functions/static_set.htm">static_set</a></span> is provided to let you change the static struct of a function (constructor or not). This way you can change what static variables are available to a constructor and its structs, and also change the &quot;static chain&quot; that a constructor belongs to.</p>
<p>The function <span class="inline3_func"><a data-xref="{title}" href="../../GML_Reference/Variable_Functions/static_set.htm">static_set</a></span> lets you change the static struct of another struct. This way you can make changes to a struct&#39;s &quot;static chain&quot;.</p>
<p>The recommended use-case for this function is deserialisation. If you&#39;re loading structs from JSON, those structs won&#39;t belong to any constructors, however you can change that by using <span class="inline3_func"><a data-xref="{title}" href="../../GML_Reference/Variable_Functions/static_set.htm">static_set</a></span> to &quot;apply&quot; a constructor to a struct, so that that struct receives its shared static variables and you can run <span class="inline3_func"><a data-xref="{title}" href="../../GML_Reference/Variable_Functions/is_instanceof.htm">is_instanceof</a></span> to check its kind.</p>
<div data-conref="../../../assets/snippets/static_set_example.hts"> </div>
<p> </p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<body>
<!--<div class="body-scroll" style="top: 150px;">-->
<h1><span data-field="title" data-format="default">method_get_self</span></h1>
<p>This function retrieves the instance or struct reference which is the <a href="../../GML_Overview/Instance_Keywords.htm"><span class="inline">self</span></a> context used when the method is called. If the variable is <i>not</i> a method then the function will return <span class="inline2">undefined</span>.</p>
<p>This function retrieves the instance or struct reference which is the <a href="../../GML_Overview/Instance_Keywords.htm"><span class="inline2" id="">self</span></a> context used when the method is called. If the variable is <i>not</i> a method then the function will return <span class="inline2">undefined</span>.</p>
<p> </p>
<h4>Syntax:</h4>
<p class="code"><span data-field="title" data-format="default">method_get_self</span>(method);</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
<body>
<h1><span data-field="title" data-format="default">static_get</span></h1>
<p>This function returns the <a data-xref="{title}" href="../../GML_Overview/Structs/Static_Structs.htm">Static Struct</a> for the given function or struct.</p>
<p>When you supply a function or method, this function returns the static struct for that function or method.</p>
<p>You can also supply a struct. What&#39;s returned depends on the struct: </p>
<p>When a struct is passed, the return value will be depend on the struct itself: </p>
<ul class="colour">
<li>For a struct created from a constructor using the <span class="inline2"><a data-xref="{title}" href="../../GML_Overview/Language_Features/new.htm">new</a></span> keyword: the static struct for the constructor that was used to create the struct (or a different struct if it was changed after creation using <span class="inline3_func"><a data-xref="{title}" href="static_set.htm">static_set</a></span>).</li>
<li>For a static struct: the parent static struct in the <a data-xref="{text}" href="../../GML_Overview/Structs/Static_Structs.htm#h">Static Chain</a>. When using constructor inheritance, static structs are chained, i.e. you can get the static struct of a static struct, if the constructor has a parent constructor.</li>
<li>For any other struct: this struct&#39;s &quot;parent&quot; struct, which links the struct to the &quot;root&quot; struct.</li>
<li>For the &quot;root&quot; struct: <span class="inline2">undefined</span></li>
<li><strong>For a method</strong>: Returns the static struct for the function behind the method and <strong>not</strong> the method struct itself (methods are stored as structs, see: <a data-xref="{text}" href="../../GML_Overview/Method_Variables.htm#h">Methods Are Structs</a>).</li>
<li><strong>For a struct created from a constructor using the <span class="inline2"><a data-xref="{title}" href="../../GML_Overview/Language_Features/new.htm">new</a></span> keyword</strong>: Returns the static struct for the constructor that was used to create the struct (or a different struct if it was changed after creation using <span class="inline3_func"><a data-xref="{title}" href="static_set.htm">static_set</a></span>).</li>
<li><strong>For a static struct</strong>: Returns the parent static struct in the <a data-xref="{text}" href="../../GML_Overview/Structs/Static_Structs.htm#h">Static Chain</a>. When using constructor inheritance, static structs are chained, i.e. you can get the static struct of a static struct, if the constructor has a parent constructor.</li>
<li><strong>For any other struct</strong>: Returns this struct&#39;s &quot;parent&quot; struct, which links the struct to the &quot;root&quot; struct.</li>
<li><strong>For the &quot;root&quot; struct</strong>: Returns <span class="inline2">undefined</span></li>
</ul>
<p class="note"><span data-conref="../../../assets/snippets/Tag_note.hts"> </span> You can pass the result of <span class="inline3_func"><a data-xref="{title}" href="method_get_index.htm">method_get_index</a></span> to get the static struct of a method&#39;s function.</p>
<p>See: <a data-xref="{title}" href="../../GML_Overview/Structs/Static_Structs.htm">Static Struct</a></p>
<p> </p>
<h4>Syntax:</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
</head>
<body>
<h1><span data-field="title" data-format="default">static_set</span></h1>
<p>This function is used to change the <a href="../../GML_Overview/Structs/Static_Structs.htm">static struct</a> of a struct. You supply the struct to modify, and the new static struct to apply to the first struct, which you can get from other structs or functions using <span class="inline3_func"><a data-xref="{title}" href="static_get.htm">static_get</a></span>.</p>
<p>This function is used to change the <a href="../../GML_Overview/Structs/Static_Structs.htm">static struct</a> of a struct. You supply the struct to modify, and the new static struct to apply to the struct. You can get the static struct of any struct or function using <span class="inline3_func"><a data-xref="{title}" href="static_get.htm">static_get</a></span>.</p>
<p>This will override the static chain for the struct, which is used to determine which constructor(s) a struct belongs to, using <span class="inline3_func"><a data-xref="{title}" href="is_instanceof.htm">is_instanceof</a></span>.</p>
<p class="note"><span data-conref="../../../assets/snippets/Tag_warning.hts"> </span> It&#39;s not recommended to use this function aside from deserialisation use-cases, where you need to load a struct and apply a constructor to it. See: <a data-xref="{text}" href="../../GML_Overview/Structs/Static_Structs.htm#h1">Changing The Static Struct</a></p>
<p class="note"><span data-conref="../../../assets/snippets/Tag_note.hts"> </span> You cannot pass another function to this function. Methods, however, are stored as structs (see: <a data-xref="{text}" href="../../GML_Overview/Method_Variables.htm#h">Methods Are Structs</a>) but the static struct functionality is <strong>disabled</strong> for them, so this function will do nothing when a method is passed.</p>
<p> </p>
<h4>Syntax:</h4>
<p class="code"><span data-field="title" data-format="default">static_set</span>(struct, static_struct);</p>
Expand All @@ -35,12 +36,12 @@ <h4>Syntax:</h4>
</tr>
<tr>
<td>struct</td>
<td><span data-keyref="Type_Struct"><a href="../../../../GameMaker_Language/GML_Overview/Structs.htm" target="_blank">Struct</a></span></td>
<td><span data-keyref="Type_Struct"><a href="../../GML_Overview/Structs.htm" target="_blank">Struct</a></span></td>
<td>The struct to set the static struct for</td>
</tr>
<tr>
<td>static_struct</td>
<td><span data-keyref="Type_Struct"><a href="../../../../GameMaker_Language/GML_Overview/Structs.htm" target="_blank">Struct</a></span></td>
<td><span data-keyref="Type_Struct"><a href="../../GML_Overview/Structs.htm" target="_blank">Struct</a></span></td>
<td>The new static struct to use for the struct</td>
</tr>
</tbody>
Expand Down Expand Up @@ -88,7 +89,7 @@ <h4>Example 2: Assigning a Static Struct to a Pure Data Struct</h4>
<div>Next: <a data-xref="{title}" href="instanceof.htm">instanceof</a></div>
</div>
</div>
<h5><span data-keyref="Copyright Notice">© Copyright YoYo Games Ltd. 2023 All Rights Reserved</span></h5>
<h5><span data-keyref="Copyright Notice">© Copyright YoYo Games Ltd. 2024 All Rights Reserved</span></h5>
</div>
<!-- KEYWORDS
static_set
Expand Down

0 comments on commit eef14b0

Please sign in to comment.