creating new instances in c++. what is the best way?

Started by
5 comments, last by NogginBoink 18 years, 10 months ago
i have a bullet class in my game and i want to create a new var of it everytime the player shoots. in my previous game, i made an array of that class at the begining and just used those vars over and over again. but i don't think it's the best way because it limits the number of instances i can create and when i have a small amount it's not efficient. so, my question is, is there a better way of doing this? can someone show me an example or explanation?
Advertisement
It depends a little on the kind of game you are creating. The way you described is known as pooling -- creating a number of instances beforehand to save the memory allocation and deallocation. The precise size of the pool and its efficiency depend on the game. The number of bullets at any time is probably limited gameplay-wise (fire rate) so you should be able to derive the necessary pool size. If there are at any time very few bullets, then a pool is a bit too much and you could rely on the normal allocation procedure.

Greetz,

Illco
Alternatively, for a potentially limitless number of bullets, use a linked list and assign a TTL to each bullet or something so if they do escape the bounds of your gaming world they will eventually die and not take up resources. Say:

typdef struct _Bullet{_Bullet *prev, *next;  CBullet Bullet;int nIndex;int nTTL;} Bullet;typedef struct _BulletPool{Bullet *head, *tail;int nBulletNumber;} BulletPool


Then do the normal procedure for a linked list.

This will give you a pool of as many bullets as you want [smile]

hope that helps as an alternative
i'm not really familier with the list concept...
do you know any toturial about it?
nice article on Flipcode
Quote:Original post by MotionCoil
Alternatively, for a potentially limitless number of bullets, use a linked list and assign a TTL to each bullet or something so if they do escape the bounds of your gaming world they will eventually die and not take up resources. Say:

typdef struct _Bullet{_Bullet *prev, *next;  CBullet Bullet;int nIndex;int nTTL;} Bullet;typedef struct _BulletPool{Bullet *head, *tail;int nBulletNumber;} BulletPool


Then do the normal procedure for a linked list.

This will give you a pool of as many bullets as you want [smile]

hope that helps as an alternative



As he mentioned C++ then there is no need to reinvent the wheel and write another custom mediocre linked-list (unless he wants to learn how to write one thats a different story) the C++ standard library provides a list type which is implementated with doubly-linked list.

Also if you want to use a pooling allocation scheme i would recommend using boost library which has a package of pooled allocators, two of which are STL compliant.

With that in mind you would end-up with:

#include <list>#include <boost/pool/pool_alloc.hpp>//...typedef std::list<bullet, boost::fast_pool_allocator<bullet> > bullet_list;


[Edited by - snk_kid on June 11, 2005 10:02:45 AM]
I'm a newbie too, but wouldn't an STL list or vector be an appropriate tool in this case?

This topic is closed to new replies.

Advertisement