# Mmm, objects...

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't do object oriented programming(yet). When I need multiple instances of something, I use an array, like EnemyData[50]. Which, of course, limits me to 50 enemies. It seems that using objects lets you spawn an unlimited number of instances of the object, and I just have to ask: is this true? And if so, how do you keep track of all that data? How do you access a specific enemy's Health Points? How do you know which enemy your bullet collided with? Objects seem to be the way to go for this kind of thing, but I don't understand how to use them.

##### Share on other sites
Quote:
 Original post by Possumdude0I don't do object oriented programming(yet). When I need multiple instances of something, I use an array, like EnemyData[50]. Which, of course, limits me to 50 enemies. It seems that using objects lets you spawn an unlimited number of instances of the object, and I just have to ask: is this true? And if so, how do you keep track of all that data? How do you access a specific enemy's Health Points? How do you know which enemy your bullet collided with? Objects seem to be the way to go for this kind of thing, but I don't understand how to use them.
Do you have a particular language in mind?

Meanwhile I'll just point out that your '50 enemies' example is only tangentially related to the use of objects. You could have an Enemy class, but still limit yourself to a maximum of 50 through the use of a non-resizable array.

Also, in your EnemyData array example, what is the type of the elements?

##### Share on other sites
I'm assuming that you are using C++?

I think that you've misunderstood what objects are. You will still need that array even if you use objects. Objects are instances of classes. Let's say you have a class called Enemy.

A VERY simple psuedo example.
class Enemy{public:    Enemy()     {        health = 0;        x = 0; y = 0; z = 0;    };    int health;    float x;    float y;    float z;}

Now you can create an instance of this class:

Enemy myEnemy; // On the stackmyEnemy.health = 100;myEnemy.x = 10;Enemy* pSomeEnemy = new Enemy();  // On the heappSomeEnemy->health = 100;pSomeEnemy->x = 43;delete pSomeEnemy;

If you need several instances you still need to store them in an array or other collection.
I suggest you take a look at the Standard Template Library for good collection classes. There you will find collection classes for dynamic arrays.

Quote:
 And if so, how do you keep track of all that data? How do you access a specific enemy's Health Points? How do you know which enemy your bullet collided with? Objects seem to be the way to go for this kind of thing, but I don't understand how to use them.

This all depends on your implementation. For example, let's say that you have an array of enemies. Now you want to check if the bullet, which has been fired, is colliding with any of the enemies.
Then for each frame you'll have to check EACH AND EVERY ONE of the enemies to see if they collide with the bullet. Which means that if a collision is found you know which enemy to edit to decrease it's health or whatever.

psuedo example:
for(int i = 0; i < numEnemies; ++i){    if ( collision(bullet, enemies) )    {        enemies.health--;        if ( enemies.health < 0 )        {            enemies.respawn();        }        bullet.destroy();    }}

There are many things which you have misunderstood, perhaps you need go back and read more on subject.

##### Share on other sites
There's a small difference between 'object-oriented programming' (OOP) and 'object-oriented systems' (OO). An OO system is built entirely from 'Objects', while OO programming involves using the concept of 'objects' to aid the programmer. The word 'object' itself is relative.

Quote:
 It seems that using objects lets you spawn an unlimited number of instances of the object, and I just have to ask: is this true?

Yes, but I get the impression that you are underestimating the power of OO.
You can easily have unlimited 'instances' without OO; but the beauty lies in 'why' you can have an unlimited number of instances with OO.

Quote:
 And if so, how do you keep track of all that data?

In Soviet Russia, the data tracks you. But seriously...in an OO system, the data tracks itself: RTTI, reference counting, smart pointers, etc.

Quote:
 How do you access a specific enemy's Health Points?

You 'find' that specific enemy (using a 'name', or a pointer, or even an ID).
This really has nothing to do specifically with OO; although the beauty of OO is that it allows you to do this stuff easily and without much thought.

Quote:
 How do you know which enemy your bullet collided with?

Again, this really doesn't have anything specifically to do with OO. This is a whole chapter in its own called 'Collision Detection' and 'Collision Response'. It's all basically math and physics. OO would allow you to easily facilitate this functionality however.

Quote:
 Objects seem to be the way to go for this kind of thing, but I don't understand how to use them.

You seem to be on the right track. Keep researching.
I'd say start at: http://www.geometrictools.com/ - awesome code and books.

##### Share on other sites
If you are using C++ yes you can spawn as many objects (enemy) as you want by doing a new on each one. But you still need a container to hold all of them so you can access them. I would suggest you look at the Standard Template Library (STL) to hep you out with that.

theTroll

##### Share on other sites
Wow, sorry I took so long to reply, I looked for the post but didn't see it, then got distracted only now thought of it again.

I see what you guys are saying, but if I still need an array, then how do games like Rome: Total War let you have an unlimited number of units?

Yes I'm using C++, sorry for not mentioning that.

What really sucks is that I'm about to be severed from my only access to the internet for Christmas break :( Today's the last day I can get on, so I won't see your replies or be able to ask more questions for that time.

##### Share on other sites
research C++ and vectors and gain and understanding of how pointers work. This should give you the two items you need at this time.