Game Engine Instances

Started by
15 comments, last by deadimp 18 years, 10 months ago
Quote:Original post by BoReDoM_Inc
But I a still using an array of each type of object (inherited ship) and then using the vector to store information about that object. Is this right or is there some better way that will leave me open to have as many instances of an object that I want?

class IObject{public:	virtual void DoSomething() = 0;};class CShip : public IObject{public:	void DoSomething() { printf("CShip::DoSomething()\n"); }};class CMissile : public IObject{public:	void DoSomething() { printf("CMissile::DoSomething()\n"); }};class CAsteroid : public IObject{public:	void DoSomething() { printf("CAsteroid::DoSomething()\n"); }};void main(){	list<IObject *> gameObjects; //Contains ships, missiles and asteroids	IObject * o = new CShip;	gameObjects.push_back(o);	o = new CAsteroid;	gameObjects.push_back(o);	o = new CMissile;	gameObjects.push_back(o);	for (list<IObject *>::iterator i = gameObjects.begin(); i != gameObjects.end(); ++i)		(*i)->DoSomething();}


Output:

CShip::DoSomething()
CAsteroid::DoSomething()
CMissile::DoSomething()

If you have a FrameUpdate() function for each type of object, they'll all update correctly without you having to care what type they are. All you know is you have an IObject pointer, and calling FrameUpdate() will do exactly what it's supposed to do. You don't have to worry about the actual type of the object except when constructing it.
Advertisement
std::vectors (derrived from std::lists?) allow dynamic memory allocation, therefore you have no limit (well, except for memory) on how many elements you have. [Was that what you were asking about?]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Sorry to bring this up again but I'm really confused about something.
I create my ships like this:
for (int i = 0; i < 50; i++)
{
Object = Instance_Create(new Ship, 1.0f, 1.0f);
}

I'm not actually sure if an Object array is neccessary they are also created fine if i remove the "Object =".

I also have some projectiles created each time a key is pressed:
for (int i = 0; i < 1024; i++)
{
if (Object == 0)
{
Object = Instance_Create(new Bullet, PlayerX, PlayerY);
break;
}
}

Then each time the projectile steps it checks for collision with any other object:
for(int i=0; i < CurrentMap->object.size(); i++)
{
if((X > CurrentMap->object->X - CurrentMap->object->Width - X) // Projectile > Obj L. - Projectile Radius
&& (X < CurrentMap->object->X + CurrentMap->object->Width + X) // Projectile < Obj R. + Projectile Radius
&& (Y > CurrentMap->object->Y - CurrentMap->object->Height - Y) // Projectile > Obj B. - Projectile Radius
&& (Y < CurrentMap->object->Y + CurrentMap->object->Height + Y)) // Projectile < Obj T. + Projectile Radius
{
if (CurrentMap->object->ID != ID) // So projectiles doesn't collide with themselves!
{
// Destroy Self
// Destroy Contact Object
break;
}
}
}

I want to know how to make it destroy itself, and the contact object.
Instance_Destroy(ID);
Instance_Destroy(i);

and

Instance_Unregister(ID);
Instance_Unregister(i);

do not work. The program exits and brings up and error.
This happens even when all my instance functions are exactly copied from above except for variable names!
I'm not sure why this. What am I doing wrong? I suspect it has something to do with the way I created my objects.
Any help would be appreciated.
Quote:Original post by BoReDoM_Inc
I want to know how to make it destroy itself, and the contact object.


just flag the objects as "dead"( use a member like bool m_IsAlive or something ).

you would then check for "dead" objects at the begining of each frame and remove them from the array/vector/list/whatever.

---novocaine thru yer veinz
I have something along the lines of what benishor stated in my example, the variable named "vector<bool> CRoom::object_exists", if that is the problem.
*Takes more time to read the post*

EDIT: That's odd... Unless you forgot to copy something, using "instance_destroy(id)" or "instance_unregister(id)" ought to work, for that is how I do it with my instances... Are your sure you are setting the ID of the instance when it is registered? (In "instance_register()")
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Yeah I'm definitely setting the object ID. It just won't unregister. I have made a simple console text file that I write to when functions are run. Every loop it tries to delete the projectile and contact over and over and it still stays registered in the vector.

void Projectile::Step() {	for(int i=0; i < CurrentMap->object.size(); i++)	{		if((X > CurrentMap->object->X - CurrentMap->object->Width - X) // Projectile > Obj L. - Projectile Radius		&& (X < CurrentMap->object->X + CurrentMap->object->Width + X) // Projectile < Obj R. + Projectile Radius		&& (Y > CurrentMap->object->Y - CurrentMap->object->Height - Y) // Projectile > Obj B. - Projectile Radius		&& (Y < CurrentMap->object->Y + CurrentMap->object->Height + Y)) // Projectile < Obj T. + Projectile Radius		{			if (CurrentMap->object->ID != ID) // So projectiles doesn't collide with themselves!			{				Instance_Destroy(ID); // It just say ID because it's inside the projectile move function... Is this the problem?				// Destroy Contact Object				break;			}		}	}}


Instead of calling Instance_Destroy(ID) do I have to replace ID with the original object? Cause that kind of sucks. If someone can could they give me and example of how they delete instances.

[Edited by - BoReDoM_Inc on June 16, 2005 2:29:08 AM]
Here's the code for the bullet in my MegaMan X movement example (using the same instance functions as I pasted above):
bool CXBSmall::Step() { long hspd=5; if (active) {  if (view_outside()) { instance_destroy(id); return true; }  hspeed=(lr==0) ? -hspd : hspd;  image_index=lr;  x+=hspeed;  if (place_meeting(Ground,x,y)) active=false; } if (!active) {  //Death graphics  if (image_index>7) { instance_destroy(id); return true; }  image_index++; }}
Can't see why it wouldn't work with your code... Can you paste the versions of your instance handling functions (register,unregister,create,destroy)?
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement