Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Projectile/Bullet help  (Read 3052 times)

0 Members and 1 Guest are viewing this topic.

briant

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Projectile/Bullet help
« on: December 19, 2015, 02:54:12 am »
Was just a dumb mistake, but I'm still curious what you think the most optimal method for coding this is.

2D shooter, bullets are always firing, are not needed once off the screen.

Hey, so I'm making a 2D shooter and everything's been good until I got to projectiles.

What I tried was making sf::Sprite array (i.e. sf::Sprite bullet[20]) and then recycling through them (once it goes off screen, stop rendering it).

It was fine until this line:

bullet[value].move[0,1];

Error   C3867   'sf::Transformable::move': non-standard syntax; use '&' to create a pointer to member   

Error   C2109   subscript requires array or pointer type   


Now, I can work around this by simply incrementing and setting its position, but it just seems strange that sf::Sprite.move won't work in this case.

Also, I highly doubt this is the optimal way to handle this. How would you go about coding bullets?

Thanks
« Last Edit: December 19, 2015, 03:43:58 am by briant »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Projectile/Bullet help
« Reply #1 on: December 19, 2015, 03:26:32 am »

bullet[value].move[0,1];


i think that you should use parenthesis () instead of brackets [].

And use

 [code=cpp][ */code]
(and omit the asterisk)

for posting c++ code.
« Last Edit: December 19, 2015, 03:28:55 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

briant

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Projectile/Bullet help
« Reply #2 on: December 19, 2015, 03:40:29 am »
whoops.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Projectile/Bullet help
« Reply #3 on: December 19, 2015, 04:22:26 am »
the most optimal method for coding this is.
2D shooter, bullets are always firing, are not needed once off the screen.
You might want to have a look at std::array so you can use that instead of that normal array  ;)
A fixed-sized array, as you have used - also with std::array, can fall apart when you need "just one more" bullet. What happens when you need 21 bullets? Does one of the earlier bullets get killed even if it's still travelling and visible?
A dynamically-sized array are of use when storing dynamic objects such as bullets. The first port of call for this type of storage is usually an std::vector. This could do your job fine but there are other storage containers that may work better, such as std::list (if the bullets could expire at unordered times or std::queue (if the bullets expire in order).

 [code=cpp][ */code]
(and omit the asterisk)
[code=cpp][/code]

(you can use the [nobbc][/nobbc] tags to display special characters in posts - but not inside code tags)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

briant

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Projectile/Bullet help
« Reply #4 on: December 19, 2015, 04:35:46 am »
the most optimal method for coding this is.
2D shooter, bullets are always firing, are not needed once off the screen.
You might want to have a look at std::array so you can use that instead of that normal array  ;)
A fixed-sized array, as you have used - also with std::array, can fall apart when you need "just one more" bullet. What happens when you need 21 bullets? Does one of the earlier bullets get killed even if it's still travelling and visible?
A dynamically-sized array are of use when storing dynamic objects such as bullets. The first port of call for this type of storage is usually an std::vector. This could do your job fine but there are other storage containers that may work better, such as std::list (if the bullets could expire at unordered times or std::queue (if the bullets expire in order).

 [code=cpp][ */code]
(and omit the asterisk)
[code=cpp][/code]

(you can use the [nobbc][/nobbc] tags to display special characters in posts - but not inside code tags)

Neato thanks. I thought about using a dynamic array, but the way it's set up would never allow for that many to spawn, so I'll probably keep it static for simplicity's sake until later.

Also, I believe with how it's set up now it would just not spawn a bullet, so effectively your fire rate would just be slowed by a fixed amount.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Projectile/Bullet help
« Reply #5 on: December 19, 2015, 04:50:41 pm »
In general, you should resort to std::vector unless you have good reasons to use other containers like std::list or std::deque. Simply because it is the most efficient general-purpose container, and even operations like "remove in the middle" don't pay off in alternatives except for really large container sizes -- even more so when the swap-and-pop_back idiom is used.

For me, the main reason to use std::list was almost always element stability (pointers remain valid), not speed of certain operations.

I thought about using a dynamic array, but the way it's set up would never allow for that many to spawn, so I'll probably keep it static for simplicity's sake until later.
That doesn't make sense. Code is not simpler with a static array -- it's more complicated because you have to track the number of actually used elements manually and introduce case differentiations. Just use std::vector, you'll see that things will become more robust and easier.

And don't ever use arrays again -- if you actually need statically sized containers, use std::array, which has only advantages over plain C arrays.

Please avoid full quotes, it makes threads unreadable.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything