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)
Try this:
if(a > 5 && *a > 5) {}
Should work!
Quote
I tried doing something similar on AGS and it broke!
;D
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.
http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=509
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.
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
}
shouldn't it be with more parantheseses?
if ( (What.GetProperty("InvItem") ) >= 0 && ( player.InventoryQuantity[What.GetProperty("InvItem")] > 0)Ã, )
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) ) {