Game Development Community

How to create the instance of "TSStatic" and "StaticShape" by coding C++?

by Tom Giant · in Torque 3D Professional · 01/26/2011 (4:06 am) · 11 replies

For convenience and performance, I want to dynamically create a lot of instances of "TSStatic" and "StaticShape" with C++ codes.
I'm going to dynamically generate a lot of model files(DAE), then create their instances by coding C++. But I don't know how to create object with C++ code.

Of course, I'm sure there isn't so simple as following codes.
Quote:
TSStatic* p1 = new TSStatic;
StaticShape* p2 = new StaticShape;

Does anybody know what? Thanks for any help in advance.

#1
01/26/2011 (2:20 pm)
TSStatic* p1 = new TSStatic();
p1->registerObject();
That's all.
The registerObject call is required so the new object instance got registered in "console". It will assign own unique object-id (p1->mId) so it can be accessed from scripts, setup namespace linkage and will call "onAdd" method.
That's really all what you need to do, so the "registerObject" is a key-method here.
#2
01/26/2011 (6:55 pm)
Great! Thanks Fyodor.

As far as I know, "Staticshape" is a little different from "TSStatic" when coding script. "Staticshape" needs a datablock of preferred statement.

For instance,
Quote:
datablock StaticShapeData(ArrowData)
{
category="Items";
shapeFile="art/shapes/mytest/arrow.dae";
};
%obj = new StaticShape()
{
datablock=ArrowData;
...
};
MissionCleanup.add(%obj);

What should I do with this problem in C++ codes?

Quote:
StaticShape* p2 = new StaticShape();
p2->registerObject();

That should be not all.

Maybe, simply append the code like the following,
Quote:
......
p2->setDatablock( SimDataBlock_Instance );

Does anybody know what? Thanks for any help in advance.
#3
01/27/2011 (1:09 am)
To transfer your scripted example to c++:
StaticShape* p1 = new StaticShape();
   StaticShapeData* pMyDatablock = static_cast<StaticShapeData*>(Sim::findObject("ArrowData"));
   p1->setDataBlock(pMyDatablock);
   p1->registerObject();
/// or another way of doing the same:
   StaticShape* p1 = new StaticShape();
   StaticShapeData* pMyDatablock = NULL;
   if(!Sim::findObject("ArrowData", pMyDatablock))
   {
      Con::errorf("Can't find datablock");
      return;
   }
   p1->setDataBlock(pMyDatablock);
   p1->registerObject();
or, if you already have one instance of object, you can "pickup" fields from there:
p1->assignFieldsFrom(pAnotherObject);
There are also methods p1->setTransform(MatrixF) and p1->setPosition(Point3F) to set object in required position in the world.
#4
01/27/2011 (4:59 am)
Got it. Thank Fyodor very much for your detailed replies. That is really helpful.

I noticed you used the function "Sim::findObject" to get an existent datablock. I'm going to dynamically generate model file(DAE), so if I also want to create "datablock" with C++ codes, what should I do?

I'm not sure whether like the codes below.
Quote:
SimDataBlock* p_db = new SimDataBlock()
p_db->setProperty(...);
......
p_db->registerObject();
#5
01/27/2011 (5:38 am)
looks okay for me, but don't forget that you need to use correct type of datablock.
If its StaticShade - use new StaticShapeData(), if ShapeBase - new ShapeBaseData(), etc.
#6
01/27/2011 (5:49 pm)
Got it. Thank Fyodor very much.
#7
01/27/2011 (11:11 pm)
TSStatic* p1 = new TSStatic();

p1->setField("shapeName","art/shapes/desertStructures/turret01gunball.dts");	
	p1->setPosition(Point3F( 0.0f, 0.0f, 0.0f ));
	p1->setScale(Point3F( 1.0f,1.0f,1.0f));
	
	if(!p1->registerObject("test")){
	    Con::errorf("Register failed");
	}
	else{
	    Con::printf(p1->getIdString());
	}

It get executed and I can see the id in console however i cant find it in world editor/object editor. Any idea?

Edit : Nvm, found the objects but it is not in scene tree thought. And it get executed twice, hmmm.
#8
01/28/2011 (1:56 am)
Quote:
It get executed and I can see the id in console however i cant find it in world editor/object editor.

This problem seems not to happen only from coding C++, I found also there was the problem when I created the instance of TSStatic and StaticShape with script codes( for example, "%obj = new TSStatic() {......}" ). So, maybe that is certain function limitation(just my guess).



Quote:
Edit : Nvm, found the objects but it is not in scene tree thought. And it get executed twice
"get executed twice" Forgive me for my English, I'm afraid I don't understand your meaning. Maybe you meant you had tried it twice.
#9
01/28/2011 (2:00 am)
You may need to add the newly created objects to the "MissionGroup" so it appears in the editor's tree.
#10
01/28/2011 (2:06 am)
Think it over again, should need to do more for that. "Editor" also seems to be implemented by scripts.
#11
01/28/2011 (2:10 am)
Aha! Thanks Fyodor.