Jump to content
  • Advertisement
Sign in to follow this  
gordon13

Loading game objects from level file

This topic is 906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm new to game programming and C++ (I'm competent with Python and Javascript).

 

I'm using SDL2 to make a 2D game. I think I don't have the right words to search for what I want yet so I'll try and explain it here.

 

Currently the way I load the player image, background etc is all in the main function. The objects are defined there and pointers are used to store the images.

 

I guess that's acceptable for things that will be on screen all the time, but what about for objects like props or enemies that will only be in certain levels? Also what about building actual levels? How would you make that without hard coding everything?

 

I already have a way of reading level data (json parser) and setting the player's starting position and background image based on the player' progress (stored in another external file).

 

What I'd like to do is be able to define some objects and their xy positions as well as other metadata in the level file, and then be able to load the game level based on that instead of hard coded variables. Basically, what normal games do.

 

 

The following is what I think it would look like. But like I said I'm new to this so I'd appreciate some input.

 

On initialise: Parse the json file, get the type of each element and match that against the corresponding object class. Create an instance of that class along with all the initialisation data needed (texture, starting position etc) and store it in a level array (a vector ? or a list or something?).

 

In the game loop: For each iteration, user another loop to iterate through the level array and call the input handler (if it exists for that object), update and render functions for each element.

 

Does this seem like a good idea?

 

If so, any pointers (pun intended) on achieving this? If not, what would you suggest?

 

Are there any code examples of something like this out there?

 

Thanks

Share this post


Link to post
Share on other sites
Advertisement

This kind of thing is very game dependent.  There isn't a 'right' way for these kinds of things.  What you are suggesting should work fine.  Just start simple.  The less compicated you make this, the easier it will be.

 

I would make a data definition like this:

sprites: [
   sprite: {
      name: 'spr01',
      file: 'path/to/file.png',
      width: 256,
      height: 256,
      alpha: false,
   },
   sprite: {
      ...
   },
],

objs: [
   obj: {
      sprite: 'spr01',
      x: 0, y: 0, w: 32, h: 32,
      ...
   },
], 

You can start with hardcoding names of resources in the files and using those, or you could parse out the file name if it gets too complicated.  

Does that help?

Edited by Glass_Knife

Share this post


Link to post
Share on other sites

Seperate resources from game logic.

 

Your resource managment should only look after resources- Where images are located, sound, models,etc...

The mapping between the resource and the player's image, should be managed in a seperate class. (Or the player requests X image from the resource container).

Resource managment is a big topic for itself, I recommend researching it for yourself. 

 

The level's informatino file should contain ONLY game logic information, meaning:

- Map description (If it's tile based; tiles. If it's terrain based; locations).

- Spawning (player/enemies...)

- Other entities.

 

By using seperation, you have enough flexibility to add more entities to your levels.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!