Skip to content

Commit

Permalink
[General] Minor improvements to structs/constructors vs objects section
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreetsinghmatharoo committed Jan 18, 2024
1 parent 4063eaf commit e7813b2
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Manual/contents/GameMaker_Language/GML_Overview/Structs.htm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!--<div class="body-scroll" style="top: 150px;">-->
<h1><span data-field="title" data-format="default">Structs &amp; Constructors</span></h1>
<p>A <strong><a id="struct"></a>struct </strong>is a variable that holds a collection of other variables. The variables that a struct holds can be of any <a href="Data_Types.htm">data type</a> previously mentioned and these variables can be read from and written to after the initial struct declaration, and you can also add more variables to a struct after it has been declared.</p>
<p class="note"><span data-conref="../../assets/snippets/Tag_note.hts"> </span> There are subtle differences between <span data-field="title" data-format="lower-alpha">structs &amp; constructors</span> and <a data-xref="{title}" href="../../Quick_Start_Guide/Objects_And_Instances.htm">Objects And Instances</a>. See <a data-xref="{text}" href="Structs.htm#constructors_structs_vs_objects_instances">Constructors &amp; Structs vs. Objects &amp; Instances</a> for more info.</p>
<p class="note"><span data-conref="../../assets/snippets/Tag_note.hts"> </span> There are subtle differences between <span data-field="title" data-format="default">Structs &amp; Constructors</span> and <a data-xref="{title}" href="../../Quick_Start_Guide/Objects_And_Instances.htm">Objects And Instances</a>. See <a data-xref="{text}" href="Structs.htm#constructors_structs_vs_objects_instances">Constructors &amp; Structs vs. Objects &amp; Instances</a> for more info.</p>
<p>The variables used in a struct should follow the usual variable naming scheme, i.e.: they cannot start with a number and should only be made up of alphanumeric characters and the underscore &quot;_&quot; character, and also note that the contents of a struct are <em>independent of the instance or function that created it</em>, and as such you can - if you wish - use built-in variable names such as <span class="inline2"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Sprites/Sprite_Instance_Variables/image_index.htm">image_index</a></span> or <span class="inline2"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/Instance_Variables/x.htm">x</a></span> and <span class="inline2"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/Instance_Variables/y.htm">y</a></span>.</p>
<p>After the initial creation, structs have no processing overhead while they exist, although they <i>will</i> take up space in memory. The struct syntax is as follows:</p>
<p class="code"><i>&lt;variable&gt;</i> =<br />
Expand Down Expand Up @@ -264,9 +264,9 @@ <h2>String Output</h2>
<h2 id="constructors_structs_vs_objects_instances">Constructors &amp; Structs vs. Objects &amp; Instances</h2>
<p>Constructors and structs are similar to <a data-xref="{title}" href="../../Quick_Start_Guide/Objects_And_Instances.htm">Objects And Instances</a>, as they also hold variables and methods that can be executed. There are, however, a couple of important differences between them: </p>
<ul class="Disc">
<li>A struct is created by calling a constructor function using the <span class="inline2"><a data-xref="{title}" href="Language_Features/new.htm">new</a></span> operator, whereas an instance is created from an object using <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/instance_create_depth.htm">instance_create_depth</a></span> or <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/instance_create_layer.htm">instance_create_layer</a></span>.</li>
<li>Structs are just variables and don&#39;t have events. <span data-keyref="GameMaker Name">GameMaker</span> executes events for all instances, not for structs. Compared to an object&#39;s event, a struct&#39;s method never executes of its own, you have to call it yourself from somewhere in your game&#39;s code. This can be from an <a href="../../The_Asset_Editors/Object_Properties/Object_Events.htm">Object Event</a>, from a <a href="../GML_Reference/Time_Sources/Time_Sources.htm">Time Source</a> or from a more specific place where you can &quot;tap into&quot; <span data-keyref="GameMaker Name">GameMaker</span>&#39;s game loop, such as the layer and camera begin/end scripts.</li>
<li>Structs can have static variables, objects cannot. <a data-xref="{title}" href="../../The_Asset_Editors/Object_Properties/Object_Variables.htm">Object Variables</a> are not the equivalent of static variables; they don&#39;t belong to the object, rather they provide default values for <a data-xref="{title}" href="Variables/Instance_Variables.htm">Instance Variables</a> that are assigned to instances created from that object before the Create event runs.</li>
<li>A struct is created by calling a constructor function using the <span class="inline2"><a data-xref="{title}" href="Language_Features/new.htm">new</a></span> operator, by creating a struct literal <span class="inline2">{}</span> or returned by some function. An instance, on the other hand, is created from an object using <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/instance_create_depth.htm">instance_create_depth</a></span> or <span class="inline3_func"><a data-xref="{title}" href="../GML_Reference/Asset_Management/Instances/instance_create_layer.htm">instance_create_layer</a></span>.</li>
<li>Structs are just variables and don&#39;t have events. <span data-keyref="GameMaker Name">GameMaker</span> executes events for all instances, but not for structs. Compared to an object&#39;s event, a struct&#39;s method never executes on its own, you have to call it yourself in your game&#39;s code. This can be from an <a href="../../The_Asset_Editors/Object_Properties/Object_Events.htm">Object Event</a>, from a <a href="../GML_Reference/Time_Sources/Time_Sources.htm">Time Source</a> or from a more specific place where you can &quot;tap into&quot; <span data-keyref="GameMaker Name">GameMaker</span>&#39;s game loop, such as the layer and camera begin/end scripts.</li>
<li>Structs can have static variables, object instances cannot. <a data-xref="{title}" href="../../The_Asset_Editors/Object_Properties/Object_Variables.htm">Object Variables</a> are not the equivalent of static variables; they don&#39;t belong to the object, rather they provide default values for <a data-xref="{title}" href="Variables/Instance_Variables.htm">Instance Variables</a> that are assigned to instances created from that object before the Create event runs.</li>
</ul>
<h2>Struct Functions</h2>
<p>Finally, there are a number of runtime functions that you can use on structs to get the variables they contain as well as a few other things. You can find them under <a data-xref="{text}" href="../GML_Reference/Variable_Functions/Variable_Functions.htm#struct_functions">Struct Functions</a>.</p>
Expand Down

0 comments on commit e7813b2

Please sign in to comment.