|
1 registered members (AndrewAMD),
599
guests, and 3
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
re-bindable keys at runtime
#389897
12/19/11 16:27
12/19/11 16:27
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
What do you guys think would be the best method to allow re-binding keys via menu at runtime? I thought of using #define #undef... something like: #define MY_UP_KEY key_w or #define MY_UP_KEY key_cu but reading about #define in the manuall I found this: #defines are only evaluated during compilation. Thus they can't be changed anymore in a published project. This is apparently something much simpler than stuff I have been working on lately... But I don't know why it eludes me, and I cannot think of a way to implement re-bindable keys. Any ideas I could start to work from? EDIT: Nevermind, I found "key_set(number_function);" in the manuall EDIT2: What if after setting a key I want to "un-set" that key before setting it to something else? Example: while (key_any == 0) { wait(1); } // wait until a key is hit while (key_any == 1) { wait(1); } // wait until key is released key_set(key_lastpressed,my_function); // assign function to key
Last edited by Carlos3DGS; 12/19/11 16:43.
|
|
|
Re: re-bindable keys at runtime
[Re: Superku]
#389906
12/19/11 17:35
12/19/11 17:35
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
@Redeemer those functions will be extremely helpfull for the user interface to modify the keys. And @Superku that is an excellent example for using the new modified inputs. Thankyou very much both of you!
As always the community is super helpfull (and the main reason I still use and keep upgrading GS)
EDIT: Another question that just came up as I was looking in the manuall checking the number codes for each key: F1-F10 = 59-68 1-0 = 2-11 Numpad0 - Numpad9 = ??-??
The manuall states the codes for the function keys, and the normal number keys, but not for the numpad keys? Are they not usable or something?
I am making a split screen 2 player game, and both of them using keys 1-0 and F1-F10 to switch weapons would be very akward since one set of keys is directly above the other. I would really like player1 to use the standard wasd + number keys, and player2 to use arrows and numpad...
Last edited by Carlos3DGS; 12/19/11 17:39.
|
|
|
Re: re-bindable keys at runtime
[Re: Superku]
#389922
12/19/11 19:51
12/19/11 19:51
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
There is one thing in your code I don't understand. It might not affect my particular case, but I cannot avoid feeling curious about it... input_left = key_pressed(input_left_key)*player_free*...; What is "player_free" for?
Last edited by Carlos3DGS; 12/19/11 20:06.
|
|
|
Re: re-bindable keys at runtime
[Re: Carlos3DGS]
#389926
12/19/11 20:39
12/19/11 20:39
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
Hm some keyboards (esp. Notebooks) do not support the Numpad natively, so I would not rely on its availability. Nevertheless, you can use f.i. key_lastpressed and DEBUG_VAR to check scan codes of obscure keys. What is "player_free" for? I use something as follows: input_menu_left = key_pressed(input_left_key); input_left = input_menu_left*player_free*...; When I want to disable player movement (on level load/ change, cutscenes, ...) I simply set player_free to 0, 1 otherwise. Menu controls should not be affected by this (in my game), so I differentiate between movement and menu input.
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: re-bindable keys at runtime
[Re: Superku]
#389927
12/19/11 21:00
12/19/11 21:00
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
So its an easy "ignore/pause inputs" method. Interesting, that sounds like something that might be usefull in the future. This is what I have been working on (for 4 payers), adapting it to your method. does it look correct so far?
#define MAX_PLAYERS 4
int player_count=2;
int gamepad_mode[MAX_PLAYERS];
int control_mode[MAX_PLAYERS];
BOOL freeze_keys = 1;
//KEYS THAT CANNOT BE BOUND (already used)
//ESC = 1
//F1-F10 = 59-68
//F11-F12 = 87-88
//PLAYER INPUT CONTROL
BOOL up_input[MAX_PLAYERS];//forward-up
BOOL down_input[MAX_PLAYERS];//backwards-down
BOOL left_input[MAX_PLAYERS];//rotateL
BOOL right_input[MAX_PLAYERS];//rotateR
BOOL jump_input[MAX_PLAYERS];//jump-propulsion
BOOL camleft_input[MAX_PLAYERS];//rotateCamL
BOOL camright_input[MAX_PLAYERS];//rotateCamR
BOOL use_input[MAX_PLAYERS];//use-pickup world object
BOOL itemleft_input[MAX_PLAYERS];//use item in right hand
BOOL itemright_input[MAX_PLAYERS];//use item in right hand
BOOL build_input[MAX_PLAYERS];//build menu
BOOL inventory_input[MAX_PLAYERS];//inventory menu
//PLAYER KEY BINDINGS
int up_key[MAX_PLAYERS];
int down_key[MAX_PLAYERS];
int left_key[MAX_PLAYERS];
int right_key[MAX_PLAYERS];
int jump_key[MAX_PLAYERS];
int camleft_key[MAX_PLAYERS];
int camright_key[MAX_PLAYERS];
int use_key[MAX_PLAYERS];
int itemleft_key[MAX_PLAYERS];
int itemright_key[MAX_PLAYERS];
int build_key[MAX_PLAYERS];
int inventory_key[MAX_PLAYERS];
void key_control()
{
int x;
while(1)
{
for(x=0;x<player_count;x++)
{
if(gamepad_mode[x])
{
//not supported yet
}
else
{
up_input[x]=key_pressed(up_key[x])*freeze_keys;
down_input[x]=key_pressed(down_key[x])*freeze_keys;
left_input[x]=key_pressed(left_key[x])*freeze_keys;
right_input[x]=key_pressed(right_key[x])*freeze_keys;
jump_input[x]=key_pressed(jump_key[x])*freeze_keys;
camleft_input[x]=key_pressed(camleft_key[x])*freeze_keys;
camright_input[x]=key_pressed(camright_key[x])*freeze_keys;
use_input[x]=key_pressed(use_key[x])*freeze_keys;
itemleft_input[x]=key_pressed(itemleft_key[x])*freeze_keys;
itemright_input[x]=key_pressed(itemright_key[x])*freeze_keys;
build_input[x]=key_pressed(build_key[x])*freeze_keys;
inventory_input[x]=key_pressed(inventory_key[x])*freeze_keys;
}
}
wait(1);
}
}
void load_default_keys()
{
up_key[0]=key_for_str("w");
down_key[0]=key_for_str("s");
left_key[0]=key_for_str("a");
right_key[0]=key_for_str("d");
jump_key[0]=key_for_str("space");
camleft_key[0]=key_for_str("z");
camright_key[0]=key_for_str("v");
use_key[0]=key_for_str("shiftl");
itemleft_key[0]=key_for_str("q");
itemright_key[0]=key_for_str("e");
build_key[0]=key_for_str("b");
inventory_key[0]=key_for_str("i");
up_key[1]=key_for_str("cuu");
down_key[1]=key_for_str("cud");
left_key[1]=key_for_str("cul");
right_key[1]=key_for_str("cur");
jump_key[1]=key_for_str("shiftr");
camleft_key[1]=key_for_str("pgup");
camright_key[1]=key_for_str("pgdn");
use_key[1]=key_for_str("enter");
itemleft_key[1]=key_for_str("ctrl");
itemright_key[1]=key_for_str("ins");
build_key[1]=key_for_str("bksp");
inventory_key[1]=key_for_str("del");
}
void load_key_bindings()
{
}
void save_key_bindings()
{
}
Thought it could be usefull to post it for anyone else that stumbles on this thread. I have not used gamepads yet but it seems like a nice adittion so I left that part in for when I do (4 players on 1 keyboard never felt like a good idea, lol)
|
|
|
Re: re-bindable keys at runtime
[Re: Carlos3DGS]
#389933
12/19/11 22:09
12/19/11 22:09
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
load and save functions were incomplete. Adding those two functions for anyone else interested in this thread:
void load_key_bindings()
{
int x;
var keys_file = file_open_read ("keys.bindings");
if(keys_file==0)
{
load_default_keys();
}
else
{
for(x=0;x<MAX_PLAYERS;x++)
{
up_key[x]=file_var_read(keys_file);
down_key[x]=file_var_read(keys_file);
left_key[x]=file_var_read(keys_file);
right_key[x]=file_var_read(keys_file);
jump_key[x]=file_var_read(keys_file);
camleft_key[x]=file_var_read(keys_file);
camright_key[x]=file_var_read(keys_file);
use_key[x]=file_var_read(keys_file);
itemleft_key[x]=file_var_read(keys_file);
itemright_key[x]=file_var_read(keys_file);
build_key[x]=file_var_read(keys_file);
inventory_key[x]=file_var_read(keys_file);
}
file_close(keys_file);
}
}
void save_key_bindings()
{
int x;
var keys_file = file_open_write ("keys.bindings");
for(x=0;x<MAX_PLAYERS;x++)
{
file_var_write(keys_file,up_key[x]);
file_var_write(keys_file,down_key[x]);
file_var_write(keys_file,left_key[x]);
file_var_write(keys_file,right_key[x]);
file_var_write(keys_file,jump_key[x]);
file_var_write(keys_file,camleft_key[x]);
file_var_write(keys_file,camright_key[x]);
file_var_write(keys_file,use_key[x]);
file_var_write(keys_file,itemleft_key[x]);
file_var_write(keys_file,itemright_key[x]);
file_var_write(keys_file,build_key[x]);
file_var_write(keys_file,inventory_key[x]);
}
file_close(keys_file);
}
|
|
|
|