ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


functions:cond

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
functions:cond [2017/03/15 10:45] – created korshunfunctions:cond [2017/06/15 15:34] (current) korshun
Line 1: Line 1:
-====== swap ====== +====== cond ====== 
-{{tag>math}} +{{tag>math acs_enhancements}} 
-''any, any swap(any a, any b)''+''any cond(bool condition, any a, any b)''
  
 [[:types|any - any type (int, fixed or str)]] [[:types|any - any type (int, fixed or str)]]
- 
-[[:Multiple_return_values|This function returns multiple values.]] 
  
 ===== Description ===== ===== Description =====
-Returns two variables in swapped orderThis function exists to improve readability.+If condition is true, returns a. Otherwise, returns b. 
 + 
 +It simulates the ''?:'' operator from C. 
 + 
 +<note important>Both a and b are evaluated. Do not do this: 
 +<code> 
 +int y = cond(x, DoSomething(), DoSomethingElse()) 
 +</code> 
 +as both ''DoSomething()'' and ''DoSomethingElse()'' will be called. 
 + 
 +If you are using [[:BCC]] or [[:GDCC]], it is recommended to use the real ''?:'' operator instead, which is not subject to this problem. 
 +</note> 
  
 ===== Examples ===== ===== Examples =====
-Without swap(): 
 <code> <code>
-int tmp x+cond(true, A, B) -> A 
-y; +cond(false, A, B) -> B 
-tmp;+</code> 
 + 
 +Without cond(): 
 +<code> 
 +int color CR_RED
 +if (PlayerTeam() == TEAM_BLUE) 
 +    color CR_BLUE; 
 +DrawSomething(color);
 </code> </code>
  
-With swap():+With cond():
 <code> <code>
-swap(x, y)+DrawSomething(cond(PlayerTeam() == TEAM_BLUE, CR_BLUE, CR_RED));
-r1; +
-r2;+
 </code> </code>
functions/cond.1489567518.txt.gz · Last modified: 2017/03/15 10:45 by korshun