Have you every seen a code like this?
switch (room)
{
case rm_1: enemy_count=60
case rm_2: enemy_count=120
case rm_3: enemy_count=300
}
That probably looks somewhat confusing to you right now. So let me explain. Imagine you have three suitcases, which we will call cases, and you have one switchblade, which we will refer to as "switch". Now, everytime you stab a case with a switch, the case shows its true value. Now, we will pretend that you have a 50-foot long pole, which we will call "if". Now, imagine you are very close to the three cases, and you can choose to stab them with a switch of with an if. Which would you rather? You may say the "if pole", but it would take longer to use the "if pole" since there is more that we would have to do (like prepare ourselves for actually lifting the pole up...)! So there are times where you have to use this pole, but you usually want to avoid it where possible. That's why you want to use the switch. It is small, and very light, so you can easily stab each case with it!
Although I have explained this in a very different way, GML functions are very much like this. Unless you're using a very short if statement, if you can use a switch statement, it usually comes in very handy, since it is like writing an if statement but in a much shorter way. So, I will now decode the above code.
switch (room)
{
case rm_1: enemy_count=60
case rm_2: enemy_count=120
case rm_3: enemy_count=300
}
is the same as saying
if room == rm_1 {enemy_count=60}
if room == rm_2 {enemy_count=120}
if room == rm_3 {enemy_count=300}
Obviously, the switch statement is a lot faster. Notice how it says at the beginning: switch (room)? That simply lets Game Maker know that you will be "stabbing all of the cases" with a switch that is checking for the room. So then we give it a few possible cases that it can "stab". If the object which withholds the case is in the room called rm_1, then the switch will stab that case and out comes a variable called enemy count, which is set to 60. The same thing happens with all of these "cases", but the variable enemy_count is set to a different number depending on the room. Obviously, an if statement here wouldn't have been a lot longer, but a lot of times you'll run into times where you need to check a lot of different things. That is exactly what a switch statement does: it checks a lot of different things, and the things are all determined by you. So with the above code, you are checking what the room is and then you are telling it what to do depending what room it is. So, here is a review of the above stuff:
switch (room) /*Says to Game Maker: if room*/
{ //Start of the number of cases.
case rm_1: enemy_count=60 /*if room equals rm_1, enemy_count is set to 60.*/
case rm_2: enemy_count=120 /*if room equals rm_2, enemy_count is set to 120.*/
case rm_3: enemy_count=300 /*if room equals rm_3, enemy_count is set to 300.*/
} //end of a block.
By the way, cases don't only have to "hold" variables. They can also "hold" numbers. So, for example, if you want to say:
if can_shoot == 0 exit instance_create(x,y,obj_bullet)
you could use a switch statement and say,
switch (can_shoot)
{
case 0: exit /*if can_shoot == 0 exit*/
case 1: instance_create(x,y,obj_bullet) /*if can_shoot == 1 instance_create(x,y,obj_bullet)*/
}
What you have seen hardly lowers the amount of code that you need to type, but trust me, there are some situations where the switch statement can make your life a whole lot easier. As a final statement, I must also tell you that you may want to use the break statement in switch statements occasionally.
switch (room)
{
case rm_1: enemy_count=60
case rm_2: enemy_count=120
case rm_3: enemy_count=300
}
may have errors, like setitng enemy_count to 60, then 120, then 300, all in the same room. So you should say:
switch (room)
{
case rm_1: enemy_count=60 break
case rm_2: enemy_count=120 break
case rm_3: enemy_count=300 break
}
instead. That way, you should have error-free code (unless you make an unrelated mistake).
One thing I must stress is that you use { and } blocks in switch statements. Otherwise, it may cause bad errors.
Thank you for reading this small tutorial. I hope you now understand the switch statement better!
.
Users logged in:
Comments
Loading comments...