PDA

View Full Version : const bool?



Joe123
10-27-2008, 10:14 AM
Howcomes we can't have constant bools?

ScaryBinary
11-01-2008, 08:56 AM
...I haven't checked the compiler yet, but I'm assuming that you can't get your script to compile with something like
const bool This_Is_True = true; in it?

You could just use an integer (1 for true, 0 for false) and I think all your logical expressions will work fine. Something like
const int Link_Is_Awesome = 1;

if (Link_Is_Awesome && (Link->HP > 0)) {
//...do whatever...
}

should work.

Or just use a bool and try to not ever change it....?

What would you use a constant bool for? An optional game setting or something?

Joe123
11-01-2008, 09:15 AM
Well yes, I could use an integer, but it just looks a bit messy. And that if would give me a 'Cast from float to bool' warning every time I run it through the compiler, which isn't the end of the world, but it's quite annoying.

I've just gone with a bool that I'm not gonna change.
It's just for ease of reading really, I have some solidity checks which are generalised for left/right and for up/down, so they need to have a bool to tell the script which one to check.

bool soliditycheck(int x, int y, bool horizvert){
if(horizvert){
//check for up down
}else{
//check for left right
}
}

CaRmAgE
11-01-2008, 12:43 PM
This is probably because the developers didn't think anyone would need a constant boolean declaration, since you can simply tack true or false into a conditional statement or argument list for a function. For example, ScaryBinary's


const int Link_Is_Awesome = 1;

if (Link_Is_Awesome && (Link->HP > 0)) {
//...do whatever...
}

would become


if (true && (Link->HP > 0)) {
//...do whatever...
}

I do agree, however, that such declarations should be allowed for ease of reading.

And, yes, integers work, since a boolean is simply a special type of integer, and it isn't as messy as you think. You only need to change the declaration of your variables: the conditional statement does not need to be changed at all. So, in your example...


bool soliditycheck(int x, int y, int horizvert){
if(horizvert){
//check for up down
}else{
//check for left right
}
}

Does this make sense? I'm sorry if I'm confusing you.

Joe123
11-01-2008, 01:54 PM
bool soliditycheck(int x, int y, int horizvert){
if(horizvert){
//check for up down
}else{
//check for left right
}
}
The 'if(horizvert)' line would cause the compiler to give me a 'Cast from float to bool' error every time I compiled it.
I'd have to use 'if(horizvert==1)', and I don't really want to :shrug:

I understand it, I was just wondering if there was any reason for it.

CaRmAgE
11-01-2008, 02:34 PM
Wow. A boolean isn't considered a type of integer in ZScript? How odd. :odd:

Well, whatever you think is best. This thing is starting to sound like a bug to me, so I'd say post something in the Open Beta Bugs forum (http://armageddongames.net/forums/forumdisplay.php?f=153) to let the ZC developers know about the compiler error.

Joe123
11-01-2008, 05:26 PM
There are no true integers in ZScript.
All integers are actually floats.

If you look in std.zh (the header file containing the pre-set constants), there's a line that says 'const int PI = 3.142;'

It's not really a bug, I'm assuming they missed this out on purpose, I was just wondering why.
You don't get a compile error for writing that, just a warning.