Jump to content


Photo
* * * * * 2 votes

Customizable Controls Example By Nocturne


  • Please log in to reply
47 replies to this topic

#1 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 19 January 2009 - 11:35 AM

  • Title: Customizable Controls Example
  • Description: How to map keys and use an ini to save them...
  • GM Version: GM7pro / GM8pro
  • Registered: Yes (Should be compatible with GM6 and GM7Light)
  • File Type: gmk and ini files (zipped)
  • File Size: 0.31mb
  • File Link: link.png
  • File Link GM8: link.png
Additional Info
This example is an introduction to customizable controls and basic use of ini files and is recommended for intermediate users. It is for anyone who wishes their game to have a user defined keys menu and should work in GM6 and GM7light but I have not tested it so it may be that GM7pro is necessary.

There are two objects (one for mouse and another for keyboard, they are both basically the same, only the way of selecting the keys to map changes) which use arrays to define the keys and then save the values to an ini file. The ini file must always be located in the same directory as the game file.

Before starting to modify this please open the ini file in a text editor to see how it is structured, then read through all the codes and comments to make sure you understand everything.

This can be merged into any game (delete everything except the keyboard_object that you want to use and the scripts) but will need modifying to adapt to your needs. Pay particular attention to the arrays and drawing options...

Thank you for downloading this and as always, no credit is necessary but would be apreciated! Oh, and please comment if you download this...

Nocturne

EDIT: Kvegas83 has provided an optimised (and easier to change) script here... Thank you!

Edited by Nocturne, 27 June 2013 - 08:54 AM.

  • 7

#2 tamper2

tamper2

    GMC Member

  • New Member
  • 369 posts

Posted 20 January 2009 - 10:18 AM

Very nice tutorial. :blink:
Built very well, and well commented.
Very easy to understand and follow comments. :)
Only mistake I've seen, is for saving into ini, you used Backspace instead of Escape. :blink:
Overall really good,
Tamper :blink:
  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 20 January 2009 - 10:38 AM

Thanks tamper2... I really apreciate the comments! Oh, and I used backspace so that anyone who didn´t want to save to the ini could press escape and exit the program without changing anything.
  • 0

#4 Diptoman

Diptoman

    GMC Member

  • GMC Member
  • 1849 posts
  • Version:GM:Studio

Posted 22 January 2009 - 05:03 PM

Hmm, I already figured how to do keyboard mapping, the only problem was that I couldn't find out a way to actually show what key is assigned for which action, except for the long and tedious method of writing each keycode (like you have done in this).
Oh, well! Just thinking if there was any other method...
But I can always copy the keycodes from here :( . So yeah, this was helpful in a way.

You have commented well enough making it easy to understand as well (Only applies to people who actually want to understand rather than copy-paste stuff). Should be very much useful for many. Good work.

Edited by dmsuper, 23 January 2009 - 06:39 AM.

  • 0

#5 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 22 January 2009 - 05:18 PM

Thanks dmsuper! I respect your replies in all topics and it´s nice of you to comment...

I actually made a quick program in GM to write all the key codes to a file after a keypress then started in the upper left of the keyboard and pressed every key one at a time. It was the only way I could think of to get the numbers... The boring part was then putting all that info into a script with key names as well! At least the work hasn´t gone to waste and other people can benefit from it...

Thanks again!
  • 0

#6 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 04 February 2009 - 09:33 PM

200 downloads and 2 replies? C´mon guys a bit of respect...

Edited by Mark13673, 19 February 2009 - 10:31 AM.

  • 0

#7 Obj_Control

Obj_Control

    GM Mentor

  • New Member
  • 2561 posts

Posted 24 February 2009 - 12:34 AM

This is very useful. I never really bothered learning how to write .ini files, but now I know. There was plenty enough comments to help me understand how it works, and I think the methods were very good. Except, a few times, I see you writing a whole code 10 times, instead of using if statements to change a value, then writing the code once.

This is going to help me a lot in my next game, and I hope I won't lose it. I may finish the list of keys, and I may not, but if I do, I'll post it here for others.

Thanks for making the tutorial.

8/10
  • 0

#8 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1956 posts
  • Version:GM8

Posted 24 February 2009 - 01:20 AM

I agree with obj_control.
This is very well documented. I already knew how to make customizable controls, but this will be of great use to people who didn't know this yet.
  • 0

#9 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 24 February 2009 - 07:01 AM

Thanks guys! Nice comments... and as for the code, obj_control, you are right that the draw event can be optimised with a switch statement and a for loop, but as I want this to be used by anyone I thought that it would be easier to change the drawing positions etc... if I laid out the code in the simplest form possible... And feel free to add to the key list... I left some out as I felt that they were problematic or unnecessary but I suppose I should have put a complete list for the end user to choose from...
  • 0

#10 samthetrue

samthetrue

    GMC Member

  • New Member
  • 8 posts

Posted 03 March 2009 - 07:14 PM

Very well documented. As a beginner, this has taught me many things about saving information as well. Thank you very much, I hope to learn more from your other examples, and I'm off to download and study them right now



Keep up the good work!
Sam
  • 0

#11 Batch

Batch

    GMC Member

  • GMC Member
  • 728 posts

Posted 07 March 2009 - 09:39 PM

Haha, I took about 2 hours "decompiling" all this info into something that I could understand! Yay! Now it works lol. :mellow: If you wanna see this in action, click on my support bar in my sig and check out v2.75. Love this example! Thanks Mark!


-CubinJ
  • 0

#12 safwat1995

safwat1995

    GMC Member

  • New Member
  • 394 posts

Posted 02 April 2009 - 10:08 AM

Great example. well done :)
  • 0

#13 frankpiet

frankpiet

    ^destroyed evil chicken^

  • New Member
  • 760 posts

Posted 02 April 2009 - 08:58 PM

Good example. This will help a lot of people.
  • 0

#14 ShadowKirby10

ShadowKirby10

    GMC Member

  • New Member
  • 24 posts

Posted 07 August 2009 - 11:41 PM

Example is straight forward, Nice work, and yes it works great on the pro edition too
  • 0

#15 iluvfuz

iluvfuz

    GMC Member

  • GMC Member
  • 1452 posts
  • Version:GM:Studio

Posted 08 August 2009 - 12:34 AM

Cool, this can teach people about ini reading/writing, arrays and loops.
  • 0

#16 Pymann

Pymann

    GMC Member

  • GMC Member
  • 467 posts
  • Version:GM:Studio

Posted 11 August 2009 - 11:58 PM

It requires pro for GM7 not sure about GM6
I have GM6 Registered on one computer GM7Lite on another cuz it won't accept the registration code I gave it.
  • 0

#17 kvegas83

kvegas83

    GMC Member

  • GMC Member
  • 84 posts
  • Version:GM:Studio

Posted 07 September 2009 - 10:42 PM

hey Mark... made a little modification to your code, making it obsolete to have to create a whole other program to show you the keycodes. Here's the new KeyMapCheck script, though it's formatted a bit differently and has a few differences in what keys can be mapped. It also prevents the need to constantly assign Mapping to true.

//GET LAST KEY PRESSED AND CORRESPONDING CODE
//IF KEY IS NOT LISTED, IT IS CONSIDERED AN INVALID ENTRY

Mapping=true;

switch keyboard_lastkey{

case vk_control:	name="Ctrl";break;
case vk_tab:		name="Tab";break;
case vk_space:	  name="Space";break;

//ARROW KEYS
case vk_left:	   name="Left";break;  case vk_right:	  name="Right";break;
case vk_up:		 name="Up";break;	case vk_down:	   name="Down";break;

//NUMPAD
case vk_numpad0:	name="Num0";break;  case vk_numpad1:	name="Num1";break;
case vk_numpad2:	name="Num2";break;  case vk_numpad3:	name="Num3";break;
case vk_numpad4:	name="Num4";break;  case vk_numpad5:	name="Num5";break;
case vk_numpad6:	name="Num6";break;  case vk_numpad7:	name="Num7";break;
case vk_numpad8:	name="Num8";break;  case vk_numpad9:	name="Num9";break;

//LETTERS
case ord('A'):	  name="A";break;	 case ord('B'):	  name="B";break;
case ord('C'):	  name="C";break;	 case ord('D'):	  name="D";break;
case ord('E'):	  name="E";break;	 case ord('F'):	  name="F";break;
case ord('G'):	  name="G";break;	 case ord('H'):	  name="H";break;
case ord('I'):	  name="I";break;	 case ord('J'):	  name="J";break;
case ord('K'):	  name="K";break;	 case ord('L'):	  name="L";break;
case ord('M'):	  name="M";break;	 case ord('N'):	  name="N";break;
case ord('O'):	  name="O";break;	 case ord('P'):	  name="P";break;
case ord('Q'):	  name="Q";break;	 case ord('R'):	  name="R";break;
case ord('S'):	  name="S";break;	 case ord('T'):	  name="T";break;
case ord('U'):	  name="U";break;	 case ord('V'):	  name="V";break;
case ord('W'):	  name="W";break;	 case ord('X'):	  name="X";break;
case ord('Y'):	  name="Y";break;	 case ord('Z'):	  name="Z";break;

//UNCHARTED KEY
default:			Mapping=false;break;
}
if Mapping		  code=keyboard_lastkey;

  • 4

#18 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 08 September 2009 - 06:29 AM

Thanks for taking the time to do that! I should have done so myself (I´m much more experienced now with GML) as I also realised that the code could be optimised in more or less the same way a while back... Just never got round to it! The only addition I would make is adding in vk_shift....

Anyway, thanks again and I will add a mention into the first post...
  • 0

#19 kvegas83

kvegas83

    GMC Member

  • GMC Member
  • 84 posts
  • Version:GM:Studio

Posted 09 September 2009 - 05:22 AM

Thanks for taking the time to do that! I should have done so myself (I´m much more experienced now with GML) as I also realised that the code could be optimised in more or less the same way a while back... Just never got round to it! The only addition I would make is adding in vk_shift....

Anyway, thanks again and I will add a mention into the first post...


Hey, glad to have helped! I really like this example, its helped me alot.
  • 0

#20 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 13 December 2009 - 01:08 PM

BUMP!
  • 0

#21 CRxTRDUDE

CRxTRDUDE

    GMC Member

  • GMC Member
  • 64 posts
  • Version:GM8

Posted 23 March 2010 - 02:32 AM

Hello. I replied on my post by the way saying thanks to you. By the way, can i modify the code used? I want to learn how to code GML. And If I have problems, can you help me in my post. Very much appreciated if you can help.
  • 0

#22 Devilfromhost12

Devilfromhost12

    GMC Member

  • GMC Member
  • 735 posts

Posted 23 March 2010 - 10:34 PM

Once again Nice Work Mark13673
  • 0

#23 Nial8r

Nial8r

    GMC Member

  • New Member
  • 341 posts

Posted 07 August 2010 - 11:55 PM

I have decided my post was too stupid and 'removed' it . :P


Edited by Nial8r, 12 May 2011 - 11:41 AM.

  • 0

#24 regniwekim

regniwekim

    GMC Member

  • GMC Member
  • 223 posts
  • Version:GM:Studio

Posted 23 August 2010 - 09:32 PM

This may sound like a bit of a noobish question...

But here it goes anyways...

Once I have the custom keys selected, how do I apply them?
  • 0

#25 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 23 August 2010 - 09:52 PM

Hi there regniwekim! The use is easy... Just use the global variables for moving or whatever... EG:

//TO MOVE LEFT
if keyboard_check(global.LeftKey) x-=4;

//TO END THE GAME
if keyboard_check_pressed(global.QuitKey) game_end();

//etc...

I hope that helps!
  • 0

#26 regniwekim

regniwekim

    GMC Member

  • GMC Member
  • 223 posts
  • Version:GM:Studio

Posted 24 August 2010 - 02:58 AM

Hi there regniwekim! The use is easy... Just use the global variables for moving or whatever... EG:

//TO MOVE LEFT
if keyboard_check(global.LeftKey) x-=4;

//TO END THE GAME
if keyboard_check_pressed(global.QuitKey) game_end();

//etc...

I hope that helps!



Awesome. That's exactly what I was asking about. Thanks a lot!
  • 0

#27 caiys

caiys

    GMC Member

  • GMC Member
  • 83 posts
  • Version:GM:Studio

Posted 03 October 2010 - 03:19 PM

It works flawlessly and the code is all nicely commented so was easy to integrate into my game.
  • 0

#28 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 08 April 2011 - 08:14 AM

Since Ive changed my user name Im bumping my examples so people can see them... Posted Image
  • 0

#29 Hepo

Hepo

    GMC Member

  • New Member
  • 3 posts

Posted 12 May 2011 - 04:12 AM

2 years later and I say thanks!
  • 0

#30 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 21987 posts
  • Version:GM:Studio

Posted 12 May 2011 - 06:21 AM

Better late than never! Thank you...
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users