• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Probleme With the switch expression and array.(noob)

Hello everyone! I am new to programming and I do not understand why my code is not working. I thought it is possible to assign multiple variables via an array. Here is my code. Have a lovely day everybody!

// Step event

//Controle Jeux.

keyRestart = keyboard_check(ord("R"));
keyQuit = keyboard_check(vk_escape);

keyGame = array_create(keyRestart, keyQuit);

switch (keyGame)
{
case keyRestart:
game_restart();
break;

case keyQuit:
game_end();
break;

}[/CODE]
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
This is not how it's done I'm afraid. You are passing in the array ID to the switch statement, not the results of a key press within the array (and you are defining the array incorrectly too, read the manual!). For your code to work as you are designing it you don't even need an array and can do this:

GML:
var _key = "";
if keyboard_check(ord("R"))
{
_key = "Restart";
}
else if keyboard_check(vk_escape)
{
_key = "Escape"
}

switch (_key)
{
case "Restart": game_restart(); break;
case "Escape": game_end(); break;
}
If you want to use an array you would need to create a loop that checks the values of the array components, so, like this (although it's pretty pointless as you're doing two checks when you can use the first check on it's own!):

GML:
var _check_array = array_create(2, 0);
if keyboard_check(ord("R"))
{
_check_array[0] = 1;
}
if keyboard_check(vk_escape)
{
_check_array[1] = 0;
}

for (var i = 0; i < array_length(_check_array); ++i;)
{
if _check_array[0] == 1
    {
    game_restart();
    }
else if _check_array[1] == 1
    {
    game_end();
    }
}
 
Last edited:
Top