• Forum
  • Lounge
  • How to create and load custom c++ "prefa

 
 
How to create and load custom c++ "prefabs"?

I'll try to explain my problem the best I can so please bare with me...

I have a gun in my game that holds a specific shooting pattern, basically just a spread. What I want to do is the ability to load that spread pattern with any type of bullet. For example if I have a fire bullet, an energy bullet, lazer bullet, etc.. And they all do different things, the fire bullet could explode and send off flying little bullets in a radius after a certain time has passed or whatever... I want to be able to load that spread pattern with any given bullet.

So what I tried to do is create a base bullet class and then have the other bullet types inherit from them. After that I don't really know how can I code my pattern to use the given bullet. What I'm trying to achive in code (pseudo-code) would look something like this:

SpreadPattern::Update( BulletType* referenceBullet, int number_of_bullets )

{

/* Within the pattern I would like to create a new bullet like the one passed into it as the
reference bullet. */

... Add bullet to a c++ vector...

... Update Bullets within vector...
}

Hopefully I was able to explain my problem in a clear manner. I'm really bad when it comes to explaining myself... Thanks for taking the time!..
Functional style:
1
2
3
4
5
6
7
8
9
10
typedef std::function<std::unique_ptr<Bullet>()> constructor_t;

void SpreadPattern::add_bullets(const constructor_t &c, size_t count){
    this->bullets.reserve(this->bullets.size() + count);
    while (count--)
        this->bullets.emplace_back(c());
}

pattern.add_bullets([](){ return std::make_unique<FireBullet>(); }, 100);
pattern.add_bullets([](){ return std::make_unique<LaserBullet>(); }, 100);

Template style:
1
2
3
4
5
6
7
8
9
template <typename T>
void SpreadPattern::add_bullets(size_t count){
    this->bullets.reserve(this->bullets.size() + count);
    while (count--)
        this->bullets.emplace_back(std::make_unique<T>());
}

pattern.add_bullets<FireBullet>(100);
pattern.add_bullets<LaserBullet>(100);

Polymorphic style (I think this one is less clean):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Bullet{
public:
    virtual std::unique_ptr<Bullet> clone() = 0;
    //...
};

void SpreadPattern::add_bullets(Bullet &reference, size_t count){
    this->bullets.reserve(this->bullets.size() + count);
    while (count--)
        this->bullets.emplace_back(reference.clone());
}

pattern.add_bullets(std::make_unique<FireBullet>(), 100);
pattern.add_bullets(std::make_unique<LaserBullet>(), 100);
Last edited on
Wow!.. Thanks a lot, this is very helpful, I appreciate it
Just a quick question... How could I make sure that I'm passing in a "bullet" data type if I use the template style?

I mean it works wonders the way you did it but I just want to try and make it a little bit safer for myself lol
Last edited on
If you pass as template parameter a type unrelated to Bullet, you'll get a compiler error on line 5 when you try to push the pointer into the vector, since T will not be a subclass of Bullet. The code is perfectly safe (you won't get run time errors like crashes and data corruption); at the worst you'll get incomprehensible compiler errors.

EDIT: Oh, one thing I forgot to mention. One advantage the functional style has over the others is that it's dead easy to adapt it to a "data-based" approach:
1
2
3
4
constructor_t constructors[] = {/*...*/};

while (input_file.more_data())
    bullets.emplace_back(constructors[input_file.read_int()]());
Last edited on
Oh ok! Yeah you're right I didn't even think of that
I'll take a good look at all the examples you gave me, thanks a lot!
Realistically a bullets MOA is going to be with in a tight grouping (a gun is more accurate than you are). You really just have to draw a circle and pick a random coordinate inside of it, overlay that on your camera and see what it intersects on the Z axis. If you're writing your own engine, learn from the masters: http://www.jagregory.com/abrash-black-book/
Realistically a bullets MOA is going to be with in a tight grouping (a gun is more accurate than you are).


remember too that moa is called that for a reason. The farther you go away from the muzzle, the bigger the spread, if its not a tuned weapon with carefully handmade ammo (which tightens the angles, it still does it but a lot less). So you need to project along angles so at a distance it behaves as you want, if you wanted realism. Most games just do ray trace, a few apply gravity, a very few apply wind. The few that do gravity and wind almost invariably do it wrong. Its depressing. One game (and its recent) that I tried the pattern repeats so you can memorize it and adjust for it. If you really want to go there, as the barrel heats up from rapid fire, the accuracy usually decreases from thermal expansion. Again, how real do you want it :P
Last edited on
Acceptable Breaks from Reality. The purpose of a video game is to be fun, not to be an accurate simulation of the real world. Also, I'm pretty sure OP is making some kind of shmup anyway.
Yea, it sounds fun. I want laser boolits :)
Topic archived. No new replies allowed.