Entity Example (Loading Game Levels)

Started by kevin, May 18, 2011, 07:19:24 AM

Previous topic - Next topic

kevin

  Entity Example (Loading Game Levels)
 
   This example shows one method of building a entity based system that will allow you to externalize your game data from the program code and allow for easier development of multi level games.   Which is something that seems to be stumbling block for many new programmers.

   To tackle this problem, we'll be creating a file format to store the necessary information needed to play a level (or more) of your game, as well as a loader to import this content and spawn all the characters within the game.    Given this is an example, here i'm going to limit the number of characters and their properties so the method doesn't get lost under the implementation.   So we'll only store and load 3 fictitious character types, those being the PLAYER, DRAGON and a CAR.  With each character we're only storing the information to position and colour it.    

   I've selected to use a text format in this example, but you could use a binary format.  Both have their advantages and disadvantages.  In our example the level data looks like the following.


; THis is the players starting position in the  level
[Player]
   Pos = 100,200


[Dragon]
Colour =$00ff00
   Pos = 500,322


[Car]
   Pos = 200,100
   Colour = $0000ff


[Car]
   Pos = 320,100
   Colour = $ff0000


 
     
  The data is structured as follows.  Each character ID is in square bracket tags, each ID is followed by it's properties list on separate rows.  The declaration is completed with a blank line.   Not the most efficient format, but easy to understand for us humans. :)  

  So the loader first imports this text into a string array,  next it then steps down the text looking for the ID tags. When it finds one, it spawns that character type, then parses it parameters variables from the following lines.   It repeats this process until it reaches the end of the rows of text.      

  In this example it's spawning everything as some 'generic' character type, so after loading we end with with a linked list containing all the characters.  You could do it like this, but in the real world you'd more than likely have separate arrays / lists.. But you know it's an example.. not a library.    

  Have fun!



kevin

#1
Entity Loading Example #2

  This examples shows another way programmers can separate game data from  game code. The concept is here, is that instead of storing every little detail about our game levels inside our games source code, we more the  object creation out to a text tile.  The file could have all kinds of data stored within it, but here it's just got one section of data that represents the spawn points for the in game objects in this level.  

   To load the level, the function loads the text file into memory then  splits it to an array.   To decode it, we loop through the lines from  top to bottom, Looking for any section markers.  In this example the code only understands a mock up <OBJECTS> marker, but you could add as many as need be.   When the we find a marker, we swap modes from search mode, to decode section mode.  So each section will need a separate decode block of code work out what the rows of text mean.
 
   Here's a sample of what the object spawn points look like in the level file

 
<Objects>
add_platform(2150,240,150,150,0,45,310,1)
add_platform(2150,240,150,150,0,135,310,1)
add_platform(2150,240,150,150,0,225,310,1)
</Objects>`
 


   So the <OBJECTS> block decode routine, looks at these rows, splits the Object  name and the parameters apart.  We then trap the object name and check the   number of parameters etc.  If it's known object name with the right number of   parameters we call our 'in game'  object creation function.  So this function  would create this object type in the world world ready to rock.  
 
   Anyway, the goal here is not to plug this into your game, but rather  for you to apply such a concept to your own games.

   Have fun !   (READ THE CODE CAREFULLY & RUN THE EXAMPLE IN DEBUG MODE F7 )