

Note that each case can have it's own code, and so you can set up a sort of "inheritance" system where more than one case and it's code will run consecutively with the next until a break is reached depending on the value of the initial switch expression. Note how in the code we have used the way that switch can check multiple cases and continue if no break is encountered to permit various keys to be used to get the same result. If it meets any of the required values then the corresponding code is executed. The above code uses switch to check for a keyboard event and then compares that to each case listed. If there is no break statement for a particular case, the execution simply continues with the code for the next case, eg:

Note that multiple case statements can be used to execute the same statement, as the break is not always required for each and every case. In this example the instance simply switches between two states, but it is extremely easy to expand this to include more states by adding further case statements for additional state strings, like "fight" or "die", etc. Next it is compared with the results of the different. Here we have a global variable that holds a string value which is used to set the behaviour (state) of the instance. This section deals with the uses of switch in GameMaker: Studio. If (point_distance(x, y, obj_Player.x, obj_Player.y) 100)

You can do this using a number of consecutive if / else statements, but when the possible choices gets above two or three it is usually easier to use the switch statement. In a number of situations you want to let your instances complete different actions depending on a particular value.
