Legacy GM Q:Displayng keys

Z

zendraw

Guest
Hi, so im trying to draw the keys that the player will use to play the game but when drawing the mouse buttons it draws nothing and on the arrow keys it draws other symbols.
this is the line i use to draw
Code:
draw_text(xx, yy, chr(key));
 

GMWolf

aka fel666
what you could do is have a switch statement to get the correct name of the key (I built one of those before).
[/SPOILER
Code:
///keycode_get_name(keycode);
/**
 **Returns a string representing the keycode of the given argument
 **/
    switch(argument0) {
        case vk_add: return "add"; break;
        case vk_alt: return "alt"; break;
        case vk_anykey: return "anykey"; break;
        case vk_backspace: return "backspace"; break;
        case vk_control: return "control"; break;
        case vk_decimal: return "decimal"; break;
        case vk_delete: return "delete"; break;
        case vk_divide: return "divide"; break;
        case vk_down: return "down"; break;
        case vk_end: return "end"; break;
        case vk_enter: return "enter"; break;
        case vk_escape: return "escape"; break;
        case vk_f1: return "f1"; break;
        case vk_f2: return "f2"; break;
        case vk_f3: return "f3"; break;
        case vk_f4: return "f4"; break;
        case vk_f5: return "f5"; break;
        case vk_f6: return "f6"; break;
        case vk_f7: return "f7"; break;
        case vk_f8: return "f8"; break;
        case vk_f9: return "f9"; break;
        case vk_f10: return "f10"; break;
        case vk_f11: return "f11"; break;
        case vk_f12: return "f12"; break;
        case vk_home: return "home"; break;
        case vk_insert: return "insert"; break;
        case vk_lalt: return "lalt"; break;
        case vk_lcontrol: return "lcontrol"; break;
        case vk_left: return "left"; break;
        case vk_lshift: return "lshift"; break;
        case vk_multiply: return "multiply"; break;
        case vk_nokey: return "nokey"; break;
        case vk_numpad0: return "numpad0"; break;
        case vk_numpad1: return "numpad1"; break;
        case vk_numpad2: return "numpad2"; break;
        case vk_numpad3: return "numpad3"; break;
        case vk_numpad4: return "numpad4"; break;
        case vk_numpad5: return "numpad5"; break;
        case vk_numpad6: return "numpad6"; break;
        case vk_numpad7: return "numpad7"; break;
        case vk_numpad8: return "numpad8"; break;
        case vk_numpad9: return "numpad9"; break;
        case vk_pagedown: return "pagedown"; break;
        case vk_pageup: return "pageup"; break;
        case vk_pause: return "pause"; break;
        case vk_printscreen: return "printscreen"; break;
        case vk_ralt: return "ralt"; break;
        case vk_rcontrol: return "rcontrol"; break;
        //case vk_return: return "return"; break;
        case vk_right: return "right"; break;
        case vk_rshift: return "rshift"; break;
        case vk_shift: return "shift"; break;
        case vk_space: return "space"; break;
        case vk_subtract: return "subtract"; break;
        case vk_tab: return "tab"; break;
        case vk_up: return "up"; break;
        default: return chr(argument0);
    }
I never ended up using it for much else than this one project. Hope you put it to good use :)

Now that i think about it, This could be improved with a lookup table of some sorts...
 
Z

zendraw

Guest
yea i know i can do that but i was wondering if i culd use the string of the assigned key even if its shown as vk_left, thats easly modifiable. is there really no direct way to accomplish this?
 

GMWolf

aka fel666
yea i know i can do that but i was wondering if i culd use the string of the assigned key even if its shown as vk_left, thats easly modifiable. is there really no direct way to accomplish this?
I think the fact I have this script in my library points to no.
Im fairly sure there is no GML function that will do that. But its why we have scripts :)
 

Nux

GameMaker Staff
GameMaker Dev.
No, because there is no valid character for left/right/up/down arrow keys (no valid character since chr() only returns a single character, not an array of characters such as "vk_left" - aka a string).
It's like asking the game to print out the character of backspace, return, or tab; they have keystates, but they're enumerated, so they could point to completely different characters entirely.
If you want to display arrow keys, your best bet is to use @Fel666 's script. Since, gamemaker isn't going to return VK_LEFT if you press the left key.
 
Z

zendraw

Guest
i see, but is it good to access a script that has a switch statement, in a for loop?
 

GMWolf

aka fel666
i see, but is it good to access a script that has a switch statement, in a for loop?
yes its fine.
Do not worry about performance. seriously... "premature optimization is the root of all evil"
besides, I think YYC will optimize it to a lookup table.

And if you like, you can optimize that to a lookup table yourself. Though i think there are holes in the keycodes... I dont like that. Perhaps a hashmap? but then its probably not as efficient as the switch... as i said; Dont think about optimization.
 
Z

zendraw

Guest
i see, thanks, but i wont mark it as solved for now in case some1 suggests something else, you never know.
 

GMWolf

aka fel666
Just for the record. Switch statements aren't that slow at all. They are generally considered to be rather quick.
In some cases, they can be O(1) fast, thanks to compiler optimization.

When not optimized at all, they are still O(N) fast. That's still very good. And since the number of keycodes here is fixed, its really O(1) time.
So your for loop will follow O(N) time even when calling this switch statement.

If this does become a problem performance wise, then optimizing this script won't help: the real problem is the number of times you are calling it.
 
Z

zendraw

Guest
okay so i saved your script and put it in use but the effect is the same, as if it goes directly to 'default' statement
this is the draw code
Code:
 draw_text(xx, yy, scr_conf_key(key[i,0]));
and this is in the creation event
Code:
ini_open('gamesettings.sav');

key[0,0] = ini_read_string('keys', string(0), ord('A'));
key[1,0] = ini_read_string('keys', string(1), ord('D'));
key[2,0] = ini_read_string('keys', string(2), ord('W'));
key[3,0] = ini_read_string('keys', string(3), ord('S'));
key[4,0] = ini_read_string('keys', string(4), mb_left);
key[5,0] = ini_read_string('keys', string(5), mb_right);
key[6,0] = ini_read_string('keys', string(6), ord('F'));
key[7,0] = ini_read_string('keys', string(7), ord('C'));
key[8,0] = ini_read_string('keys', string(8), ord('T'));
key[9,0] = ini_read_string('keys', string(9), vk_left);
key[10,0] = ini_read_string('keys', string(10), vk_right);
key[11,0] = ini_read_string('keys', string(11), vk_up);
key[12,0] = ini_read_string('keys', string(12), vk_down);

ini_close();
in your script ive simply removed few lines like rcontroll lcontroll etc.
im sure itsn ot in the settings cus like i said it returns the values as they are and im also using the key[] vars to move around the menu.
 

GMWolf

aka fel666
what are you doing? You are not passing a keycode, you are passing a string!
Those a two different things!
 

GMWolf

aka fel666
how do i pass it rightly?
Do you undestand the difference between a string and a keycode?
When you read from e inifile, you are getting a string. That is, a series of characters.
But, what you want are the keycodes vk_left, etc. those are real values, numbers.

You have to find a way to convert your strings into the numbers. (I cannot remember if GML allows you to switch over strings...)
 
M

Muharif Al Hanif

Guest
Maybe you can do like this...
Code:
key[0,0] = ini_read_real('keys', string(0), ord('A'));
...
Or...
Code:
key[0,0] = real(ini_read_string('keys', string(0), ord('A')));
...
 
Z

zendraw

Guest
yea i know what a real number and a string is, i just didnt know what was being passed. but i fixed it now and thanx.
 
Top