You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(general): Automatically remove unused assets when compiling - describe how to prevent
YoYoGames/GameMaker-Bugs#9011
* "MarkTagAsUsed" pragma already added on the Game Options page (in YoYoGames/GameMaker-Bugs#8313)
* Added a note on how referencing an asset only through asset_get_index() will cause it to be removed by the compiler and how to prevent using the "MarkTagAsUsed" pragma
* Added a second code example that shows how to use asset_get_index() in combination with the "MarkTagAsUsed" pragma
<p>This function gets the handle for a game asset from its name.</p>
20
-
<p>If the asset is not found, the function will return a value of -1, otherwise it will return the handle for the asset being checked. This handle can then be used in other functions as you would any other handle, like <spanclass="inline2"><adata-xref="{title}" href="../Sprites/Sprite_Instance_Variables/sprite_index.htm">sprite_index</a></span> or <spanclass="inline2"><adata-xref="{title}" href="../Paths/Path_Variables/path_index.htm">path_index</a></span>, for example. Please note that although this function can be used to reference assets from strings (see example below), you should always make sure that the asset exists before using it otherwise you may get errors that will crash your game.</p>
20
+
<p>If the asset is not found, the function will return a value of -1, otherwise it will return the handle for the asset being checked. This handle can then be used in other functions as you would any other handle, like <spanclass="inline2"><adata-xref="{title}" href="../Sprites/Sprite_Instance_Variables/sprite_index.htm">sprite_index</a></span> or <spanclass="inline2"><adata-xref="{title}" href="../Paths/Path_Variables/path_index.htm">path_index</a></span>, for example.</p>
21
+
<h3>Usage Notes</h3>
22
+
<ulclass="colour">
23
+
<li>Although this function can be used to reference assets from strings (see the first example below), you should always make sure that the asset exists before using it. Otherwise, you may get errors that will crash your game.</li>
24
+
<li>If the only reference to an asset in your code is through this function (as a string) then the asset will not be known to the compiler, which will remove it if <strong>Automatically remove unused assets when compiling</strong> is enabled in the <adata-xref="{title}" href="../../../../Settings/Game_Options.htm">Game Options</a> (the default). Consequently, the asset will not be found at runtime and the function will return -1. To prevent this you can assign this asset a tag in <adata-xref="{title}" href="../../../../Introduction/The_Asset_Browser.htm">The Asset Browser</a> and mark all assets with this tag as "used" using the "MarkTagAsUsed" <spanclass="inline3_func"><adata-xref="{title}" href="../../OS_And_Compiler/gml_pragma.htm">gml_pragma</a></span>. See the second example.</li>
<p>The above code first marks all assets with an <spanclass="inline2">"include_me"</span> tag as "used" in a call to <spanclass="inline3_func"><adata-xref="{title}" href="../../OS_And_Compiler/gml_pragma.htm">gml_pragma</a></span> in a script asset. In an object's Create event, a random number from 0 to 7 is chosen for the current instance with <spanclass="inline3_func"><adata-xref="{title}" href="../../Maths_And_Numbers/Number_Functions/irandom.htm">irandom</a></span> and <spanclass="inline3_func"><spandata-field="title" data-format="default">asset_get_index</span></span> is called to retrieve the NPC sprite with that suffix number. Finally, in the Draw event, the chosen sprite is drawn at the instance's position.<br/>
66
+
For this code to work correctly the project must contain 8 sprite assets named <spanclass="inline2">spr_npc_0</span>, <spanclass="inline2">spr_npc_1</span>, <spanclass="inline2">spr_npc_2</span>, ..., <spanclass="inline2">spr_npc_7</span>, each of them tagged <spanclass="inline2">"include_me"</span>. In this case the <spanclass="inline3_func"><adata-xref="{title}" href="../Sprites/Sprite_Manipulation/sprite_exists.htm">sprite_exists</a></span> check isn't needed and can be left out.</p>
67
+
<p></p>
52
68
<p></p>
53
69
<divclass="footer">
54
70
<divclass="buttons">
55
71
<divclass="clear">
56
-
<divstyle="float:left">Back:<ahref="Assets_And_Tags.htm">Assets And Tags</a></div>
0 commit comments