Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
1 registered members (AndrewAMD), 599 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
re-bindable keys at runtime #389897
12/19/11 16:27
12/19/11 16:27
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline 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:
Quote:
#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?
Quote:
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.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: re-bindable keys at runtime [Re: Carlos3DGS] #389899
12/19/11 16:36
12/19/11 16:36
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
key_set() only allows you to set key events. To create expressions involving redefinable input variables, you must use the key_for_str and str_for_key functions.


Eats commas for breakfast.

Play Barony: Cursed Edition!
Re: re-bindable keys at runtime [Re: Redeemer] #389901
12/19/11 17:00
12/19/11 17:00
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
What I use is key_pressed and simply set and save the scan codes in my options menu.
Then I have a function, f.i. init_gamepad(), that checks those scan codes via key_pressed and differentiates between keyboard and gamepad:

while(1)
{
if(gamepad) ..
else
{
input_left = key_pressed(input_left_key)*player_free*...;
input_right = ...
}
wait(1);
}

My movement code simply uses the variables input_left, input_right ... and so on. This way I could easily interchange different keyboard and gamepad inquries without damaging any of my remaining codes.


"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] #389906
12/19/11 17:35
12/19/11 17:35
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline OP
User
Carlos3DGS  Offline 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.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
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 Offline OP
User
Carlos3DGS  Offline 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...
Originally Posted By: Superku
input_left = key_pressed(input_left_key)*player_free*...;

What is "player_free" for?

Last edited by Carlos3DGS; 12/19/11 20:06.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
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 Offline
Senior Expert
Superku  Offline
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.

Quote:
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 Offline OP
User
Carlos3DGS  Offline 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?

Code:
#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)


"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
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 Offline OP
User
Carlos3DGS  Offline OP
User

Joined: Oct 2008
Posts: 513
load and save functions were incomplete. Adding those two functions for anyone else interested in this thread:

Code:
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);
}




"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1