Make RPG tutorial : chapter II - Part 3/4

     
         
   
Learn Game Programming much faster: join my list! The source code for the game programming tutorials (first chapter 1, then chapter 2...) and more!
You'll be a pro in a couple weeks!
(You must be at least 13!)
 
Name:
Email:
Comment:
 
Privacy policy:  This list is all about the fun and entirely safe.  You can remove yourself permanently in just seconds, anytime you wish.  No one is getting your email from me and there will be no spam!
   
    Part 2 Back to Tutorials Part 4    
         
   

Keep track of your game loop when you make games.

Keeping your game loop in mind is a good way to not get lost or have to spend hours figuring out where to add new code to get a new feature to work.

Our sample game's game loop is already getting a bit complex. That can be intimidating, but it shouldn't. If you keep good track of it, you'll realize it is just a series of logical steps that slowly build up as your game develops.

First the room0 is created, or restarted at each consecutive step, as well as the objects that were added to it.

Things that happen to the character (events such as draw, left key, up key...) cause actions (executing code, running scripts). The scripts and codes can start new events of their own by creating new objects (instance_create()) for example. Scripts can also call new scripts (if certain conditions are met by using an if statement, or without conditions at all).

The game loop restarts at each step. How long a step lasts depends on the room speed you choose in room settings. Default is 30 steps per second, which means a step lasts 1/30th of a second. In this game I changed it to 60 so that the cursor would stay on the topbar. I'll change it back in a later tutorial though... There are other ways.

At each step the room is restarted. All possible events are either checked for (key events) or executed every time (step events, draw events).

Events generated by existing objects

Each object can potentially generate events, just by existing. The step event happens at every step, as well the draw event. Nothing special has to happen.

The main character object events

main_ch is the name of the main character object. If you open its object properties, you'll see four keyboard events, an O key event, a draw event and a space release event.

If one of the arrow keys is pressed, this is an event that main_ch will respond to by carrying out the list of actions on the right. Arrow key actions for main_ch are execute a piece of code, which adjusts the direction the character is heading (global.heading), its coordinate (x or y depending on the key that is pressed) and checking if nothing is in the way:

(if instance_position(x-10,y,all)=noone x-=4
else if global.space_mess=0 global.space_mess=1;

This translates as: if no object (noone) of any type (all) is at instance position (my x coordiante-10, my y coordinate) or 10 pixels to my left, then my new x coordinate is x-4 (x-=4) so I will move 4 pixels to the left. Otherwise (else), if global.space_mess=0, then change that global variable (global means it will be recognized in other scripts maunched by other objects) becomes =1.

global.space_message is a variable that will be used to determine if a message is shown on the screen later in the loop. This prepares for that message to be shown, if another object is on its way to the left.

The O key event starts a dialog box you can use to rename Hobo.

main_ch is drawn. If space_message=1, it shows a message informing the player that the space key will show what options he has at any given point in the game if he is facing something.

The sprite associated to the object is drawn, in conformity with the global.heading value.

Takes the x and y "local" values and copies them into global values that can be used anywhere (global.posx and y).

Runs the script character_options() if a variable value is equal to 1.

Space is released. This changes a variable that will be used in the next main_ch object's draw event. The variable changes if there is anything in one of the four directions, 10 pixels away. If not, it "sleeps" for 1/1000th of a second for each direction and then runs nothing_to_do(), another script that says there is nothing to do.

Now let's move on to another object.

   
         
    Part 2 Back to Tutorials Part 4    
 

Making Video Games Home     Game Programming Tutorials     Email Courses with Source Code    Make Games for a Living   Swords and Sorcery   Contact  Affiliates

© www.makingvideogames.com 2009

Other project:  Understand Fat Loss