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

Author Topic: Projectiles  (Read 2905 times)

0 Members and 2 Guests are viewing this topic.

wooshj

  • Newbie
  • *
  • Posts: 4
    • View Profile
Projectiles
« on: July 19, 2017, 06:17:54 am »
Hello,

So I'm having issues understanding something and I'm not sure if this is the correct place to ask this since technically its more game code related and not SFML related but we'll see.

If you want to shoot a projectile from a character using a class called lets say projectile, the way I saw another person do it was he would whenever space was pressed, create an instance of projectile then push it back onto a vector.

I have two questions about this; firstly shouldn't you use a dynamic instance of the projectile and then delete it either when the program ends by looping through the vector, or if it goes out of screen(certain cordinate?). Secondly I can't understand how creating the instance and pushing it onto the vector works? For example the code:

Code: [Select]
for("player presses space bar")
{
projectle1.rect.setPosition(Player1.rect.getPosition()); //rect is just the hitbox and image is following it
projectleArray.push_back(projectle1);
}

This is creating a using the same instance called projectile1 over and over again? How does that even make sense? does that mean when you delete projectle1 all the projectiles are lost?

Also again should you use a dynamic version of projectle1? Or can you not do that with instances.. Kinda new to this.

Thanks :)
« Last Edit: July 19, 2017, 06:23:07 am by wooshj »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Projectiles
« Reply #1 on: July 19, 2017, 06:36:03 am »
vector::push_back inserts a copy of the object, so everytime it is called it actually creates a new instance. Thus, the "projectile1" instance is never itself into the vector, it's just a "prototype" properly configured to duplicate for creating new projectiles.

When you don't need the projectile anymore, you just have to remove it from the vector to destroy the instance.
Laurent Gomila - SFML developer

wooshj

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Projectiles
« Reply #2 on: July 19, 2017, 01:09:09 pm »
Oh its that simple? lol, vectors are very powerful.

Martin Sand

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Projectiles
« Reply #3 on: July 20, 2017, 07:43:13 pm »
Quote
Oh its that simple? lol, vectors are very powerful.

Yes, and sometimes tricky. Be careful when iterating through vectors. When you projectile goes out of a certain range (coordinates) and you want to delete it from the vector, the iterator goes out of scope.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Projectiles
« Reply #4 on: August 04, 2017, 12:33:48 am »
Hi,

Doesn't C++ have List<> objects as C#? Or those are what you call "vectors"? Sorry I don't know much about C++

For deleting a projectile you can do this

for (a = 0; a < projectileList.Count; a++)
    if (projecitleList[a].IsOffSide())  // it is out of range
    {
        projectileList.RemoveAt(a);
        a--;
    }

Actually this should execute at every frame, same as the code that draws all active projectiles

I think that what Martin referred to is that you can't use foreach to iterate for deleting objects from a List (or "vector")

Hope this helps

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Projectiles
« Reply #5 on: August 04, 2017, 10:58:26 pm »
It seems, yes, that C#'s List is similar in concept to C++'s vector. That is, a form of dynamic array.
Quote
List<T> Class... Represents a strongly typed list of objects that can be accessed by index.

Note also that C++ has another type called list which is a 'linked list' that doesn't (necessarily) store elements contiguously.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything