Page 1 of 1

handling multiple objects from the same class

PostPosted: 10 Aug 2011, 10:27
by fra9000
Hi FreeGameDev Team,

I'm new to C++ programming and I'd like to ask you how you usually manipulate objects from the same class..
to better explain myself..here it is an example:


[header]

{l Code}: {l Select All Code}
class Missile  {

private:

int initial_x_position;
int x_position;

public:

Missile(int init_x);
Move();

...
...};


Missile::Missile(int init_x) {

initial_x_position = init_x;

}


Move::Missile() {
   
   x_position++;

}


[main]

{l Code}: {l Select All Code}
...

Missile Miss1(10);
Missile Miss2(20);
Missile Miss3(30);

[b][end][/b]

so how could I move them together without calling them 1 by 1? like..

Miss1.move();
Miss2.move();
Miss3.move();



thank you in advance..

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 10:54
by Knitter
The most obvious (simple) way would be to put them in some data structure (array, list, map) and iterate that structure in a loop calling the method you want.

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 11:02
by charlie
Try to use the tags like [code] - I added it in for you.

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 12:32
by fra9000
@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


@ charlie: oh ty ;)

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 18:43
by Pix3l
fra9000 {l Wrote}:@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


@ charlie: oh ty ;)


Hi pal, what you need is explained here: Java 2D game programming tutorial
Here he makes a simple space invaders clone, that involve the handling of multiple objects.
It use a list, but you can use an Array if you like :]

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 20:38
by rogerdv
STL is your friend. I extensively use it in my game project.

Re: handling multiple objects from the same class

PostPosted: 10 Aug 2011, 21:55
by Knitter
@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


I'm not really a C++ user but sure I can post something, but if you're having problems with this I really feel you need to start with more basic stuff like arrays, loops and such.

{l Code}: {l Select All Code}
Missile missiles[] = { Missile(10), Missile(20), Missile(30) };
//or in two steps, you can also add the elements later on
//Missile missiles[3];
//missiles[0] = Missile(10);
//missiles[1] = Missile(20);
//missiles[2] = Missile(30);

for(int i = 0; i < 3; i++) {
    missiles[i].move();
}


That is an example using arrays. In your code you shouldn't be using arrays, or at least not these type (fixed) of arrays and should be using other data structures like lists or maps or something else. Again, C++ is not my usual language so I may have missed the syntax there, also I would follow on the STL as rogerdv mentioned.

Re: handling multiple objects from the same class

PostPosted: 11 Aug 2011, 13:33
by fra9000
Thank you guys for the quick and complete replies..it was exactly what I needed ;)

Re: handling multiple objects from the same class

PostPosted: 14 Aug 2011, 08:20
by fra9000
here I am..again :)

The method described above has worked for some basic iteration..but not in the following case:

target.h

{l Code}: {l Select All Code}
#include <string>


class Target {
   
   
   private:
   
   
   sf::Sprite sprite;
   
   float pos_x;
   float pos_y;
   
   
   public:

   
   Target(float x, float y, std::string n);
   
   void move_sprite(float t);
   sf::Sprite get_sprite();
   
};


Target::Target(float x, float y, std::string n) {
   
   
   static sf::Image img;
   if (!img.LoadFromFile(n)) {};
   
   sprite.SetImage(img);
        sprite.SetPosition(x, y);
   
   
}


void Target::move_sprite(float t) {
   
   
   sprite.Move(150.f*t, 0);


}


sf::Sprite Target::get_sprite() {
   
   
   return sprite;
   
   
}


// create targets
Target Target_1(   0.f, 240.f, "target.png");
Target Target_2( -60.f, 240.f, "target.png");
Target Target_3(-120.f, 240.f, "target.png");
Target Target_4(-180.f, 240.f, "target.png");
Target Target_5(-240.f, 240.f, "target.png");
Target Target_6(-300.f, 240.f, "target.png");
Target Target_7(-360.f, 240.f, "target.png");
Target Target_8(-420.f, 240.f, "target.png");


Target targets[8] = { Target_1, Target_2, Target_3, Target_4, Target_5, Target_6, Target_7, Target_8 };




main.cpp

{l Code}: {l Select All Code}

ElapsedTime = App.GetFrameTime();
   
// targets movement system [WIP]   
for(int i = 0; i < 9; i++) { targets[i].move_sprite(ElapsedTime); }




giving me a segmentation fault due to a stack overflow (I think)..


furthermore using:

{l Code}: {l Select All Code}
   Target_1.move_sprite(ElapsedTime);
   Target_2.move_sprite(ElapsedTime);
   Target_3.move_sprite(ElapsedTime);
   Target_4.move_sprite(ElapsedTime);
   Target_5.move_sprite(ElapsedTime);
   Target_6.move_sprite(ElapsedTime);
   Target_7.move_sprite(ElapsedTime);
   Target_8.move_sprite(ElapsedTime);


works perfectly..

I don't find a way to fix the problem..any advice? :(

Re: handling multiple objects from the same class

PostPosted: 14 Aug 2011, 08:56
by oln
Try:
main.cpp

{l Code}: {l Select All Code}

ElapsedTime = App.GetFrameTime();
   
// targets movement system [WIP]   
for(int i = 0; i < 7; i++) { targets[i].move_sprite(ElapsedTime); }


The array has a size of 8, and you were trying to access 10 elements.
Also, consider using std::vector rather than a basic array.

Re: handling multiple objects from the same class

PostPosted: 14 Aug 2011, 13:41
by Knitter
Not 7 nor 9, 8 :)

Arrays start at index 0, if you have na array with size 2, it will have position 0 and position 1. Since you created your array with size 8, it will have positions ranging from 0 to 7, thus your loop condition will have to be i < 8 or i <= 7.

When a C or C++ seg faults while using an array it's almost always caused by trying to access an invalid position.

Re: handling multiple objects from the same class

PostPosted: 15 Aug 2011, 12:09
by fra9000
oh..what a stupid error..ty again guys