<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
</script>
<style type="text/css">
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
</style>
<script type="text/javascript">
gRootRelPath = "../..";
gCommonRootRelPath = "../..";
gTopicId = "9.1.1";
<script type="text/javascript" src="../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Variables And Variable Scope</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Page outlining variables and their scoping in GML"/>
<meta name="rh-index-keywords" content="Variables And Variable Scope"/>
<meta name="search-keywords" content="variables,scope,local variables,instance variables,global variables"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="Variables And Variable Scope">
<span>Variables And Variable Scope</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<h1>Variables And Variable Scope</h1>
<p>Like any programming language <b>GML</b> uses <i>variables</i> as the basic unit for most programming operations. Variables are used to store information in the devices memory for later (or instant) use, and they are given a name so that you can refer
to them in runtime and script functions. A variable in <b>GML</b> can store many different <a href="Data_Types.htm"><strong>data types</strong></a>, like a real number (eg: 100, 2.456575, -56 etc...), a string (eg: "Hello world!"), an integer
(eg: 1, 556, -7), or a boolean (<tt>true</tt> or <tt>false</tt>), as well as other things:</p>
<p class="code">var _num = 126.4545;<br/> var _str = "Hello World";<br/> new_num = _num * 100;<br/> global.my_string = _str + " I said";</p>
<p>You can also use variables to hold the values returned from functions, for example:</p>
<p class="code">var _id = instance_nearest(x, y, obj_Tree);<br/> root = sqrt(1000);<br/> global.str = string_upper("Hello World");</p>
<p>So, a variable is something that we can name and use to store a value for later use in one or more operations. A great "real world" example of a variable is <b>pi 𝜋 </b>... it is a variable that everyone know and it holds the value 3.14159265(etc...).
Why do we have it in our language? Well, it's much easier to say to someone "pi" than "three point one four one five nine two six five"! Naming things like this makes life a lot simpler and it also means that should the value
of that variable ever change, we don't have to change the number everywhere as the variable <i>name</i> is still the same.</p>
<p>When forming variables in <b>GML</b> it must have a name that starts with a letter or the underscore symbol "_" and can contain only letters, numbers, and the underscore symbol '_' with a maximum length of 64 symbols. So, valid variables
are things like <tt>fish</tt>, <tt>foo_bar</tt>, <tt>num1</tt>, or <tt>_str</tt>, while invalid variables would be <tt>6fish</tt>, <tt>foo bar</tt>, or <tt>*num</tt>.</p>
<p>Now, In many programming languages you need to create a variable "assignment" before you can use it. This basically means that you tell the computer the name you wish to use for the variable and assign it an initial value. The variable is
then given a place in memory to store the value or perform operations on it. Assigning a variable takes the form of:</p>
<p class="code"><variable> = <expression>;</p>
<p>An expression can be a simple value but can also be more complicated, so, rather than assigning a value to a variable, one can also add a value to the current value of the variable using <span class="inline"><b>+=</b></span>, for example:</p>
<p class="code">a = 100; // Assigning a simple value<br/> b = 200;<br/> c = 300;<br/> a += b; // Assigning with operation<br/> a = b + c; // Assigning with expression</p>
<p class="note"><b>NOTE</b>: The GameMaker Language will also accept "<span class="inline">:=</span>" for assignments, although this is not typically the most common way to do it:</p>
<p class="code"><variable> := <expression>;</p>
<p>Similarly, you can subtract using <span class="inline"><b>-=</b></span>, multiply using <span class="inline"><b>*=</b></span>, divide using <span class="inline"><b>/=</b></span>, or use bitwise operators using <span class="inline"><b>|=</b></span>,
<span class="inline"><b>&=</b></span>, or <span class="inline"><b>^=</b></span>. You can also add or subtract <i>one</i> from a value using <span class="inline"><b>++</b></span>, <span class="inline"><b>--</b></span>. For further information see
the section on <a href="Expressions_And_Operators.htm">Expressions And Operators</a>.</p>
<p>Note that you <i>cannot</i> do the following (or any variation):</p>
<p class="code">a = b = c = 4;</p>
<p>And instead it should be done as:</p>
<p class="code">a = 4;<br/> b = 4;<br/> c = 4;</p>
<p>The variable assignments shown above are all <strong>instance </strong>variables, however there are actually three other main variable categories when you program with GameMaker Studio 2 and each has its own <strong>scope </strong>(which can be considered
as its area of operation, or reach). The different kinds of variables and their scope are all outlined in the following pages:</p>
<ul class="colour">
<li><a href="Variables/Local_Variables.htm">Local Variables</a></li>
<li><a href="Variables/Instance_Variables.htm">Instance Variables</a></li>
<li><a href="Variables/Global_Variables.htm">Global Variables</a></li>
<li><a href="Variables/Constants.htm">Constants</a></li>
</ul>
<p>The GameMaker Language also has multiple different built-in variables that can have any of the above mentioned scopes (except <i>local</i>). These variables are special as they are included by default as part of the objects and the rooms in the game
world. Some built in global variables are listed in the section mentioned above, and the different parts of the manual for sprites, rooms, objects, etc... also outline the built-in variables available in each case. Examples of such built-in instance
variables would be:</p>
<p class="code"><tt><a href="../GML_Reference/Asset_Management/Sprites/Sprite_Instance_Variables/sprite_index.htm">sprite_index</a></tt><br/>
<a href="../GML_Reference/Asset_Management/Paths/Path_Variables/path_index.htm"><tt>path_scale</tt></a><br/>
<a href="../GML_Reference/Asset_Management/Instances/Instance_Variables/speed.htm"><tt>speed</tt></a></p>
<p>And examples of built-in global variables would be:</p>
<p class="code"><a href="../GML_Reference/Cameras_And_Display/Cameras_And_Viewports/view_xport.htm"><tt>view_xport</tt></a><br/>
<a href="../GML_Reference/OS_And_Compiler/GM_version.htm"><tt>GM_version</tt></a><br/>
<a href="../GML_Reference/Asset_Management/Rooms/room.htm"><tt>room</tt></a></p>
<p>Most built-in variables can be changed and set like other variables, and some can even be <a href="Arrays.htm">arrays</a>, only you don't have to set them to create them like you would a regular variable as they will already be initialised to a
default value.</p>
<p>Finally, there are a number of functions that are dedicated to setting, getting or checking variables in some way, available from the following page:</p>
<ul class="colour">
<li><a href="../GML_Reference/Variable_Functions/Variable_Functions.htm">Variable Functions</a></li>
</ul>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="GML_Overview.htm">GML Overview</a></div>
<div style="float:right">Next: <a href="Arrays.htm">Arrays</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
</body></html>