User Tag List

Results 1 to 5 of 5

Thread: Trying to get multiple ifs working.

  1. #1
    Octorok Plissken's Avatar
    Join Date
    Aug 2006
    Location
    VA
    Age
    32
    Posts
    431
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,662
    Level
    13
    vBActivity - Bars
    Lv. Percent
    68.86%

    Trying to get multiple ifs working.

    Code:
    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)
    Creator of:

    INFO | VIDEOS | PICS | YOUTUBE

    ~Less is More~


  2. #2
    Octorok Elmensajero's Avatar
    Join Date
    May 2008
    Age
    35
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    853
    Level
    10
    vBActivity - Bars
    Lv. Percent
    19.8%

    Re: Trying to get multiple ifs working.

    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...
    Code:
    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();
    		}
    	}
    }
    Looking for the newest 2.5 content? Check out CZC for quests, demos, scripts, and more!

  3. #3
    Octorok Plissken's Avatar
    Join Date
    Aug 2006
    Location
    VA
    Age
    32
    Posts
    431
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,662
    Level
    13
    vBActivity - Bars
    Lv. Percent
    68.86%

    Re: Trying to get multiple ifs working.

    Ah, I see now much easier to work with now. Thanks a lot.
    Creator of:

    INFO | VIDEOS | PICS | YOUTUBE

    ~Less is More~


  4. #4
    Gel
    Join Date
    Dec 2006
    Age
    32
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    734
    Level
    9
    vBActivity - Bars
    Lv. Percent
    53.26%

    Re: Trying to get multiple ifs working.

    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...

  5. #5
    Octorok Elmensajero's Avatar
    Join Date
    May 2008
    Age
    35
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    853
    Level
    10
    vBActivity - Bars
    Lv. Percent
    19.8%

    Re: Trying to get multiple ifs working.

    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.
    Code:
    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
    Looking for the newest 2.5 content? Check out CZC for quests, demos, scripts, and more!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social