PDA

View Full Version : map/level object design


chot
11-03-2005, 11:28 AM
hi, im considering two choices for loading and storing a map/level into my game;

1. have all containers "global" in the game, like if you (theoreticly then) would load two levels at the same time, all their objects would be put in the same storage and nothing would work

2. have a "map" object that stores all the information of the map, so that (again theoritcly, one would never actually do this) your able to load two maps at the same time

now, its a bit more work to implement the second approach, but its more general, but since two levels is never ever going to be loaded simultaniously its quite useless.
on the other hand, the second approach would most certanly make the programming more general and easy to understand

i would like to hear your point of view of this problem, and is there any third approach that ive forgotten?

thanx in advance
/chot

sheijk
11-03-2005, 12:49 PM
The second one might be nice if you want to preload levels. Apart from that I don't really see where putting your data in a class would be more complicated than making it global variables. You will need a pointer to the level to be able to add objects etc. but as the project grows bigger this might even turn into an advantage because you can't access the game objects freely from everywhere which might lead to a cleaner structure

geon
11-05-2005, 12:30 PM
(...) which might lead to a cleaner structure

Yep.

Globals are usually considered a bad coding practice. The "its so much simplier" argument is not really valid.

I made a small 1 player puzzle game once, where I stored the game field as a global. Naturally, I found out later I wanted to have a 2 player game instead. It would have been trivial if I just had a class for the entire field.

Cybrid
11-07-2005, 09:01 AM
Yes, OOP gives a much cleaner and easy to maintain code that using globals that can be modifyed anywhere in the code and that can cause problems if it's modifyed when it shouldn't.

pater
11-08-2005, 03:45 AM
You could, to make it simple in the beginning, implement the map class as a singleton. Then you could create a global (or even more OOP-like, static) function getCurrentMap() which returns the currently active map (stored in a static reference). This is almost as simple as having the whole map global, but is very easy to expand if you later decided you'd require more than one map at a time.

SpreeTree
11-08-2005, 03:50 AM
Yep.
Globals are usually considered a bad coding practice. The "its so much simplier" argument is not really valid.


Globals have their uses, as does every other "Don't ever use it" feature of a programming language. I personally like globals for a variety of reasons, but hey, just my opinon.

Spree

geon
11-08-2005, 04:44 AM
Spree:

Yes, globals can be useful, but you should really know WHY you use them.

The laziest solution is mostly not simplier in the long run. To just think it is a simplier solution is naive. (Yes, I've been there, done that.)

chot
11-08-2005, 10:13 AM
k thanx, think i got an idea on how to do now