GMS 2 [SOLVED]Custom Controls?

Doggo

Member
So, im trying to make a control menu so that you can change your controls.
I have a script that checks for a input, then assigns it to a variable.

Code:
global.str0 = ""
global.keyPressed = argument
switch (keyboard_key) {
        case vk_enter: global.str0 = "Enter"; break;
        case vk_left: global.str0 = "Left"; break;
        case vk_right: global.str0 = "Right"; break;
        case vk_up: global.str0 = "Up"; break;
        case vk_down: global.str0 = "Down"; break;
        case vk_escape: global.str0 = "Escape"; break;
        case vk_space: global.str0 = "Space"; break;
        case vk_shift: global.str0 = "Shift"; break;
        case vk_control: global.str0 = "Control"; break;
        case vk_alt: global.str0 = "Alt"; break;
        case vk_backspace: global.str0 = "Backspace"; break;
        case vk_tab: global.str0 = "Tab"; break;
        case vk_home: global.str0 = "Home"; break;
        case vk_end: global.str0 = "End"; break;
        case vk_delete: global.str0 = "Delete"; break;
        case vk_insert: global.str0 = "Insert"; break;
        case vk_pageup: global.str0 = "Page Up"; break;
        case vk_pagedown: global.str0 = "Page Down"; break;
        case vk_pause: global.str0 = "Pause"; break;
        case vk_printscreen: global.str0 = "Printscreen"; break;
        case vk_f1: global.str0 = "F1"; break;
        case vk_f2: global.str0 = "F2"; break;
        case vk_f3: global.str0 = "F3"; break;
        case vk_f4: global.str0 = "F4"; break;
        case vk_f5: global.str0 = "F5"; break;
        case vk_f6: global.str0 = "F6"; break;
        case vk_f7: global.str0 = "F7"; break;
        case vk_f8: global.str0 = "F8"; break;
        case vk_f9: global.str0 = "F9"; break;
        case vk_f10: global.str0 = "F10"; break;
        case vk_f11: global.str0 = "F11"; break;
        case vk_f12: global.str0 = "F12"; break;
        case vk_numpad0: global.str0 = "Numpad 0"; break;
        case vk_numpad1: global.str0 = "Numpad 1"; break;
        case vk_numpad2: global.str0 = "Numpad 2"; break;
        case vk_numpad3: global.str0 = "Numpad 3"; break;
        case vk_numpad4: global.str0 = "Numpad 4"; break;
        case vk_numpad5: global.str0 = "Numpad 5"; break;
        case vk_numpad6: global.str0 = "Numpad 6"; break;
        case vk_numpad7: global.str0 = "Numpad 7"; break;
        case vk_numpad8: global.str0 = "Numpad 8"; break;
        case vk_numpad9: global.str0 = "Numpad 9"; break;
        case vk_multiply: global.str0 = "Numpad *"; break;
        case vk_divide: global.str0 = "Numpad /"; break;
        case vk_add: global.str0 = "Numpad +"; break;
        case vk_subtract: global.str0 = "Numpad -"; break;
        case vk_decimal: global.str0 = "Numpad ."; break;
}

return string(global.str0)
I want to know how I can change the global.keyPressed variable to the keys that I listed, and the one the user pressed, and bring that value to a variable to the object that is loading the script. Same for global.str0. Any way to do this?
 
Can you please elaborate? Sorry, but I find the current explanation of what you are trying to do confusing. I'm also confused by this line:

Code:
global.keyPressed = argument
argument is an array containing all the arguments passed to the function, there is no way you're intending to assign that to your global variable. Did you mean to use argument0?
 

Doggo

Member
Can you please elaborate? Sorry, but I find the current explanation of what you are trying to do confusing. I'm also confused by this line:

Code:
global.keyPressed = argument
argument is an array containing all the arguments passed to the function, there is no way you're intending to assign that to your global variable. Did you mean to use argument0?
Yeah, that's what i meant. I thought that would work as when I call the script it asked for it. I am trying to make customizable controls(like Minecraft for a example) and i want to know when I run the script, it will check for a key input(all those listed) and then check what was pressed. Then assign what was pressed into global.keyPressed and the string to global.str0. Then, whatever object called the script, will assign those variables to the variable that I want, global.keyUp.
 

NightFrost

Member
You should stop the switch running needlessly while no key is pressed. Also, you said you want to read the keypress outside the script and then call it with that. For that you should:
Code:
// Do this part outside the script:
global.keyPressed = keyboard_key;
Since global.keyPressed has global scope, you don't need to send it to the script and just use it there instead.
Code:
// scr_change_input contents
global.str0 = "";
if(global.keyPressed != 0){ // Do the work only if some key was pressed.
    switch(global.keyPressed){
        // Your cases here.
    }
}
There's no need to return anything either since you're putting the value into global.str0, as it is accessible in global scope.
 

Doggo

Member
You should stop the switch running needlessly while no key is pressed. Also, you said you want to read the keypress outside the script and then call it with that. For that you should:
Code:
// Do this part outside the script:
global.keyPressed = keyboard_key;
Since global.keyPressed has global scope, you don't need to send it to the script and just use it there instead.
Code:
// scr_change_input contents
global.str0 = "";
if(global.keyPressed != 0){ // Do the work only if some key was pressed.
    switch(global.keyPressed){
        // Your cases here.
    }
}
There's no need to return anything either since you're putting the value into global.str0, as it is accessible in global scope.
This worked, but I had to put
Code:
global.keyPressed = keyboard_key;
in the script and when I press it, it goes to the next room(as I told it too) and I replaced global.upStr with it, but it doesn't replace it.
Code:
global.keyPressed = keyboard_key
global.str0 = "";
if(global.keyPressed != 0){ // Do the work only if some key was pressed.
    switch(global.keyPressed){
        case vk_enter: global.str0 = "Enter"; break;
        case vk_left: global.str0 = "Left"; break;
        case vk_right: global.str0 = "Right"; break;
        case vk_up: global.str0 = "Up"; break;
        case vk_down: global.str0 = "Down"; break;
        case vk_escape: global.str0 = "Escape"; break;
        case vk_space: global.str0 = "Space"; break;
        case vk_shift: global.str0 = "Shift"; break;
        case vk_control: global.str0 = "Control"; break;
        case vk_alt: global.str0 = "Alt"; break;
        case vk_backspace: global.str0 = "Backspace"; break;
        case vk_tab: global.str0 = "Tab"; break;
        case vk_home: global.str0 = "Home"; break;
        case vk_end: global.str0 = "End"; break;
        case vk_delete: global.str0 = "Delete"; break;
        case vk_insert: global.str0 = "Insert"; break;
        case vk_pageup: global.str0 = "Page Up"; break;
        case vk_pagedown: global.str0 = "Page Down"; break;
        case vk_pause: global.str0 = "Pause"; break;
        case vk_printscreen: global.str0 = "Printscreen"; break;
        case vk_f1: global.str0 = "F1"; break;
        case vk_f2: global.str0 = "F2"; break;
        case vk_f3: global.str0 = "F3"; break;
        case vk_f4: global.str0 = "F4"; break;
        case vk_f5: global.str0 = "F5"; break;
        case vk_f6: global.str0 = "F6"; break;
        case vk_f7: global.str0 = "F7"; break;
        case vk_f8: global.str0 = "F8"; break;
        case vk_f9: global.str0 = "F9"; break;
        case vk_f10: global.str0 = "F10"; break;
        case vk_f11: global.str0 = "F11"; break;
        case vk_f12: global.str0 = "F12"; break;
        case vk_numpad0: global.str0 = "Numpad 0"; break;
        case vk_numpad1: global.str0 = "Numpad 1"; break;
        case vk_numpad2: global.str0 = "Numpad 2"; break;
        case vk_numpad3: global.str0 = "Numpad 3"; break;
        case vk_numpad4: global.str0 = "Numpad 4"; break;
        case vk_numpad5: global.str0 = "Numpad 5"; break;
        case vk_numpad6: global.str0 = "Numpad 6"; break;
        case vk_numpad7: global.str0 = "Numpad 7"; break;
        case vk_numpad8: global.str0 = "Numpad 8"; break;
        case vk_numpad9: global.str0 = "Numpad 9"; break;
        case vk_multiply: global.str0 = "Numpad *"; break;
        case vk_divide: global.str0 = "Numpad /"; break;
        case vk_add: global.str0 = "Numpad +"; break;
        case vk_subtract: global.str0 = "Numpad -"; break;
        case vk_decimal: global.str0 = "Numpad ."; break;
    }
    global.upStr = global.str0
    room_goto_next()
}
^here it is so you can visualize it...
 
Where do you define your variables? Could it be you are resetting them when moving through rooms because of new Create events being fired?
 

Doggo

Member
I am defining them through the player event. I thought the player not being in the room might do something with it
 

Doggo

Member
I am defining them through the player event. I thought the player not being in the room might do something with it
nvm i just assigned the variable to the script and it worked
thanks for all your help guys ;)
 
Top