• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Help with syntax - if X == (a bunch of stuff)

K

Kululu17

Guest
Hi - a syntax question: Is there a way to list several items following an if statement?

I would like something like:

if global.Colony_Database[(Colony_Nr),COL_Behavior] == ("Trading" or "Adjusting" or "Advanced" or "Specializing") {do stuff}

But this is not proper syntax. The correct way of doing this is:

if global.Colony_Database[(Colony_Nr),COL_Behavior] == "Trading" or global.Colony_Database[(Colony_Nr),COL_Behavior] == "Adjusting" or blabla

However doing this ends up with a line of code so long it goes off the end of the page. I could use a switch statement, but then you have many lines of code. Is there a shorthand notation for this logic? It's a simple logic, and I was hoping to accomplish it in a short piece of code. Thanks!
 
T

ThunkGames

Guest
If you end up repeating this several times in your code, you could create a function that would return true or false if all conditions are true.

Otherwise:
Code:
if (
global.Colony_Database[(Colony_Nr),COL_Behavior] == "Trading" ||
global.Colony_Database[(Colony_Nr),COL_Behavior] == "Adjusting" ||
global.Colony_Database[(Colony_Nr),COL_Behavior] == "This" ||
global.Colony_Database[(Colony_Nr),COL_Behavior] == "That") {
// Do stuff
}
Is as good as you can do (as far as I know).
 

obscene

Member
Well it's long because you are using this long part twice...

global.Colony_Database[(Colony_Nr),COL_Behavior]

The way to clean stuff like this up is this...

Code:
var cd=global.Colony_Database[(Colony_Nr),COL_Behavior];
if ( cd=="Training" || cd=="Adjusting" )
  {
  }
And switch would be good here too...

Code:
var cd=global.Colony_Database[(Colony_Nr),COL_Behavior];
switch ( cd )
  {
  case "Trading":
  case "Adusting":
  //do stuff
  break;
  }
 
A

Aura

Guest
You could write a script for that as well.

Code:
for (var i = 1; i < argument_count; i++) {
   if (argument[0] == argument[i]) {
      return true;
   }
}
return false;
Then use it this way:

Code:
var value = global.Colony_Database[Colony_Nr, COL_Behavior];
if (any(value, "Trading", "Adjusting", "Advanced", "Specializing")) {
   // Do something
}
 

TheouAegis

Member
Don't use strings and make enumerators instead. Manually assign the values for each numerator to be powers of 2.

enum Behaviors {
Undecided = 0;
Trading = 1;
Adjusting = 2;
Advanced = 4;
Specializing = 8;
}

Or whatever. Just remember to keep them powers of 2:

1
2
4
8
16
32
64
128
256
512

Then you check if the value ANDed by the conjunction of them all is true.

if global.Colony_Database[Colony_Nr, COL_Behavior] & (Behaviors.Trading | Behaviors.Adjusting | Behaviors.Advanced | Behaviors.Specializing)
{ do stuff }
 
M

mikahon

Guest
Out of curiosty, why does it have to be a power of 2 ?
 

Gradius

Member
@mikahon: Because it uses bitwise (binary) maths, which relies of a power-of-two number system. It's the binary equivilant of having things go up in powers of 10 (i.e. 10, 100, 1000) that way the values never 'overlap'.
 
Top