<?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_4";
//]]></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>do / until</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 do / until functions"/>
<meta name="rh-index-keywords" content="do,until"/>
<meta name="search-keywords" content="do,until"/>
</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="do / until">
<span>do / until</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>do / until</h1>
<p>A <tt>do</tt> function is another way of iterating over one or more <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> multiple times, and is really a "<tt>do... until</tt>" statement as
you cannot have one without the other since you are telling GameMaker Studio 2 to do something until a specific <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> returns <tt>true</tt>. It has this form:</p>
<p class="code">do<br/> {
<br/> <statement>;
<br/> <s <span class="inline"></span>tatement>;<br/> ...
<br/> }
<br/> until (<expression>);</p>
<p>The statement (which can be a code block of multiple statements within curly brackets <tt>{}</tt>) is executed until the expression is found to be <tt>true</tt>, and the initial statement is <strong>always executed at least once</strong>. Below you
can find an example of a typical way to use <tt>do... until</tt>:</p>
<p class="code">do<br/> {
<br/> x = random(room_width);<br/> y = random(room_height);<br/> }
<br/> until (place_free(x, y));</p>
<p>The above code tries to place the current object at a free position and will set the x/y variables at least once, and then perform as many <a class="tooltip" title="An iteration is a single pass through a set of operations in your project code. One form of iteration in computer programming is via loops. A loop will repeat a certain segment of code until a condition is met and it can proceed further. Each time the computer runs a loop, it is known as an iteration. In simple terms, iteration is the process of repeating a particular snippet of code over and over again to perform a certain action.">iteration</a><span class="glossextra">s</span> as required
until the <tt>place_free()</tt> expression returns <tt>true</tt>.</p>
<p><strong>When should you use a <span class="inline">do</span> / <span class="inline">until</span> loop?</strong> It should be used any time you want to repeat one or more statements, but don't actually know how many times it has to repeat, and want
to ensure that the statements are run <em>at least once</em> before the loop ends.</p>
<p>You can also use the <a href="break.htm"><span class="inline">break</span></a> and <a href="continue.htm"><span class="inline">continue</span></a> statements within your <span class="inline">do</span> loops. Using <span class="inline">break</span> will
immediately exit the loop and move on to any code that is in the event or function after the loop should have finished, eg:</p>
<p class="code">var _id = noone;<br/> do
<br/> {<br/> _id = list[| 0];<br/> if instance_exists(_id)<br/> {<br/> _break;<br/> }<br/> ds_list_delete(list, 0);<br/> }<br/> until (ds_list_empty(list));<br/> target = _id;</p>
<p>In the above code, we create a local variable and set it to hold the keyword <a href="../Instance_Keywords.htm">noone</a>. We then perform a <span class="inline">do / until</span> loop checking the first position of a DS list to see if it holds a valid
instance ID, and if it does then we <span class="inline">break</span> the loop, otherwise the value for the list position is deleted. After the loop is terminated (either by the <span class="inline">break</span> or because the list is empty) the local
variable value is then assigned to the instance variable <span class="inline">target</span>.</p>
<p>An example of using continue in a <span class="inline">do / until</span> loop would be:</p>
<p class="code">do<br/> {
<br/> var _x = random(room_width);<br/> var _y = random(room_height);<br/> if (instance_position(_x, y, obj_Enemy)<br/> {
<br/> continue;
<br/> }
<br/> instance_create_layer(_x, _y, "Instances", obj_Enemy);<br/> }
<br/> until (instance_count(obj_Enemy) >= 10);</p>
<p>This code will generate a random room position then check if an instance of the object <span class="inline">obj_Enemy</span> exists at that position. If it does, the current loop iteration is terminated using <span class="inline">continue</span> and
a new iteration is started, and if it doesn't then an instance of the object <span class="inline">obj_Enemy</span> is created at the random position. The loop will only terminate when there are 10 or more instances of the object in the room.</p>
<p>One final note: be careful with your <tt>do</tt> loops, as you can easily make them loop forever, in which case your game will hang and not react to any user input anymore and they will have to force close it.</p>
<p>For more examples of loop keywords please see the sections on <a href="repeat.htm"><tt>repeat</tt></a>, <a href="while.htm"><tt>while</tt></a>, and <a href="for.htm"><tt>for</tt></a>.</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="for.htm">for</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
do
until
-->
<!-- TAGS
do
until
-->
</body></html>