gm-docs-parser 1.0.0

A collection of typings for GameMaker Studio 2 manual pages
Documentation
<?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_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>repeat</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 repeat function"/>
  <meta name="rh-index-keywords" content="repeat"/>
  <meta name="search-keywords" content="if,else,then,?,ternary,conditional"/>
</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="repeat">
        <span>repeat</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>repeat</h1>
  <p>The GameMaker Language has a number of ways that you can perform <i>loops</i>, ie: have a statement or statements iterate over itself a certain number of times. The simplest of these is the <tt>repeat</tt> statement, which has the form:</p>
  <p class="code">repeat (&lt;expression&gt;)<br/>     {<br/>     &lt;statement&gt;;<br/>     &lt;statement&gt;;<br/>     ...<br/>     }</p>
  <p>With <tt>repeat</tt> the given <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> is repeated the number of times indicated by the rounded value of 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>. For
    example, the following creates five balls at random positions:</p>
  <p class="code">repeat (5)<br/>     {
    <br/>     instance_create_layer(random(400), random(400), &quot;Instances&quot;, obj_ball);<br/>     }
  </p>
  <p>This can be very useful to avoid typing out the same code multiple times, or for using arrays, or for counting through a number of operations etc... You are not limited to using a single statement either, and can repeat multiple statements by enclosing
    them within curly brackets <tt>{}</tt>. For example:</p>
  <p class="code">var _x = 32;<br/> repeat (global.p_lives)<br/>     {<br/>     draw_sprite(spr_heart, 0, _x, 32);<br/>     _x += sprite_get_width(spr_heart);<br/>     }</p>
  <p>The above example repeats the statements in the curly brackets for however 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> the &quot;lives&quot; global variable has, and each iteration draws
    the heart sprite at the <span class="inline">_x</span> position, then moves the position along a bit based on the heart sprite width.</p>
  <p><strong>When should you use a <span class="inline">repeat</span> loop?</strong> Anytime that you want to repeat over one or more statements a fixed number of times without any specific need to maintain a count of the iterations.</p>
  <p>It is worth noting that you can use the special <span class="inline"><a href="break.htm">break</a></span> and <span class="inline"><a href="continue.htm">continue</a></span> statements within a
    <font face="Lucida Console"><span style="font-size: 16px;"><b>repeat</b></span></font> loop too. 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 i = 0;<br/> var temp = 0;<br/> repeat (10)<br/>     {<br/>     temp += array[i];<br/>     if (temp &gt; max_total)<br/>         {<br/>         break;<br/>         }<br/>     else<br/>         {<br/>         i += 1;<br/>         }<br/>     }</p>
  <p>The above code loops through 10 <a href="../Arrays.htm">array</a> values and adds them to a local variable. If the total of the local variable is greater than the given value for <span class="inline">max_total</span>, then the loop is terminated using
    break, otherwise the loop will continue. </p>
  <p>An example of using <span class="inline">continue</span> in a <span class="inline">repeat</span> loop would be:</p>
  <p class="code">repeat(10)<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, &quot;Instances&quot;, obj_Enemy);<br/>     }</p>
  <p>This code will repeat 10 times, generating a random room position then checking 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&#39;t then an instance of the object <span class="inline">obj_Enemy</span> is created at the random position.</p>
  <p>For more examples of loop functions please see the sections on <a href="while.htm"><tt>while</tt></a>, <a href="do___until.htm"><tt>do... until</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="while.htm">while</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
repeat
-->
  <!-- TAGS
repeat
-->

</body></html>