|
Title: Using WASD in Game Maker Post by: Rat Casket on June 16, 2012, 03:24:47 PM Hello! I hope this is in the right place! I am extremely new to... well... everything. I've picked up Game Maker as a way to sort of teach myself some basic programming stuff before moving on to a full language. It also provides a nice environment to teach myself simple animation, pixel art, and level design. I apologize in advance for my vast ignorance.
I'm working on my first "engine" for a side scrolling brawler/beat em up and I have hit a snag when it comes to movement. The code works just fine when using the arrow keys, which is the code posted below. However, if I replace the (vk_key) to (ord('A') etc, my character only moves at the correct speed backwards and up, but extremely slow forwards and down. I'm not sure why this is. Could somebody take a look at this code and help me understand why this is happening? Or, if possible, provide some completely different code to accomplish the same thing? Thank you so much! Code: // character movement vx = (keyboard_check(vk_right) - keyboard_check(vk_left)) * 10; vy = (keyboard_check(vk_down) - keyboard_check(vk_up)) * 10; if (place_free(x, y + vy)) { y += vy; } if (place_free(x + vx, y)) x += vx; Title: Re: Using WASD in Game Maker Post by: ஒழுக்கின்மை on June 16, 2012, 05:07:37 PM it's because you can't rely on those functions to only return either a 1 or a 0. sometimes they might return a fractional value (or a -1). i'd suggest not using math like that and just using if statements
Title: Re: Using WASD in Game Maker Post by: moi on June 16, 2012, 07:52:40 PM don't use WASD only
Title: Re: Using WASD in Game Maker Post by: ink.inc on June 16, 2012, 08:50:13 PM Yeah; not everyone uses QWERTY keyboards; our more international users might have a tricky playing your game! Not that you HAVE to cater to them, but it's just something to remember in case you ever want to release your product.
Title: Re: Using WASD in Game Maker Post by: DustyDrake on June 16, 2012, 09:19:27 PM Gang Garrison 2 does this:
Code: if keyboard_check(global.left) && menuOpen == false keybyte |= $40; andif keyboard_check(global.right) && menuOpen == false keybyte |= $20; if keyboard_check(global.jump) && menuOpen == false keybyte |= $80; if keyboard_check(global.attack) && menuOpen == false && global.myself.humiliated == 0 keybyte |= $10; if keyboard_check(global.special) && menuOpen == false && global.myself.humiliated == 0 keybyte |= $08; if keyboard_check_pressed(global.medic) && menuOpen == false keybyte |= $04; if keyboard_check_pressed(global.taunt) && menuOpen == false inputTaunt(); if keyboard_check_pressed(global.special) && menuOpen == false && global.myself.humiliated == 0 inputSpecial(); if keyboard_check_pressed(global.chat1) && menuOpen == false inputChat1(); if keyboard_check_pressed(global.chat2) && menuOpen == false inputChat2(); if keyboard_check_pressed(global.chat3) && menuOpen == false inputChat3(); if keyboard_check_pressed(global.drop) && menuOpen == false inputDrop(); Code: global.jump = ini_read_real("Controls", "jump", ord("W")); global.down = ini_read_real("Controls", "down", ord("S")); global.left = ini_read_real("Controls", "left", ord("A")); global.right = ini_read_real("Controls", "right", ord("D")); global.attack = ini_read_real("Controls", "attack", MOUSE_LEFT); global.special = ini_read_real("Controls", "special", MOUSE_RIGHT); global.taunt = ini_read_real("Controls", "taunt", ord("F")); global.chat1 = ini_read_real("Controls", "chat1", ord("Z")); global.chat2 = ini_read_real("Controls", "chat2", ord("X")); global.chat3 = ini_read_real("Controls", "chat3", ord("C")); global.medic = ini_read_real("Controls", "medic", ord("E")); global.drop = ini_read_real("Controls", "drop", ord("B")); If you want to do character keys for keyboard_check you do ord("Capital letter here") instead of vk_key it's because you can't rely on those functions to only return either a 1 or a 0. sometimes they might return a fractional value (or a -1). i'd suggest not using math like that and just using if statements this also according to the Help files is wrong.Title: Re: Using WASD in Game Maker Post by: ஒழுக்கின்மை on June 16, 2012, 09:20:16 PM the help files get a lot wrong
Title: Re: Using WASD in Game Maker Post by: Evan Balster on June 17, 2012, 06:21:57 AM If you wanted to circumvent this problem you could make a script with a simple name like "key_down" and the following code:
Code: if (keyboard_check(argument0)) return 1; return 0; Then you could do things the way you like using that script instead of the GM function. For bonus points, allow a second parameter so you can call it like key_down(ord('W'), vk_up) to support either scheme. Title: Re: Using WASD in Game Maker Post by: ஒழுக்கின்மை on June 17, 2012, 08:39:55 AM that'd unnecessarily slow the game down; i think it's best to do things in a moderately fast way, gm's function overhead is pretty bad
here is how i do it i set up some constants, KEY_A, KEY_W, etc., each of them equivalent to the ascii code for that key. that voids the function overhead of using the ord() functions. this also allows users to configure their keys if they want to change them, since you'd just have to change the ascii values for those constants then i FIRST check if any key is pressed. this saves an enormous amount of time because checking each key individually is slow. so if "any key" is pressed, then and only then do i run a series of checks on each of the movement keys. these use if statements. if statements are *a lot* faster than using scripts to turn things into a 1 or a 0 i don't change x,y values directly, i use gm's speed and direction object properties. you can do either one, but the way i do it is the keys pressed determine the direction, and whether *any* of the keys is pressed determines the speed Title: Re: Using WASD in Game Maker Post by: DustyDrake on June 17, 2012, 10:20:35 AM ...How do you change constants?
They're kinda... constant and unchanging. Title: Re: Using WASD in Game Maker Post by: ஒழுக்கின்மை on June 17, 2012, 11:07:17 AM in traditional programming languages yeah, but GM is an weird language; it's "semi-compiled" every time you run it, it stores the source code in the .exe and only semi-compiles it to bytecode every time the game is run
Title: Re: Using WASD in Game Maker Post by: Rat Casket on June 18, 2012, 05:53:06 AM Thank you for the input everyone. I was able to find another solution and will post the code later. Turns out one of my friends knows quite a bit about all of this.
Learned a lot from your posts though. |