How can I --NOT-- use if?

Non-platform specific questions.

How can I --NOT-- use if?

Postby Hblade » Wed Mar 21, 2012 8:21 pm

What are some of the best ways to "Not" use if statements? For exapmle, the shortest way of creating something like.. well like this, but without the if's
Code: Select all
if(ArmorPiece==1)
{
    ///
}
if(ArmorPiece==2)
{
    ///
}
if(ArmorPiece==3)
{
    ///
}
if(ArmorPiece==4)
{
    ///
}
if(ArmorPiece==5)
{
    ///
}
if(ArmorPiece==6)
{
    ///
}
if(ArmorPiece==7)
{
    ///
}
if(ArmorPiece==8)
{
    ///
}
if(ArmorPiece==9)
{
    ///
}
if(ArmorPiece==10)
{
    ///
}


I know there's "Switch" like this:
Code: Select all
switch(ArmorPiece)
{
    case 0: break;
    case 1: break;
    case 2: break;
    case 3: break;
    case 4: break;
    case 5: break;
    case 6: break;
    case 7: break;
    case 8: break;
    case 9: break;
    case 10: break;
}



But is there an easier way?

I was thinking something like:
Code: Select all
ArmorPiece = 0 + var;


But then you'd still have to use if statements to change the armor.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How can I --NOT-- use if?

Postby skydereign » Wed Mar 21, 2012 8:43 pm

The two best ways I've noted to avoid using if statement are first structuring your program so it doesn't need to make choices, and the other is the switch statement. Sometimes if statements are the way to go, but in the case of several conditions, a switch will be better. But there are times where you can avoid making the program have to make a choice at all. For instance I was helping another user implement highscores. The idea was that the score would increase as the game was playing, but not while the game had ended. You can use an if statement to do that.
score_display -> Draw Actor -> Script Editor
Code: Select all
if(game_state==0) // 0 standing for in game
{
    score+=10;
}

That way when the player is destroyed, you can set game_state to 1, and that way in the highscore menu it wouldn't increase the score. But, you can also just add the score+=10 call in the player's draw event. Since the player only exists while you are playing the game, it makes sense that after the game mode ends (the player is destroyed) the score would stop increasing. This method bypasses the need for the if statement and the game_state variable. Another method would be to use EventDisable to disable the score_display's draw event while not in game mode. But the use of player's makes more sense, and it follows the object oriented approach. By using actors as objects, you are able to get rid of a lot of the decision making you normally would have to have.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: How can I --NOT-- use if?

Postby Hblade » Wed Mar 21, 2012 8:53 pm

Come to think of it, another way you can do it is, while the player is alove, an invisible actor (or region) is on the screen, have it not take events while outside of the vision, then
Player->Destroy Actor->Script Editor
Code: Select all
region.y=-5000;

This will be the same, but having it on the players draw actor seems more efficient than that.

"Structuring the program" what do you mean?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How can I --NOT-- use if?

Postby skydereign » Wed Mar 21, 2012 9:12 pm

Just like that example. You could make the program decide if game_state is equal to 0, or you could make the player increase the score. With that change, you didn't need to check what game_state was. This idea can be applied to a lot of different things. For instance using spawner actors, that have the animation of the actor they spawn. This way you can use a single spawner actor, that can spawn whichever actor you want, just by changing the animindex.
Code: Select all
switch(animindex) // determines spawn type
{
    case 0: // whatever case 0 was
    // create some actor
    break;

    case 1:
    // create another actor
    break;

    // and so on
}

That makes sense, but you can make it so the spawner doesn't need to choose which type it is making.
Code: Select all
CreateActor(getAnimName(animindex), "stand_r", "(none)", "(none)", 0, 0, false);

And assuming you use some standard set of naming conventions for animations, you can adjust that to be exactly what you want, but with only one line of code, and no decision making on the part of the program.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: How can I --NOT-- use if?

Postby Hblade » Wed Mar 21, 2012 9:20 pm

Very interesting! I like how you used that "getAnimIndex" code in CreateActor, functions inside functions. incredible.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest