Game Development Community

Selection

by Mak Andrew · in RTS Starter Kit · 12/17/2004 (4:10 am) · 7 replies

When a unit is selected, TerrainRender::addSelection will be called. However i realise that a SceneObject *obj is called in as a parameter. that would mean that i can't get the unit's attribute and further process the attribute before i drawn the circle on the terrain

#1
12/20/2004 (2:01 pm)
Sure you can. Use dynamic_cast<>.
#2
12/20/2004 (4:47 pm)
Ok, so you are saying that RTSUnit inherited from SceneObject, (sorry still don't quite understand the torque engine)
#3
12/20/2004 (8:07 pm)
Yes, exactly.
#4
12/20/2004 (8:28 pm)
Here are a couple of lines from RTSUnit::processTick() in RTSUnit.cc that demonstrate how you can cast back and forth between inheritence trees for a known pointer. Do be careful, as by it's very nature casts are "forcing" the executable to do things with pointers outside of the "expected", and therefore easy to cause problems.

// Check if goal is to attack a target
      ShapeBase* target = dynamic_cast<ShapeBase*>(&(*mAimObject));
      if(mAimObjectSet  && (!target || target->getDamageState() == ShapeBase::Enabled))
      {
         // Check that our aim object is or is derived from RTSUnit
         if(RTSUnit *target = dynamic_cast<RTSUnit*>((GameBase*)mAimObject))

Note here that you are using a pointer to mAimObject (*mAimObject) in two different ways, and getting data that is appropriate from different classes that the pointer can be (accurately) cast to.

First, we set a ShapeBase *target to a cast of the mAimObject as a ShapeBase, and then 4 lines later, we are casting mAimObject to be an RTSUnit.

It's a little bit more complex than I summarized, but it illustrates the technique.
#5
12/20/2004 (8:32 pm)
Yes, you are right Zepp, that's why i'm trying to search for a better solution than dynamic cast on the SceneObject if possible, ... but maybe i should go ahead with the casting, thank guys you all are great
#6
12/21/2004 (3:27 am)
Well, while it's an advanced technique, there isn't anything wrong with it as long as you know what you are doing.

One of my content designers has a really interesting signature, that applies directly to this post:

Quote:
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg" - Bjarne Stroustrup

In other words, casting is a perfectly valid (and valuable) technique, but know why, what, and how you are doing what you are doing, or it will come back to byte you!
#7
12/21/2004 (3:19 pm)
You are certainly right Zepp, but just that it's kind of a habit for me not to use dynamic cast cause from times to times unexpected result occur and it's hard to monitor the problem(of course only if you use it the wrong way... )