If-statements

If you want to run a piece of code only when certain conditions are met, you should use an if-statement:

for each player do
   if current_player.team == team[0] then
      --
      -- ... do something ...
      --
   end
end

An if-statement typically consists of the word "if," followed by one or more conditions. Each condition is either a comparison statement or a function call. Conditions must be separated from each other by the word "or" or the word "and." A condition may be prefixed with the word "not". After the conditions, you must write the word "then".

Conditions are executed in sequence. You cannot adjust the order of operations e.g. by using parentheses. If you want to test an expression like "(A and B) or (C and D)," then you have two options:

Alt and alt-if

Megalo does not have any built-in "else" or "elseif" statements. However, the Megalo dialect used by ReachVariantTool offers similar statements as a shortcut. The two pieces of code below behave identically (in fact, the former compiles to the latter):

if x == 1 then
   run_function_a()
altif y == 1 then
   run_function_b()
alt
   run_function_c()
end
if x == 1 then
   run_function_a()
end
if x != 1 and y == 1 then
   run_function_b()
end
if x != 1 and y != 1 then
   run_function_c()
end

The main difference between this construct and "else(if)" is that if, in our example above, if x is initially 1 but run_function_a changes the value of x to something other than 1, then both the "x is 1" and "x is not 1" branches may be able to run.

If you need your branches to be mutually exclusive as they would be in an "else(if)" statement, then you will need to use a variable to maintain state:

alias ran_any = global.number[0]
ran_any = 0
if x == 1 then
   ran_any = 1
   run_function_a()
end
if ran_any == 0 then
   if y == 1 then
      ran_any = 1
      run_function_b()
   end
   if ran_any == 0 then
      if y != 1 then
         run_function_c()
      end
   end
end

The example above may be easier to read if you consider that else(if) statements can be written as nested else and if statements:

if x == 1 then
   run_function_a()
else
   if y == 1 then
      run_function_b()
   else
      run_function_c()
   end
end

Else and else-if

As mentioned in the section above, the Megalo script engine doesn't have any built-in support for "else(if)" statements. It would be theoretically possible for the compiler to support such statements by automating the workaround described above, but this has not been implemented as of this writing, and it would come with some ugly limitations involving the nesting of else(if) statements.

Micro-optimizations

In Megalo, most code blocks compile to a single "trigger," or compiled block. However, if-statements are a common exception. Conditions can be compiled directly into their containing blocks, and they act as a kind of conditional exit: the block stops running early if the condition is not met. This means that if an if-statement is the last element in its containing block, then it can be "inlined" into that block's compiled trigger, but if the if-statement isn't the last element, then it has to be compiled as a nested trigger.

In other words: the following two pieces of code do the same thing, but the second one uses one less trigger and so contributes less to script size limits:

for each player do
   if current_player.team == team[0] then
      game.show_message_to(current_player, none, "Your objective is under attack!")
   end
   script_widget[0].set_visibility(current_player, true)
end
for each player do
   script_widget[0].set_visibility(current_player, true)
   if current_player.team == team[0] then
      game.show_message_to(current_player, none, "Your objective is under attack!")
   end
end