Sign in to follow this  
urisoft

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this