Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: edmundito on Sun 17/04/2005 18:29:48

Title: About Conditional Statements...
Post by: edmundito on Sun 17/04/2005 18:29:48
Some programming languages allow you to do the following gimmick:

int *a;
(...)
if(a && *a > 5) {}


But I tried doing something similar on AGS and it broke! does it not have some sort of "short-circuit" mechanism? or do I have to do it this way:

if(a){
  if(*a >5) {}
}


Actually, what I did had nothing to do with pointer, what I checked for was if a property did not have a certain value and then if the player had the inventory with the property value... which broke when I didn't set a value to the property (default being -1)
Title: Re: About Conditional Statements...
Post by: DoorKnobHandle on Sun 17/04/2005 18:44:08
Try this:


if(a > 5 && *a > 5) {}


Should work!

Quote
I tried doing something similar on AGS and it broke!

;D
Title: Re: About Conditional Statements...
Post by: edmundito on Sun 17/04/2005 19:14:32
this is what I did, though:

if(What.GetProperty("InvItem") >= 0 && player.InventoryQuantity[What.GetProperty("InvItem")] > 0)


But it still checks for player.InventoryQuantity[What.GetProperty("InvItem")]  even though it should have failed by What.GetProperty("InvItem") >= 0.
Title: Re: About Conditional Statements...
Post by: strazer on Sun 17/04/2005 20:06:49
http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=509
Title: Re: About Conditional Statements...
Post by: edmundito on Sun 17/04/2005 20:17:32
Ahh... thanks, Strazer. I guess I'm going to stop being lazy and do it right :P

actually, it would be nice if you someone could help me out on this one.. because this is what it's supposed to do:

if(What.GetProperty("InvItem") >= 0 && player.InventoryQuantity[What.GetProperty("InvItem")] > 0) {
  // success!
}
else {
  // display message saying that you can't do it
}


but if I change it to, then I have to do something silly like:

if(What.GetProperty("InvItem") >= 0) {
  if(player.InventoryQuantity[What.GetProperty("InvItem")] > 0) {
    // success!
    }
    else {
       // display message saying that you can't do it
    }
}
else {
  // display message saying that you can't do it AGAIN!!!
}


I'm a bit rusty (and lazy) on coding right now... I'm guessing that the other way is to do some sort of else-if or even an if statement. I'll think about it out some more.
Title: Re: About Conditional Statements...
Post by: strazer on Tue 19/04/2005 06:59:29
How about this:


  bool success;

  if (What.GetProperty("InvItem") >= 0)
    if (player.InventoryQuantity[What.GetProperty("InvItem")] > 0)
      success = true;

  if (success == true) {
    // success!
  }
  else {
    // display message saying that you can't do it
  }
Title: Re: About Conditional Statements...
Post by: Scummbuddy on Tue 19/04/2005 16:16:13
shouldn't it be with more parantheseses?

if ( (What.GetProperty("InvItem") ) >= 0 && ( player.InventoryQuantity[What.GetProperty("InvItem")] > 0)Ã,  )
Title: Re: About Conditional Statements...
Post by: strazer on Tue 19/04/2005 19:11:33
Nope, that's not it.

Edit:

The expressions are still both evaluated.

But in general, it is indeed good practice to surround combined expressions with their own braces:

if ( (What.GetProperty("InvItem") >= 0) && (player.InventoryQuantity[What.GetProperty("InvItem")] > 0) ) {