I Wanna Be The Forums!

Please login or register.

Login with username, password and session length
Advanced search  

News:

Gaiden 1.2 patch released. Click here to download!

Author Topic: How to make controls changeable (Game maker)  (Read 12730 times)

chaosrain37

  • Jr. Member
  • **
  • Posts: 79
  • Dont care
    • View Profile
How to make controls changeable (Game maker)
« on: November 06, 2008, 11:12:43 pm »

To make your keys changeable, you must threat your keys as variables.

Each single key in your keyboard has a number associated to it. For example:

Shift = 16
Enter = 13

Just to give some examples.

So... to make a variable for the jumping button, you can create a spriteless object, and add a "Game Start" event to it. Then, go to the control tab and put the execute a piece of code action inside. Then you'll have a notepad-ish blank space where you can write code. This code you type in will execute ONLY when the game starts.

Now, on to initializing the variables, you can type:

global.jump=16

This way, the variable jump will be set to 16, which is the shift key. You can then add more variables:

global.left = 37
global.right = 39
global.jump = 16

There. 37 and 39 are, respectively, the left and right arrow keys. By writing this, you've just set up the default keys (Also, note that before the variable's name, I wrote global and a dot. This indicates game maker that it is a global variable, and makes it persistent between rooms and objects, so basically, you can read and write on it anytime, anywhere).

Now, by just adding these, the character won't just start moving, you need to make it so that the character object checks when the corresponding button is pressed, sort of like adding a "If left key is pressed" event.

Add a step event to your character object. Step event is an event that happens every frame that the game runs. So, were gonna make your character check for the button presses every damn frame.

To make it check for these, go to the control tab, and add a "If an expression is true" action. Then you will get a text box. In that text box, you must tell it what it has to check for. For example:

keyboard_check(37)

This will check if the left button is being pressed. The exact same as a keyboard left event. Now this also works if we put the name of the variable in:

keyboard_check(global.left)

Then the game will go something like this:

"What? global.left isnt a keyboard number... Oh I see! It's a global variable. How much is global.left worth? 37, ok it makes sense now so, If 37 is pressed then..."

So, it will replace global.left with its numerical value, and then will check if the corresponding key is being pressed. You can use the following checks to have different results:

keyboard_check(); The same as the keyboard event
keyboard_check_pressed(); Same as Key press event
keyboard_check_released(); Same as Key release event

Ok, now that you've told it what button to check for, you now gotta program the actions for it in case the check returns true. Doing this is really easy. On the control tab, there are two purple arrows. These work like the parenthesis, put an up arrow and a down arrow right after the keyboard check and you're all set. I'll explain the current run to you:

"Keyboard_check(global.left)

Oh! The key assigned to global.left is being pressed! I must do something... I know! Let's execute whatever there is in between these 2 arrows"

Basically, now you just gotta put actions in between the 2 arrows. If the keyboard_check returns true, it will execute those actions.

Have you changed your keys to variables yet? Great! Now we can go on with the tutorial.

In game maker, there exists a variable that keeps the last pressed key number. For example, if you pressed right, then that variable would turn into 39. This is the variable I'm talking about:

global.keyboard_lastkey

So, if you wanted to change a key to another one lets say... "global.jump". By default it has the 16 value but if we wanted to change it to something else we would put in a code:

global.jump=global.keyboard_lastkey

This will change the global.jump key number to the last key that was pressed.

Now to give you an idea on how to implement this, you could make and options screen, with clickable objects. Clicking them takes you to a different room. For example, you could make an object, that when you click it, it takes you to the jump changing room. You can then put an object in that room with an "any key" event. That way, it would execute actions as soon as you press any key. The action you want it to execute is the key changing action so:

Put an "execute a piece of code" action inside, and make it run the code mentioned earlier:

global.jump=global.keyboard_lastkey

Then, as soon as you press any key, the game will change "global.jump" to the last key pressed, so, that any key will be the key you want to use. I also suggest you add a change room action so that you take the player back to the options screen or something, else, they will be stuck there.

Anyways, you gotta make a room for each of your control variables. They will all execute the exact same code except for one thing: The variable to change. You'll get something like this:

Room 1:

global.jump=global.keyboard_lastkey

Room 2:

global.left=global.keyboard_lastkey

Room 3:

global.right=global.keyboard_lastkey

And so on. Make as many rooms as needed. Good luck!
Logged

Rad

  • The Guy
  • *****
  • Posts: 2223
  • Radical Dood
    • View Profile
Re: How to make controls changeable (Game maker)
« Reply #1 on: November 07, 2008, 02:26:15 am »

thankyou, i hope someone will make me happy by using this.
Logged
"I want Rad's dick."

Koppis

  • The Guy
  • *****
  • Posts: 1344
  • JAKE ON URPO
    • View Profile
Re: How to make controls changeable (Game maker)
« Reply #2 on: November 07, 2008, 09:04:46 am »

___________________________________________
ERROR in
action number 2
of  Step Event
for object napit1:

Error in code at line 1:
   global.left2=global.keyboard_lastkey;

at position 21: Unknown variable keyboard_lastkey


:/

I'm making 3 different objects. When one is pressed, it checks the variable "checking" true and then I can choose the key.

EDIT:
A HA!
Its a flaw in your tutorial...
Its not "global.keyboard_lastkey"
its "(keyboard_lastkey)"

« Last Edit: November 07, 2008, 09:24:33 am by Kòppís ^^, »
Logged
lol

Ellipsis

  • Ellipsis
  • The Guy
  • *****
  • Posts: 1907
    • View Profile
    • If Not Equal Limited
Re: How to make controls changeable (Game maker)
« Reply #3 on: November 08, 2008, 05:42:35 pm »

great turorial! should deferntly be sticked. the only problem is that now you need to code the movement actions all in the step event which it almost inpossibly anoything to do useing drag and drop (another reason why code is king) is there a action in the step event you can use to trigger the left arrow key event  ect.? I think there is but i cant remember it.. oh well thant agen that tutorial was very useful.
Logged

Koppis

  • The Guy
  • *****
  • Posts: 1344
  • JAKE ON URPO
    • View Profile
Re: How to make controls changeable (Game maker)
« Reply #4 on: November 09, 2008, 05:54:06 am »

I made it very simple, no need for different rooms.
check it out!
http://www.savefile.com/files/1877857
If you want help just ask me or msn me kappis3@luukku.com
Logged
lol