Skip to content

Commit

Permalink
[General]Standalone ternary operations don't work despite manual clai…
Browse files Browse the repository at this point in the history
…ming otherwise

Closes YoYoGames/GameMaker-Bugs#2682
Kept the statement as an example on how the ternary operator cannot be used
  • Loading branch information
YYBartT committed Jan 9, 2024
1 parent 8018e26 commit 9194590
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</head>
<body>
<!--<div class="body-scroll" style="top: 150px;">-->
<h1>if / else and Conditional Operators</h1>
<h1><span data-field="title" data-format="default">if / else and Conditional Operators</span></h1>
<p>A fundamental feature of most programming languages is the ability to ask a simple question that gives a boolean <span class="inline">true</span> or <span class="inline">false</span> answer, and in GML this is achieved using the <span class="inline">if</span> keyword. A simple <span class="inline">if</span> condition takes an <a class="glossterm" data-glossterm="expression" href="#">expression</a> and will perform one or more <a class="glossterm" data-glossterm="statement" href="#">statement</a><span class="glossextra">s</span> if the expression resolves as <span class="inline">true</span>, with the following basic form:</p>
<p class="code">if (&lt;expression&gt;)<br />
{<br />
Expand All @@ -23,7 +23,7 @@ <h1>if / else and Conditional Operators</h1>
    ...<br />
}</p>
<p><br />
Here you are saying that if an expression resolves as <span class="inline">true</span> then do something. Note that the &quot;then&quot; part of the condition is <em>implicit</em>, but there is a <span class="inline">then</span> keyword that can be used (although it&#39;s almost always omitted), so you can also create conditionals like this:</p>
Here you are saying that if an expression resolves to <span class="inline">true</span> then do something. Note that the &quot;then&quot; part of the condition is <em>implicit</em>, but there is a <span class="inline">then</span> keyword that can be used (although it&#39;s almost always omitted), so you can also create conditionals like this:</p>
<p class="code">if (&lt;expression&gt;) then<br />
{<br />
    &lt;statement&gt;;<br />
Expand All @@ -39,7 +39,7 @@ <h1>if / else and Conditional Operators</h1>
{<br />
    &lt;statement&gt;;<br />
}</p>
<p>In this case the expression will be evaluated, and if it evaluates as <span class="inline">false</span>, the statement after <span class="inline">else</span> is executed, otherwise the initial statement is executed (it&#39;s <span class="inline">true</span>).</p>
<p>In this case the expression will be evaluated, and if it evaluates to <span class="inline">false</span>, the statement after <span class="inline">else</span> is executed, otherwise the initial statement is executed (it&#39;s <span class="inline">true</span>).</p>
<p class="note"><span class="note">NOTE</span> In the GameMaker language any value that is less than or equal to 0 will evaluate as <span class="inline">false</span>, while any value that is greater than 0 will evaluate as <span class="inline">true</span>.</p>
<p>It is a good habit to always put brackets around the expressions and curly brackets {} around the statements in the <span class="inline">if</span> (otherwise only the first statement will be executed), and take a new line in the block for each statement, for example:</p>
<p class="code">// This will work<br />
Expand Down Expand Up @@ -96,14 +96,14 @@ <h1>if / else and Conditional Operators</h1>
<p class="code">var temp_x = (x &lt; (room_width / 2)) ? 32 : (room_width - 32);</p>
<p>The above code will check the value of &quot;x&quot; against the value of half the room width and then if it is less it will set &quot;<span class="inline">temp_x</span>&quot; to 32 otherwise &quot;<span class="inline">temp_x</span>&quot; will be room width - 32. Here are a few more examples of use:</p>
<p class="code">draw_text(x, y, &quot;The fee is &quot; + (global.Member ? &quot;$2.00&quot; : &quot;$10.00&quot;));<br />
path_start(((global.level &gt; 10) ? path_hard : path_easy;), 2, path_action_reverse, true);<br />
(--hp &lt;= 0) ? instance_destroy() : score += 10;</p>
path_start(((global.level &gt; 10) ? path_hard : path_easy;), 2, path_action_reverse, true);</p>
<p>Note that conditional operations must be part of an expression and cannot go on the left-hand side of an assignment. For example, the following statement doesn&#39;t work: </p>
<p class="code">(--hp &lt;= 0) ? instance_destroy() : score += 10;</p>
<p>It is worth noting too that you can nest conditional operations but that if you do then each operation will need to be enclosed in brackets, for example:</p>
<p class="code">var c = a ? &quot;foo&quot; : (b ? &quot;bar&quot; : &quot;whee&quot;); // Correct<br />
var c = a ? &quot;foo&quot; : b ? &quot;bar&quot; : &quot;whee&quot;;   // Will cause an error</p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
Expand Down

0 comments on commit 9194590

Please sign in to comment.