<?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.2.0_6";
//]]></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>switch</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 the switch function"/>
<meta name="rh-index-keywords" content="switch,case"/>
<meta name="search-keywords" content="switch,default"/>
</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="switch">
<span>switch</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>switch</h1>
<p>In a number of situations you want to let your instances complete different actions depending on a particular value. You can do this using a number of consecutive <a href="If_Else_and_Conditional_Operators.htm"><tt>if / else</tt></a> statements, but
when the possible choices gets above two or three it is usually easier to use the <tt>switch</tt> statement. A <tt>switch</tt> statement has the following form:</p>
<p class="code">switch (<expression>)<br/> {
<br/> case <constant1>: <statement1>; ... ; break;<br/> case <constant2>: <statement2>; ... ; break;<br/> ...
<br/> default: <statement>;<br/> }
</p>
<p>This works as follows:</p>
<ul class="colour">
<li>First the <a class="tooltip" title="An expression is a combination of one or more constants, variables, operators, and/or functions that are interpreted according to particular rules of precedence and association to return another value. A simple expression would be (5 + 5), which returns 10.">expression</a> is executed.</li>
<li>Next it is compared with the results of the different <a href="../Variables/Constants.htm">constants</a> after each of the <span class="inline">case</span> <a class="tooltip" title="In programming, a statement is a single line of code written legally in a programming language that expresses an action to be carried out. A statement might have internal components of its own, including expressions, operators and functions. An example of a statement is A = B + 5. A GameMaker Studio 2 program is nothing but a sequence of one or more statements that together perform a task (like move the player). ">statement</a><span class="glossextra">s</span>.
When we say "constant" what we mean is that the value in the case cannot be a variable expression and must be a fixed value of any <a href="../Data_Types.htm">data type</a>, like "fight" or 3 or the keyword <span class="inline"><a href="../Instance_Keywords.htm">noone</a></span>. </li>
<li>The execution continues after the first <span class="inline">case</span> statement with the correct value, <i>until a <a href="break.htm">break</a> statement is encountered</i>.</li>
<li>If no case statement has the right value, then the <span class="inline">default</span> statement will be executed (it is not required to have a <span class="inline">default</span> statement, and if none is supplied then no action will be taken).</li>
</ul>
<p>A simple example of using a switch statement would be something like this:</p>
<p class="code">switch (global.state)<br/> {<br/> case "alert":<br/> if (instance_exists(obj_Player))<br/> {<br/> if (point_distance(x, y, obj_Player.x, obj_Player.y) < 100)<br/> {<br/>
global.state = "chase";<br/> }<br/> }<br/> break;<br/> case "chase":<br/> var _lost = false;<br/> if (instance_exists(obj_Player))<br/> {<br/> move_towards_point(obj_Player.x,
obj_Player.y, 2);<br/> if (point_distance(x, y, obj_Player.x, obj_Player.y) > 100)<br/> {<br/> _lost = true;<br/> }<br/> }<br/> else _lost = true;<br/> if (_lost)<br/> {<br/> speed = 0;<br/> global.state = "alert";<br/> }<br/> break;<br/> }</p>
<p>Here we have a global variable that holds a string value which is used to set the behaviour (state) of the instance. In this example the instance simply switches between two states, but it is extremely easy to expand this to include more states by adding
further <span class="inline">case</span> statements for additional state strings, like "fight" or "die", etc...</p>
<p>Note that multiple <tt>case</tt> statements can be used to execute the same statement, as the <tt>break</tt> is not always required for each and every <span class="inline">case</span>. If there is no <span class="inline">break</span> statement for a
particular <span class="inline">case</span>, the execution simply continues with the code for the next case, eg:</p>
<p class="code">switch (keyboard_key)<br/> {
<br/> case vk_left:<br/> case ord("A"):<br/> x -= 4;<br/> break;
<br/> case vk_right:<br/> case ord("D"):<br/> x += 4;<br/> break;
<br/> case vk_up:<br/> case ord("W"):<br/> y -= 4;<br/> break;
<br/> case vk_down:<br/> case ord("S"):<br/> y += 4;<br/> break;
<br/> }
</p>
<p>The above code uses <tt>switch</tt> to check for a keyboard event and then compares that to each <span class="inline">case</span> listed. If it meets any of the required values then the corresponding code is executed. Note how in the code we have used
the way that <tt>switch</tt> can check multiple <span class="inline">cases</span> and continue if no <span class="inline">break</span> is encountered to permit various keys to be used to get the same result. Note that each <span class="inline">case</span> can have it's own code, and so you can set up a sort of "inhertitance" system where more than one <span class="inline">case</span> and it's code will run consecutively woth the next until a break is reached depending on the value
of the initial <span class="inline">switch</span> expression.</p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="../Language_Features.htm">Language Features</a></div>
<div style="float:right">Next: <a href="break.htm">break</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
switch
-->
<!-- TAGS
switch
-->
</body></html>