<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
//<![CDATA[
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">/*<![CDATA[*/
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
/*]]>*/</style>
<script type="text/javascript">//<![CDATA[
gRootRelPath = "../../..";
gCommonRootRelPath = "../../..";
gTopicId = "9.1.1.0_2";
//]]></script>
<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>Instance Variables</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 explaining instance variables"/>
<meta name="rh-index-keywords" content="Instance Variables"/>
<meta name="search-keywords" content="instance 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="Instance Variables">
<span>Instance Variables</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>
<!--<div class="body-scroll" style="top: 150px;">-->
<h1>Instance Variables</h1>
<p>An <strong>instance </strong>variable is created within an instance of an object and is considered unique to that instance - ie: many instances of the same object can have the same variable, but each variable can hold a different value as they are <em>unique </em>to
each instance. But <i>how</i> is an instance variable created? Well, you create new variables by simply assigning a value to them as shown in this small example (this is called <em>declaring</em> the variable):</p>
<p class="code">potions = 12;<br/> life = 100;<br/> name = "Jock MacSweeney";<br/> strength = 5.5;<br/> armour = -2;</p>
<p>As you can see you just have to give the name and then a value (the value can be any <a href="../Data_Types.htm">data type</a>) to set that variable and have it ready for use within an instance of the object you are coding for (note that the value can
come from the return value of a function or the result of an operation between other variables, etc...). These variables can then be used and modified in a number of ways from within the instance, for example this code could be in a collision event
and used to take an amount off of the variable "<span class="inline">life</span>":</p>
<p class="code">life -= 5 + armour;</p>
<p>If "<span class="inline">life</span>" is at 100 it will now have a value of 97 (100 - (5 + -2) = 97). Now, that's a simple example, and you <i>could</i> replace "<span class="inline">armour</span>" for the actual value of -2,
but what happens if that value is <a class="tooltip" title="In computer programming, the term hard-coded is used to describe code that is considered fixed and not likely to change. Hardcoded features are built into hardware or software in such a way that they cannot be modified later on. For example, if you are making a game and 'hard-code' the player health to 10, then you would be using the value 10 throughout the game code rather than using a variable.">hard-coded</a> in multiple places and then you decide to change it? You would have to go through ALL your code and change every -2 to whatever the new value
is, which is time consuming and very error prone! But if you use a variable, all you have to do is reassign it a new value and the code will automatically use that new value from then onwards, making things far more flexible and far easier to fix
should there be a problem. It should also be noted that even if a value is not going to change it is far easier to remember what a variable called "<span class="inline">life</span>" means rather than just looking at a number.</p>
<p>GameMaker Studio 2 has a collection of "built in" instance variables too, so you should be aware of them as you may name one of your own instance variables the same or wish to have your own global variable with the same name and wonder why
you are getting errors. They are easy to spot, however, as they are shown in a different colour in the code editor and also come up in auto-complete and are shown in bar at the bottom of the <a href="../../../The_Asset_Editors/Scripts.htm">code editor</a>.</p>
<p>There are quite a few <a href="../Runtime_Functions.htm">runtime functions</a> designed to help you when dealing with instance variables, which are all listed in the following section:</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="../Variables_And_Variable_Scope.htm">Variables And Variables Scope</a></div>
<div style="float:right">Next: <a href="Global_Variables.htm">Global Variables</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
instance variables
-->
<!-- TAGS
instance_variables
-->
</body></html>