Gw Temp

Menu

Tutorial - 'RPG Basics Part II: Keyboard Input' by Kuro

An item about Gamemaker posted on

Blurb

Kuro strikes again, with another great RPGmaking Gamemaker tutorial!

Body

Rpg Basics in Game Maker: Keyboard Input

Back again for another tutorial, but first I'd like to say a few things. One, I'm writing this tutorial after playing GZ Storm's Gythol Granditti - The Crypt of Darkness. It totally blew me away. It was very enjoyable to say the least and as I was playing, I wondered "How did he program his keyboard routines?" Anyway, to understand what I'm talking about, go and play his game, you won't be disappointed. Two, ...err, well there is no two. So let's get started.

Keyboard Input is always one of the biggest problems in Game Maker, at least for me. The drag and drop schema Game Maker is designed for seems like a good idea at first, especially for people who are new to Game Maker, but honestly, it isn't. Maybe for simplistic games yes, but for RPG's, heck no! So as I was testing my game, it suddenly hit me. So now I share my knowledge with you.

Part I: INI Files

This is the first part of my grand idea. If you people have already been doing this, curse you for not telling me. Anyway, if you don't know what an INI file is, don't sweat it, I don't either and I'm a computer technician. I can't remember if INI stands for anything but what I do know is that its a configuration file used to hold values in a text format rather than binary. Now there is no file protection to keep people from editing INI files, but after we're done, most people won't know how to edit it anyway. Here's what an INI file looks like:

[General]
name = Kuro
school = NONE HAHA SUCKERS!
height = 5'7
ethnicity = Black

[Attributes]
strength = 16
dexterity = 15
constitution = 17
intelligence = 12
wisdom = 19
charisma = 3

And that's an INI file. Let me run through it so you understand some of the key parts (or if you know what an INI file is, skip to Part II and ignore this boring stuff.)

[General] is a Section Name. (BTW I'm not using any technical terms here because I don't know JS about INI files.) A Section Name does what its name implies. It defines a section. name, school, height, and ethnicity are Key Names. Key Names are like variables in any programming language, but at this point, they're only for looks. Kuro, NONE HAHA SUCKERS, 5'7, and Black are Key Values. Key Values are the most important part of the INI file because all programs that use INI files will use these values.

Now the whole point of the second section titled [Attributes] is there because I'd like to point out the two types of variables Game Maker can decode from an INI file. There are Real values (all existing numbers) and String values (words and characters). We need to be careful when we ask Game Maker to get these values from the INI file. If you ask to retrieve a string where what's supposed to be a real value, then you're going to end up with a string containing the real value. Also note that real values with foreign characters in them, like 5'7, won't read as a real value. I don't know what Game Maker will do if you try it, so be careful with that as well. Now I'm done explaining this stuff.


Part II: The Scripts

To utilize the INI file, we'll be using a few Scripts to help us out. I've wrote the scripts myself, and they're actually quite small. Read this over:


// Script-> LoadBindings(ini_file_name)
// Created by Brandon Outlaw

// Open the INI file specified by the passed argument - argument0
ini_open(argument0);

// The simple part, reading in the values. The arrays you see are global and hold each character's key bindings
for(i=0;i for(j=0;j<10;j+=1){ // 10 is equivalent to the number of keys I want to load. I always use a ten-key config: Left,Right,Up,Down,A,B,C,X,Y,Z
global.player_keys[i,j] = ini_read_real("Keys_Player_" + string(i), "Key_" + string(j), -1);
}
}

ini_close();


That's the first Script, LoadBindings(). All it does is open the specified INI file and read the values into the global.player_keys[] array. Global.player_keys[] is a two dimensional array in case you didn't understand the whole player_keys[i,j] thing. Before I get into any more detail, check out the next Script, SaveBindings()


// Script-> LoadBindings(ini_file_name)
// Created by Brandon Outlaw

// Open the INI file specified by the passed argument - argument0
ini_open(argument0);

// The simple part, writing in the values. The arrays you see are global and hold each character's key bindings
for(i=0;i for(j=0;j<10;j+=1){ // 10 is equivalent to the number of keys I want to load. I always use a ten-key config: Left,Right,Up,Down,A,B,C,X,Y,Z
ini_write_real("Keys_Player_" + string(i), "Key_" + string(j), global.player_keys[i,j]);
}
}

ini_close();


Now how simple was that? Its practically the same thing as LoadBindings(), but it writes values to the INI file instead of reading it. Simple right? Now of course, we don't want to input the values through notepad or such, so we'll let the game do it for us. I present to you, another Script: ResetBindings()

// Script-> ResetBindings()
// Created by Brandon Outlaw

// NOTE: You might want to call SaveBindings() after you ResetBindings() ONCE to get your INI file's keys filled out

// Player 1's default bindings
global.player_keys[0,0] = ord('H');
global.player_keys[0,1] = ord('K');
global.player_keys[0,2] = ord('U');
global.player_keys[0,3] = ord('J');
global.player_keys[0,4] = ord('A');
global.player_keys[0,5] = ord('S');
global.player_keys[0,6] = ord('D');
global.player_keys[0,7] = ord('Q');
global.player_keys[0,8] = ord('W');
global.player_keys[0,9] = ord('E');

// Player 2's default bindings
global.player_keys[1,0] = vk_left;
global.player_keys[1,1] = vk_right;
global.player_keys[1,2] = vk_up;
global.player_keys[1,3] = vk_down;
global.player_keys[1,4] = vk_numpad4;
global.player_keys[1,5] = vk_numpad5;
global.player_keys[1,6] = vk_numpad6;
global.player_keys[1,7] = vk_numpad7;
global.player_keys[1,8] = vk_numpad8;
global.player_keys[1,9] = vk_numpad9;



That's it. As you can see, each key up to the last one is set to a corresponding ASCII value on the keyboard. The function ord() is a built-in function that can be used to return an ASCII value from a specified character. So now when you call this function, the player_keys[] array will be filled out with default values (which you can change) and all you have to do is call SaveBindings() afterwards to get your INI file filled out. Pretty sweet eh? Well, this tutorial is getting boring again so let's wrap things up.


Part III: The Meaning of Purpose

Oh wait, its actually 'The Purpose of Meaning' but ah well. We have three scripts that can manipulate load preset values and we have full control over what those values are, but how do we use them in a game? Well, there are many approaches to this, and not one way is the best I can assure you, but the way I've done it works pretty damn well. Check it: In my 'player' object, I create an array called keys[10] (10 values remember) and set them all to 0 in the creation event. During the step event, I create a 'for' loop. Now inside that for-loop, there's one line of code that does everything I need it to:

-> self.keys[global.player_keys[self.pID,i]] = keyboard_check(global.player_keys[self.pID,i]);

That is an excerpt from my game. self.pID holds the 'player' object's player ID. This variable references which set of keyboard controls the object wants to use, whether it'd be global.player_keys[0,whatever] or global.player_keys[1,whatever]. The keyboard_check function returns a boolean value depending on if the specified key is down.
It may seem weird, but trust me, it works. From here, I call another Script that handles my input accordingly (TIP: Keep your input processing separate from your player object(s)) and then processes the Player's state. That's basically how its done boys and girls. Let me part with a few words of wisdom.


Part IV: WORDS TO THE UNWISE

This tutorial was aimed at people who wanted to extend their knowledge of Game Maker. But if you were expecting a bunch of more code to illustrate the finer points of processing keyboard input, then look elsewhere. For the last 4 years, I've worked my arse off trying to understand programming. It finally hit me when I realized that it wasn't all about the code and syntax, but the logic behind the game. Regardless of your knowledge at GML or any programming language out there, what really counts is your ability to problem-solve as you create your game. Design before coding is essential if you want to make anything remotely successful. Its the golden rule of game developers everywhere, at least, it should be.

Back to the point, keyboard input isn't as hard as one would think. In reality, its probably the easiest part when designing a game of Game Maker proportions. As you finish reading this tutorial, I hope I've put a few seeds of imagination in your brain to help you develop a few tricks to make your game better and easier to code. Lately I've had a rush of ideas lately and I'll be sure to share each of them with you. But right now, my hands are hurting pretty bad. Peace out.