Smarter Variable Control

Game Editor comments and discussion.

Smarter Variable Control

Postby Hblade » 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.
    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.
    SCRN1.jpg


    Good! now, inside of there type the following code:
    Code: Select all
    int Variables[100];
    short int Switches[100];
    char *KEYS;

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]

    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.
    Code: Select all
    #define MOVE_SPEED Variables[0]
    #define TEMP01 Variables[1]
    #define SPC_TMP Variables[2]

    #define MOVE_ACTIVE Switches[0]

    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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Smarter Variable Control

Postby Game A Gogo » Sun Apr 10, 2011 6:28 pm

this is good, it refreshed my memory and should teach new things to less advanced users!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Smarter Variable Control

Postby Hblade » Mon Apr 11, 2011 2:45 am

:D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Smarter Variable Control

Postby lcl » Mon Apr 11, 2011 6:05 am

Cool. :)
Great work, this could help new ones. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Smarter Variable Control

Postby Hblade » Wed May 11, 2011 5:24 pm

Thanks :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest