Hey Folks! I'm currently working on a RTS-Game (C&C-like) and i'm stuck at one point:
I got a class called "Vehicle" for vehicles like Tanks and for testing purposes I create two or three Vehicles just before my game-cycle in the main-method. But i have absolutely no idea how to create more Objects while the game is running. If I e.g. decide to create Buildings, which "produce" my tanks, how do I tell my program to create more objects of the type "Vehicle" during the game?
One idea of mine was to create a couple of Objects before the start and set them invisible, while there are not created. But when my game grows, there might be five or six different types of Vehicles or maybe more and i dont know if my solution makes much sense then.
thx for your help
RTS Game: Question about Creating Objects
-
- Posts: 1215
- Joined: Tue Jan 09, 2007 7:03 pm
- Location: Leuven, Belgium
Re: RTS Game: Question about Creating Objects
This is pretty general programming knowledge, you know
You would want your 'Building' objects to be able to create new instances of your vehicle class and store them away somewhere so they can be managed throughout your game
What kind of programming experience do you have?
You would want your 'Building' objects to be able to create new instances of your vehicle class and store them away somewhere so they can be managed throughout your game
What kind of programming experience do you have?
Re: RTS Game: Question about Creating Objects
I'm possibly thinking the question is more basic than you mean it. I apologize for that in advance.
There isn't really a difference in creating an object prior to the device loop or within it. You basically use the exact same methods you use in preparing for it (e.g., if it was Vehicle* mytanks[tankcounter++]= new Vehicle(tanktype) prior to entering the game loop, you would do that again with perhaps a different pointer). It would be normal to create and destroy objects in the course of the game, which means within the device loop. In fact, most of the time, you won't initialize until after you enter the game loop in a multi-level game. The initial level has no precedence over other levels.
Presumably you have some method of tracking how many tanks are left such that you know when to spawn additional tanks. Whatever the conditional statement is that you use to detect that, would be the place, or trigger, to spawn more via creating new instances of your vehicle class. Might mention that be sure you keep track of the ones needing destruction in that type of scenario. You will probably need to destroy them in the device loop too.
There isn't really a difference in creating an object prior to the device loop or within it. You basically use the exact same methods you use in preparing for it (e.g., if it was Vehicle* mytanks[tankcounter++]= new Vehicle(tanktype) prior to entering the game loop, you would do that again with perhaps a different pointer). It would be normal to create and destroy objects in the course of the game, which means within the device loop. In fact, most of the time, you won't initialize until after you enter the game loop in a multi-level game. The initial level has no precedence over other levels.
Presumably you have some method of tracking how many tanks are left such that you know when to spawn additional tanks. Whatever the conditional statement is that you use to detect that, would be the place, or trigger, to spawn more via creating new instances of your vehicle class. Might mention that be sure you keep track of the ones needing destruction in that type of scenario. You will probably need to destroy them in the device loop too.
-
- Posts: 758
- Joined: Mon Mar 31, 2008 3:32 pm
- Location: Bulgaria
Re: RTS Game: Question about Creating Objects
Umm... Can`t get exaclty what your problem is, but have you tried using arrays/lists/maps for storing those units` pointers?
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
-
- Posts: 3
- Joined: Mon Oct 10, 2011 6:16 pm
Re: RTS Game: Question about Creating Objects
first of all THX for your help!
well.. i'm no expert in programming but i got most of my experience in Java-coding (e.g. with the Slick-GameEngine) so my biggest lack should be handling pointers and stuff.
so what i did before the main loop was creating objects like this:
Tank firstVehicle = new Tank(..........);
Tank secondVehicle = new Tank(.......):
etc.
then create a list or a vector of vehicles and add all tanks to it.
maybe thats kinda noobish(?) ^^
if so, could you please tell me how to do it correctly in c++ ?
well.. i'm no expert in programming but i got most of my experience in Java-coding (e.g. with the Slick-GameEngine) so my biggest lack should be handling pointers and stuff.
so what i did before the main loop was creating objects like this:
Tank firstVehicle = new Tank(..........);
Tank secondVehicle = new Tank(.......):
etc.
then create a list or a vector of vehicles and add all tanks to it.
maybe thats kinda noobish(?) ^^
if so, could you please tell me how to do it correctly in c++ ?
Re: RTS Game: Question about Creating Objects
The recommendation to use arrays or lists might help you but that depends on your design. If you truly want to have a monolithic tank entity for each one you create (e.g., firstVehicle, secondVehicle etc), your way would work and you keep generating those discrete unique names. Normally that probably wouldn't be what you'd want for most types of games because it would become instantly unmanageable. Rather, you would have a somewhat generic manageable array of what tanks are currently spawned in the level. The pointer of type Tank, might be what some would put in that at the time you do a new on your class, and presumably, the Tank class should have variables/functions that can determine the status of that instance. At the time a tank is to be removed from your game loop (say with your ~Tank function), you might erase the array entry too. If you look at irrlicht's array object, you can see how to set up a construct like that to manage all the Tanks you'll be creating and destroying over the course of the game.
I'm old so I use a different way, I use a regular array typed to the class and null out entries that have been destroyed. It's a bad way and not growable easily, but it is easier to understand for a new programmer. To figure out how to do it the right way, study lists, Iterators, and the Irrlicht array. There may be a solution for you in that.
I'm old so I use a different way, I use a regular array typed to the class and null out entries that have been destroyed. It's a bad way and not growable easily, but it is easier to understand for a new programmer. To figure out how to do it the right way, study lists, Iterators, and the Irrlicht array. There may be a solution for you in that.