Seemed easy, is really hard, How do you program bullets?

Started by
14 comments, last by Satharis 9 years ago


Should I really be allocating and deallocating memory like that?

Most systems like this, like particle systems and other rapidly created/destroyed objects, live in a memory pool.

An appropriate size pool of objects is allocated and initialized, and then marked as not being active. When you need one it is marked as active, when you are done with it the item is marked as inactive.

The details of an appropriate size depends on your needs. I've seen high-performance particle systems that have space for several hundred thousand particles.

Do you have any general info on the object pool technique? I feel like I should know this.

Advertisement


Do you have any general info on the object pool technique? I feel like I should know this.

Scroll back up, read Nypyren's messages more carefully. Follow that link. If you need more information, use that for search terms.

Heres a simple javascript example of an object pool in case you still need help:


function Bullet(alive) {
    this.alive = alive;
    // ... all your other stuff here
}

// maxSize is optional and is only there to 
// protect you from allocating to many objects
function Pool (maxSize) {
    this.objects = [];
    this.maxSize = maxSize;
}

Pool.prototype.get = function() {
    // first check for any objects that aren't being used
    for(var i = 0; i < this.objects.length; i++) {
        if(this.objects[i].alive === false) {
           objects[i].alive = true;
           return objects[i];
        }
    }

    // only create a new object if the pool allows for it
    if(this.objects.length < this.maxSize) {
        this.objects.push(new Bullet(true));
        return this.objects[this.objects.length - 1];
    }

    // this means there are no more bullets available, either set 
    // maxSize to a higher value or stop requesting so many bullets!
    return null;
}

It's works, I created an array of pointers to handle turning the bullet on/off individually. Then I directly modified the bullet class's variables from my button input class. All I need to do is create conditions to turn the bullets off and my prototype is done.

Excellent...


Do you have any general info on the object pool technique? I feel like I should know this.

This is a good pattern of design to use to help in memory management. Game Programming Patterns by Robert Nystrom goes into several really good patterns that you might also find helpful.

Do you have any general info on the object pool technique? I feel like I should know this.


This is a good pattern of design to use to help in memory management. Game Programming Patterns by Robert Nystrom goes into several really good patterns that you might also find helpful.

In general you'll find the word "cache" or "pool" used a lot in game dev, almost of buzzword quality simply to refer to the idea of a design that favors keeping objects in memory vs destroying and recreating them on demand.

You technically could create and destroy each bullet as needed, and it would work, the only real negative to that is that things like bullets usually are an obvious candidate for pooling.

This topic is closed to new replies.

Advertisement