PDA

View Full Version : Trying to get multiple ifs working.



Plissken
07-30-2008, 01:07 AM
ffc script Menus{
void run(int options){
int y = 16;
this->Y = y;
if(options == 2);
while(this->Y < 32){
if(Link->InputDown){
this->Y += 16;}
Waitframes(30);
}
if(options == 3);
while(this->Y < 48){
if(Link->InputDown){
this->Y += 16;}
Waitframes(30);
}
Waitframe();
}
}

Ok, you see where it says: if(options == X); ?
I want that to be if D0 equals 2 than the while loop underneath if(options == 2); to be carried out. Same goes for if D0 equals 3 and the if(options == 3);

But I can't get anything but the bottom one to work, if D0 equals 2 than it still does the while loop underneath if(options == 3);

I know I'm most likely just forgetting or misplacing some brackets or something so if anybody could help me out on this that'd be awesome. Oh and yes Gleeok I'm trying to make a sorta menu script for shops and stuff like your main menu in Gika...gah your arcade game.

I'm still in the newbie sorta era of scripting so if you see anything that could easily be done better please tell me. I'm trying to get an FFC to move down 16 pixels every time you press down but stop at a certain place depending on your information in D0. (The number of options this specific menu will have)

Elmensajero
07-30-2008, 02:14 AM
The main issue here is that you put semicolons after the if conditions. Basically this is saying compare the variable options to the value 2, and then do nothing if it is true. After that, the while loop is executed no matter what, so every 30 frames it checks the user input until the menu cursor reaches Y position reaches 32. It then has another if statement that does nothing, and then executes the next while loop, which allows the user to go down to the next menu option, even if only 2 were supposed to be available. To fix this, all you have to do is add brackets around the lines of code you only want executed when the if condition is true. If there is only one line of code, you don't need brackets.

After this, we need to fix the while loops, because if you plan on allowing the user to move the cursor up as well, it won't work to check for the Y position reaching the lowest possible position. So, if you want it to keep checking the user input over and over, we have to add an infinite while loop around the whole thing (except for any initialization which you want done once).

Finally, you can combine the first two lines of code after void run() into one statement, and we can reduce the if statements down to one condition. So, after revising all of that, here is what I came up with...

ffc script Menus
{
void run(int options)
{
this->Y = 16;

while(true)
{
//First check cursor moving down, and make sure it is not already at the lowest option of the menu.
if(Link->InputDown && this->Y<options*16)
{
this->Y += 16;
Waitframes(30); //This forces the user to wait half a second before they can move the cursor again.
}
Waitframe();
}
}
}

Plissken
07-30-2008, 02:19 AM
Ah, I see now much easier to work with now. Thanks a lot.

thebetter1
08-21-2008, 11:43 AM
Actually, the problem with the second if statement is that, because there are no brackets, it is skipping the While instruction and just executing what is in the loop once. At least I think ifs work that way in ZScript...

Elmensajero
08-21-2008, 12:36 PM
Well, in C (and I am pretty sure ZScript) when there are no brackets the compiler assumes there is only one statement to be executed when the condition is true. Since a semicolon by itself is actually a statement (even though it doesn't do anything), when it saw the semicolon after the if statement, it assumed that was the only statement and that the while loop wasn't included in the if condition, so the while loop was always executed, not skipped.

Now, here is another example script to show how brackets and if conditions work.


import "std.zh"

ffc script testif
{
void run()
{
int var=2;
int rand=1;

while(true)
{
if(var==3) while(Link->InputUp){ Link->InputUp=false; rand++; } //Never executes because var==3 is always false.
Trace(rand);
Waitframe();
}
}
}

Now, according to your reply above, the if statement in this should cause the while condition after it to be skipped because there are no braces around the while loop, but still execute the instructions inside it once. (Of course, the while loop does have braces inside of it, but so did Plissken's in the code at the top.) However, the statements inside the while loop are never executed (check allegro.log to see, no matter how many times you press up, rand will always equal "1"). This is because the while loop is actually considered as one statement, since it has braces around the conditions inside its loop, so when the if condition fails, it moves on to the next statement, which is Trace();. Sorry if any of this is confusing, I tried to make it as clear as possible :D