Game Development Community

Need code help.....

by Johnathan Moore · in RTS Starter Kit · 04/02/2006 (1:52 pm) · 17 replies

Hey,
I have a bit of a problem
if I am right the obj variable in addSelection(U32 index, SimObject* obj, ColorF color) is of the type SimObjectPtr.

Could some one tell me why this doesnt work
void TerrainRender::addSelection(U32 index, SimObject* obj, ColorF color)
{

const SimObjectId curdata = (obj.getdatablock())->getId();

Error 1 error C2228: left of '.getId' must have class/struct/union c:\enginemake\engine\terrain\terrSelection.cc 175
Error 2 error C2228: left of '.getdatablock' must have class/struct/union c:\enginemake\engine\terrain\terrSelection.cc 175
Error 3 error C2227: left of '->getId' must point to class/struct/union/generic type c:\enginemake\engine\terrain\terrSelection.cc 175

#1
04/02/2006 (6:36 pm)
Obj is a pointer and needs the dereferencing operator

void TerrainRender::addSelection(U32 index, SimObject* obj, ColorF color)
{
const SimObjectId curdata = obj->getdatablock()->getId();
#2
04/02/2006 (7:54 pm)
You will also need to dynamic_cast it to the appropriate level of the hierarchy to get the appropriate datablock pointer in most circumstances.
#3
04/03/2006 (4:16 am)
Sorry I figured that error out a little after I posted, funny that happens alot. I am really new to coding in C++ and I dont know what casts are for so ill try to find out.
#4
04/09/2006 (8:45 am)
Hey I still havent figured this out after learning about casting it says 'mdatablock' : is not a member of 'SceneObject', is this not the right way to get its datablock
const SimObjectId curdata = obj->mdatablock->getId();
#5
04/09/2006 (8:59 am)
You need to upcast your SimObject reference to the class, call it MyClass, of the actual object that containing mdatablock. SimObject is the base class.

So it needs to look like this:

const SimObjectId curdata = ((MyClass *)obj)->mdatablock->getId();

or


void TerrainRender::addSelection(U32 index, MyClass* obj, ColorF color)
{
const SimObjectId curdata = obj->mdatablock->getId();
#6
04/09/2006 (9:11 am)
Hmm well at the moment I have a template because I dont know what object it is
heres the function
template <class T>
void TerrainRender::addSelection(U32 index, T* obj, ColorF color)
{

	const SimObjectId curdata = obj->mdatablock->getId();

RTSSquadData rtss;
const SimObjectId SqdDatId = rtss.getId();

RTSSquadData rtsu;
const SimObjectId UnitDatId = rtsu.getId();


switch(curdata)
{
case SqdDatId:
	RTSSquadData* objsquad = obj;
	for(S32 xbx;xbx<(obj->squadmembermax); ++xbx)
	{
		TerrainRender::unitSelectDraw(index, obj->member[50],color);

	}
	break;
case UnitDatId:

	break;
}

}
#7
04/09/2006 (9:28 am)
Then just use

const SimObjectId curdata = ((T *)obj)->mdatablock->getId();
#8
04/09/2006 (9:55 am)
Ok, this is what I have done
T object = dynamic_cast<T>(obj);

but it will not compile because it keeps treating the template T as
SceneObject class for some evil reason
#9
04/09/2006 (10:03 am)
Hmmm, I wouldn't have expect to any more than what I posted. What error message do you get when you just change the one line to (forgetting the other instance of obj, for the moment, it is them same case.)

const SimObjectId curdata = ((T *)obj)->mdatablock->getId();
#10
04/09/2006 (10:09 am)
Hmmm, I wouldn't have expect to any more than what I posted. What error message do you get when you just change the one line to (forgetting the other instance of obj, for the moment, it is them same case.)

const SimObjectId curdata = ((T *)obj)->mdatablock->getId();
#11
04/09/2006 (10:14 am)
void TerrainRender::addSelection(U32 index, T* obj, ColorF color)
{
// T object = dynamic_cast<T>(obj);
	const SimObjectId curdata = ((T *)obj)->mdatablock->getId();

RTSSquadData rtss;

'mdatablock' : is not a member of 'SceneObject' c:\enginemake\engine\terrain\terrSelection.cc
#12
04/09/2006 (10:18 am)
Then you are calling the function with a SceneObject, you need to properly up cast the object when you make the call so that the template mechanism duplicates the right type.
#13
04/09/2006 (10:20 am)
Btw, are you mis-spelling mdatablock? Is it mDataBlock?
#14
04/09/2006 (10:22 am)
Well it hasnt made any difference with the caps, I will try moving the cast before the function call
#15
04/09/2006 (10:29 am)
You mean like, right:

MyClass *obj = new MyClass();
...
addSelection(index, obj, color);
#16
04/10/2006 (7:20 am)
Ok I have it sorted apart from the line
const SimObjectId curdata = ((T *)obj)->mDataBlock->getId();

on both simobject and sceneobject it says mdatablock is not a member of simobject/sceneobject

T is a template and can either be an instance of simobject or sceneobject

at the moment I use the template but I could cast the sceneobject to simobject
#17
04/10/2006 (7:55 am)
MDataBlock is not a member of SimObject or SceneObject. YOU have to declare a derived class that contains an mDataBlock pointer, which is what I thought you were doing. That is I thought you were instantiating your template with some class of your own design, derived from one of those.

To verify this, I did a quick search of the engine .h files for mDataBlock.

It appears in the ShapeBase and StaticShape classes, which are base classes that you might be deriving from for example but not SimObject and SceneObject. Maybe that's what you meant to be using?