Jump to content

  • Log In with Google      Sign In   
  • Create Account


Get overclass attributes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Mr.L   Members   -  Reputation: 125

Like
0Likes
Like

Posted 17 July 2012 - 01:46 PM

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?

Sponsor:

#2 Neometron   Members   -  Reputation: 314

Like
0Likes
Like

Posted 18 July 2012 - 01:17 AM

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.

#3 Mr.L   Members   -  Reputation: 125

Like
0Likes
Like

Posted 18 July 2012 - 05:52 AM

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

#4 doeme   Members   -  Reputation: 693

Like
0Likes
Like

Posted 18 July 2012 - 06:11 AM

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.

#5 Aardvajk   Crossbones+   -  Reputation: 5866

Like
2Likes
Like

Posted 18 July 2012 - 06:50 AM

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.

#6 Mr.L   Members   -  Reputation: 125

Like
0Likes
Like

Posted 18 July 2012 - 11:27 AM

thank you both, but i think Aardvajk, is right, i'll have to make an extra array

#7 Mr.L   Members   -  Reputation: 125

Like
0Likes
Like

Posted 21 July 2012 - 02:56 AM

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?

#8 Krohm   Crossbones+   -  Reputation: 3020

Like
0Likes
Like

Posted 21 July 2012 - 03:20 AM

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.

#9 Mr.L   Members   -  Reputation: 125

Like
0Likes
Like

Posted 21 July 2012 - 01:17 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS