1 (edited by pelantas 2018-05-07 13:00:53)

Hi all,

As the title sais, how can i build global hotkeys in bgt? I can include the hotkeys in every script i have build, but that is very inefficient. So i am very curious if it is possible to build a seperate script, where the various hotkeys, like one to check health, energy etc are present, and how i can use those hotkeys in other parts of the games code.

I hope to hear from someone soon smile

Greetz mike

we are back in active business, visit our homepage at:
http://www.nonvisiongames.com
http://www.facebook.com/mikeygamezone
or follow @mikeschippernl on twitter

Thumbs up

2

You can make a file and in that file make a function that runs all you key press checks (like this):
void check_key_presses() {
if (key_pressed(KEY_SPACE)) {
alert("alert", "You pressed space.");
}
if (key_pressed(KEY_RETURN)) {
alert("alert", "You pressed enter.");
}
}

And then include it in your other scripts like this:
#include "key_presses.bgt"

// and someware in your game code.
check_key_presses();

Hope this answers your question.
- NicklasMCHD

I'm me. Just me. No one else. Only meeee!

Thumbs up

3

Make a function for your global hotkeys, and call that function in every loop that blocks normal input.
If you need it to be available in dynamic menus, have it take a dynamic_menu as a parameter, and the user data string, and use the set_callback method. Or, if you already have a callback for menus, have the callback invoke the hotkeys function.
Ideally, you should avoid having loops that block background operations like this, which would allow you to avoid having to paste global_hotkeys(); at the start of every while-loop.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

4

Hi,

@2

i have tried your solution, but it doesn't work yet.

@3
Could you please expand a little more on that? and probably give me an example on how to achieve this?

Hope to hear from you soon smile

Greetz mike

we are back in active business, visit our homepage at:
http://www.nonvisiongames.com
http://www.facebook.com/mikeygamezone
or follow @mikeschippernl on twitter

Thumbs up

5

For example, I always want alt+f4 to close the game, ctrl to silence speech, and pg up and pg down to adjust music volume. So, I always have a function like this:
void system_keys() {
if(key_pressed(KEY_F4) and (key_down(KEY_LMENU) or (key_down(KEY_RMENU))) exit();
else if(key_down(KEY_LCONTROL) or key_down(KEY_RCONTROL)) voice.stop();
if(key_pressed(key_prior)) change_bgm_volume(5);
else if(key_pressed(KEY_NEXT)) change_bgm_volume(-5);
}

If I have a while loop, the first line is always system_keys(); ex:
while (contmenu) {
system_keys();
bool speak=true;
if (key_pressed(KEY_DOWN)) menu_x++;
else if(key_pressed(KEY_UP)) menu_x--;
else if (key_pressed(KEY_ENTER)) {
contmenu=false;
select_menu_item(menu_x);
speak=false;
}
else speak=false;
if (menu_x<0) menu_x=0
else if (menu_x>=menu_items.length()) menu_x=menu_items.length()-1;
if (speak) voice.speak_interrupt(menu_items[menu_x]);
wait(10);
}

Or, if I'm using a dynamic_menu:
int callback ([email protected] menu, string data) {
system_keys();
// whatever you need the callback to do.
return 0;
}

And you'd use menu.set_callback(callback, data); to set the callback, so that system_keys still works within dynamic_menus.

And if you were using a more event-based system, I would probably put system_keys at the top of your key_pressed function.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

6

Hi,

@cae_jones,

When i try your example of the int callback together with the main_menu.set_callback(callback, data); suggestion, i get the following error log:

File: C:\Users\mikes\Dropbox\Public\your_advemture development kit\code met castlevania add-on\your_adventure_beta_2_by_nonvisiongames.bgt
On line: 240 (1)
Information: Compiling void main()

File: C:\Users\mikes\Dropbox\Public\your_advemture development kit\code met castlevania add-on\your_adventure_beta_2_by_nonvisiongames.bgt
On line: 251 (5)
Line: int callback ([email protected] main_menu, string data)
Error: Expected '('

File: C:\Users\mikes\Dropbox\Public\your_advemture development kit\code met castlevania add-on\your_adventure_beta_2_by_nonvisiongames.bgt
On line: 251 (5)
Line: int callback ([email protected] main_menu, string data)
Error: Instead found 'callback'
--- end of errors

I am very curious on what i am doing wrong, i have to admit callbacks are totally new to me. Or at least, this method.

greetz mike

we are back in active business, visit our homepage at:
http://www.nonvisiongames.com
http://www.facebook.com/mikeygamezone
or follow @mikeschippernl on twitter

Thumbs up