Dynamic Object Creation

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Dynamic Object Creation

Post by kamokow »

Hello there,
I've been using C++ for awhile now but just started with game development kind of recently. I recently ran into a problem when starting to work on a new game, I'm not sure if I'm missing something from basic C++ or I'm just looking at the problem wrong. Basically, I'm not sure how to dynamically create objects when I'm not sure of how many objects I will need (if that makes sense/if I'm using the right terminology).

With things like bullets that I have a set maximum for, I can use an array. For example: I can have a bullet class (lets call it 'bullet') and a player class that has an array like:

Code: Select all

class bullet;

class player{
    //other stuff for player class here
    private:
        bullet bullets[20];
};
But what I want to know is how I would go about doing the same as with bullets, except without having a limit on how many I could have. Using a small array of 20 when I might not fill the entire array isnt really all that detrimental, but when I might have a lot more of a particular object being used I wouldnt want to have a bunch of huge arrays for things that might only have 1 or 2 members used.

So, am I missing something in basic c++ that I should go back and learn? When I've tried Google for this I've come up with nothing helpful so I would assume that my problem is simply missing something that I should know from learning C++ or I'm just looking at this problem in the wrong way. I'm just hoping one of you could point me in the right direction, thanks in advance!
Image
Busy_V
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 28
Joined: Mon Jan 04, 2010 5:29 am
Programming Language of Choice: C/C++

Re: Dynamic Object Creation

Post by Busy_V »

I think this is what you're looking for.

Have fun with vector arrays! :mrgreen:
;)

P.S.: You may find this article useful: http://stackoverflow.com/questions/1447 ... of-c-array
RyanPridgeon wrote:If you wanna go there, you go for it man. Nobody is stopping you so just work hard and achieve your goals.
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Dynamic Object Creation

Post by adikid89 »

You can do something like this...

Code: Select all

class bullet;

class player{
    //other stuff for player class here
    private:
       vector<bullet*> bullets;
       AddBullet()
      {
          bullet* b = new bullet;
          bullets.push_back(b);
      }
      RemoveBullet()
      {
           //free memory
           delete bullets.back();
           //erase it from the vector
           bullets.pop_back()
      }
};
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Re: Dynamic Object Creation

Post by kamokow »

Oh, thanks you guys. I completely forgot about vectors =_=.
Image
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Dynamic Object Creation

Post by MrDeathNote »

Banned wrote:Content removed by admin.
Someone should delete all hotxxl007's posts and ban the account.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dynamic Object Creation

Post by dandymcgee »

MrDeathNote wrote: Someone should delete all hotxxl007's posts and ban the account.
Good idea, done.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply