Jump to content
feldmaus

Difference between Parantheses () and Braces{}

Recommended Posts

Hello,

 

can someone explain the differences and their main application? I have problems to use them in nested conditions.

 

Thank you

Share this post


Link to post
Share on other sites

What's up with all these "I got a problem" or "Does not work" posts lately that lack any information about what doesn't work?

If you go through the effort to click "post new thread" then at least provide some basic info.

 

Cheers

  • Like 2

Share this post


Link to post
Share on other sites

Parenthesis are used to separate, conditions (logical statements (true,false)), or mathematical expressions.

Using parenthesis can provide order of operation to both the above for when it is not clear. e.g

alive player1 && alive player2 || alive player3

Could be logically (ignoring standard order of operations) read as..

  1. If player1 AND player2 are alive OR player3 is alive
  2. If player1 is alive AND either player2 OR player3 is alive

You can make this expression explicit by

  1. ( alive player1 && alive player2 ) || alive player3
  2. alive player1 && ( alive player2 || alive player3 )

 

Braces define a scope of code. These scopes appear in many places in Arma scripting from the definition of a function..

TAG_fnc_myFunction = {
    hint "Hello World";
};

call TAG_fnc_myFunction;

Which is basically a variable TAG_fnc_myFunction that holds some code.

Or the code an If statement is to run..

if ( true ) do { some code };

You can find them in many other places as well, for instance a While statement..

while { true } do {
    hint "Hello World";
};

Or waitUntil..

waitUntil { alive player };

Unlike most programming languages where these commands expect a condition (), in Arma they expect braces, and can be replaced with any variable that holds code as long as they return any specific result needed. e.g both While and waitUntil both require a boolean (true/false).

So we could do something like..

TAG_fnc_message = {
    hint "Player is alive";
};

TAG_fnc_alivePlayer = {
    alive player
};


while TAG_fnc_alivePlayer do TAG_fnc_message;

Which is syntactically correct as both the variables in the while statement get replaced with the code they represent and TAG_fnc_alivePlayer returns the boolean needed by the while statement.

 

In the Arma scripting language braces can also be used inside conditional statements to provide lazy evaluation. e.g

if ( alive player1 && alive player2 ) then {

If player1 is not alive the condition will also check alive player2 although it has no need to as for AND (&&) to be true both player1 and player2 need to be alive, so it could just ignore the second condition as it knows its going to be false anyway.

We can make arma do this by using braces. e.g

if ( alive player1 && { alive player2 } ) then {

Here if player1 is not alive it will not check player2 and will immediately finish the condition as false.

This comes in extremely handy for when certain variables are not yet defined. e.g

if ( !isNil "_myVar" && { _myVar isEqualTo "Hello" } ) then {
    hint format[ "myVar == %1", _myVar ];
};

Here if myVar is nil (has not been defined (given a value)) then it will not try to compare its value to "Hello". Without the braces it would always try to compare the value of myVar but as it could possibly be undefined it would cause the code to throw an error.

It is also handy for when you have a condition that could be computationally expensive like maybe a distance check. e.g

if ( alive player1 && { player1 distance HQ < 250 } ) then {

Here if player1 is not alive then it will not do the distance check, which is computationally more expensive then just a true/false statement like alive.

 

Hope that helps

  • Like 6

Share this post


Link to post
Share on other sites

Excellent! You covered the bit about curly brackets and brackets in an if condition! Thanks!

Share this post


Link to post
Share on other sites

Jesus, i think i could cut myself on accident with the sheer edge this thread has now. 

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×