Smarter Variable Control
Posted: Sun Apr 10, 2011 4:19 pm
Ello' everybody! Me again :) this time, I've decided I'll bring you all a nice little tip about Variable Control.
At first this can be a little boring, but bare with me and make this little Space button program with me :D In doing so you'll learn how to use Arrays as multiple variables with Define and sturff like that :3
||||||||||
Let's get started!
Step 1: Creating the maximum ammount of variables your game will be using.
Step 2: Naming your variables
Step 3: Using the variables
hope this helped, this will also help in reducing game size as well.
- For those of you who don't know what arrays are, thats okay! This tutorial will show you ^^
At first this can be a little boring, but bare with me and make this little Space button program with me :D In doing so you'll learn how to use Arrays as multiple variables with Define and sturff like that :3
||||||||||
Let's get started!
Step 1: Creating the maximum ammount of variables your game will be using.
- Most games don't use that many variables, if your lucky you'll use over 500 of them, but why make your game so bulky with so many variables? Today we will be running an entire game off of only just a FEW variables! For now, go ahead and open up Global Code.
- Code: Select all
int Variables[100];
short int Switches[100];
char *KEYS;
Good! now, inside of there type the following code:
Step 2: Naming your variables
- Good, now your on your way to using just a few variables to control the game's outcome. Click "Add" in the global code if you havent already, then type this code in
- Code: Select all
#define MOVE_SPEED Variables[0]
#define JUMP_HEIGHT Variables[1]
#define MOVE_ACTIVE Switches[0]
#define JUMP_ACTIVE Switches[1]- Code: Select all
#define MOVE_SPEED Variables[0]
#define TEMP01 Variables[1]
#define SPC_TMP Variables[2]
#define MOVE_ACTIVE Switches[0]
Notice how the [0] and [1] keep going up? Basically every time we #define (Name of define here), we named the variable for that number in the array.. Or atleast I think thats how you'd say it :P In other words, we no longer have to use "Variables[0];" when we want to use it, simply call it with MOVE_SPEED. But for now make yours look like this.
Since we are only going to be using these variables and switches for now, we'll stick with those 4.
IMPORTANT:
Do not include the " ; " symbol after the #defines.
Step 3: Using the variables
- Create an actor called "Test". Now, go into Create Actor, and type "MOVE_SPEED=5;", and click add. Now go to Draw Actor, and type this following code in.
- Code: Select all
KEYS=GetKeyState();
if (KEYS[KEY_SPACE]==1 && SPC_TMP==0) {
TEMP01++;
SPC_TMP=1; }
else if (KEYS[KEY_SPACE]==0 && SPC_TMP==1) {
SPC_TMP=0; }
switch(TEMP01) {
case 0:
MOVE_ACTIVE=0; break;
case 1:
MOVE_ACTIVE=1; break;
case 2:
TEMP01=0; break; }
if (MOVE_ACTIVE==1) {
xvelocity=MOVE_SPEED; }
else if (MOVE_ACTIVE==0){
xvelocity=0; }
I know it may look like a lot, but it's not :) Now you can playtest the game! :D Press Space to activate his movement, then again to stop him from moving. To change his speed, simply make MOVE_SPEED different :)
hope this helped, this will also help in reducing game size as well.