Get overclass attributes

Started by
7 comments, last by Mr.L 11 years, 9 months ago
To describe my Problem I have to expand a little:

I wrote a game all of whose Objects(Timers,Inventory-Itemens, Walls, Trees...) are classes based on a subclass calleg gobject.
The classes which are direct in the world(Trees, Players, Walls) are also based on a class called wobject which is based on gobject.
always when an Object is created it gets automaticly added to a huge array of gobject pointers.
The trick here is, that that even the most unimportant object has a virtual handle function, which is called on a big loop, that calls every handle for each pointer in the array.
For example if the Player has an Item in his inventory, that automaticly heals him, I can write this in the virtual function of the class autopotion.
but the Problem is, for example in the collision detection, I have to acess Attributes like height, width and position.

How can I acess this attributes form the Pointer-Array?
Advertisement
Are attributes height, width, and position for collision in wobject class but not in gobject? I guess maybe gobject should have a virtual function to check for wobject derived class.

class gobject
{
virtual bool IsCollidable(void) {return false;}
}

class wobject : public gobject
{
virtual bool IsCollidable(void) {return true;}
}

If you know if gobject is collidable then you could cast gobject to wobject and get attributes as normal.
sorry but i don't get the pointer cast, can you show me how I workds with pointers?

sorry but i don't get the pointer cast, can you show me how I workds with pointers?


I'm assuming C++ here, dynamic casting works like this:

gobject* gobj = new wobject();
wobject* wobj = dynamic_cast<gobject*>(gobi);
if(wobj != nullptr)
{
// Cast was successful, do something
}


But beware, dynamic_casts are not very nice and might have some performance impact. Resorting to the use of dynamic_casts often smells of a design that is not optimal.

see http://www.cplusplus...al/typecasting/ for more details on casting and googling it will also give you some insight on the pros and cons of using it.
You are storing different types of object in the same array. Inevitably this causes problems.

Make the array for physical world objects only, then you are guaranteed to have an object with a position and size. Give any objects who can have inventories their own list of carryable objects. If you need to, as part of the parent objects's update method it can call update methods on the objects it is carrying.

If you need to, provide mechanism for converting a world object to an inventory object and back and utilise this mechanism in the pick-up/drop mechanic.

Deriving unrelated objects from a base class and then sticking them all in one list is effectively fighting against the advantages of using a strongly typed language. Liskov substitution principle applies here.
thank you both, but i think Aardvajk, is right, i'll have to make an extra array
no I ve got another Problem with the same Project:

I made a little map format to create a map with a text editor and an interpreter for it.

but haw can i dynamicly add new Objects to the level?
Don't be afraid of starting a new thread. It's better for everyone, including your future reference. Start it right away and maybe dump a link there.

Previously "Krohm"

sorry, a friend has just told me, so it has done itself.

This topic is closed to new replies.

Advertisement