About Conditional Statements...

Started by edmundito, Sun 17/04/2005 18:29:48

Previous topic - Next topic

edmundito

Some programming languages allow you to do the following gimmick:

Code: ags
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:

Code: ags
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)

DoorKnobHandle

Try this:

Code: ags

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


Should work!

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

;D

edmundito

this is what I did, though:
Code: ags

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.


edmundito

#4
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:

Code: ags
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:

Code: ags
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.

strazer

How about this:

Code: ags

  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
  }

Scummbuddy

shouldn't it be with more parantheseses?

if ( (What.GetProperty("InvItem") ) >= 0 && ( player.InventoryQuantity[What.GetProperty("InvItem")] > 0)Ã,  )
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

strazer

#7
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) ) {

SMF spam blocked by CleanTalk