Have you ever wanted to make a game with lots of characters? For exemple, fighting games have a menu in which you can choose what character you want to play with and sometimes even what enemy you want to fight against. So, how do you do that?
Most people will think that they need to make a separate layout for every possible character vs enemy combination, like for exemple:
layout 1 is character1 vs enemy 1
layout 2 character 2 vs enemy 1
layout 3 character 3 vs enemy 1
and then again, character 1, 2, 3 against enemy 2 and character 1,2,3 against enemy 3 etc.
If you only have 2-3 charcaters, its ok, but what if you want to have 10-20? Are you going to do a layout for every possible combination? No way.
The easy way is to use global variables. Create 2 global variables, one for characters and one for enemies.
Now, for this exemple, I will use 3 character and 3 enemies but the method works no matter how many characters/enemies you have.
When player chooses character 1, the global variable is equal to 0. If he/she chooses character 2, the variable changes to 2, etc. and the same with the other variable with the enemies.
Now, after the player chose the character and the enemy, and clicks on play, he/she expects the selected character and enemy to appear on the game layout. We can easily do that by spawning the character and the enemy based on the variable value.
If the selectCH variable is 0, character 1 will appear. If selectCH=1, character 2 will appear, etc and the same will happen with the enemy.
This way, no matter how many characters/enemies you have, you can spawn them on your game layout. Now you dont have to make a layout for every possible character-enemy combination that the player may choose. Hope it helped! :)