Creating items from C++
by Steve Lamperti · in Torque Game Engine · 10/08/2003 (10:30 am) · 12 replies
I'm trying to create items from C++, for my project, and I have part of the solution, per a post from Jerry Gibson. This code will create an Item on the fly: (Given an itemType string.)
// JSL - per a post on the GG website from Jerry Gibson
joe = new Item;
theData = dynamic_cast(Sim::findObject((const char *) itemType));
joe->onNewDataBlock(theData);
joe->setPosition(Point3F(x, y, z));
if (joe->registerObject())
return((long) joe);
else
{
delete joe;
joe = NULL;
}
Now I'm trying to do the same thing for the other types of objects. The problem is the second line. Below is my attempt to create a staticShape:
sam = new StaticShape;
//samData = new StaticShapeData;
sam->onNewDataBlock(samData);
sam->setPosition(Point3F(x, y, z));
if (sam->registerObject())
return((long) sam);
else
{
delete sam;
sam = NULL;
}
The problem is that I have no idea how to translate the itemType string into a dataBlock for the staticShape.
FindObject, which is used above, isn't right.
If anyone has any suggestions I would appreciate hearing them.
Thanks,
// JSL - per a post on the GG website from Jerry Gibson
joe = new Item;
theData = dynamic_cast
joe->onNewDataBlock(theData);
joe->setPosition(Point3F(x, y, z));
if (joe->registerObject())
return((long) joe);
else
{
delete joe;
joe = NULL;
}
Now I'm trying to do the same thing for the other types of objects. The problem is the second line. Below is my attempt to create a staticShape:
sam = new StaticShape;
//samData = new StaticShapeData;
sam->onNewDataBlock(samData);
sam->setPosition(Point3F(x, y, z));
if (sam->registerObject())
return((long) sam);
else
{
delete sam;
sam = NULL;
}
The problem is that I have no idea how to translate the itemType string into a dataBlock for the staticShape.
FindObject, which is used above, isn't right.
If anyone has any suggestions I would appreciate hearing them.
Thanks,
#2
10/09/2003 (1:36 pm)
Let's have an example of the string you are using.
#3
10/09/2003 (2:02 pm)
The string I am using in the item case is just the name of the item. For example, the thing I have been testing with is "HealthKit". Which is working fine. I'm currently working with a project that is based on the FPS stuff, so there aren't too many kinds of objects in the project. An example of what I am trying to do with the StaticShape stuff would be the object "tree2" or "tree3" which are among the only objects of that type in the mod files.
#4
maybe you are gonna have to hook into the debugger and find out why.
a couple things to try.
dump the list of items in the game, datablocks
make sure the item of choice comes up
exec the findobject in the console with your params storing the return and check it...
findObject should find your object
10/09/2003 (3:22 pm)
Hmm well your findObject should work then bro.maybe you are gonna have to hook into the debugger and find out why.
a couple things to try.
dump the list of items in the game, datablocks
make sure the item of choice comes up
exec the findobject in the console with your params storing the return and check it...
findObject should find your object
#5
10/09/2003 (3:37 pm)
FindObject does work fine. The first part of the code that I listed above is working fine. The problem I am having is that there is no findObject defined for the StaticShape class, so I am not sure how to specify the datablock for the staticShape.
#6
10/09/2003 (9:17 pm)
Sim::findObject is templatized, so it will work for any derivative of SimObject (ie, StaticShape).
#7
theData = dynamic_cast(Sim::findObject((const char *) itemType));
But the very similar line
theData = dynamic_cast(Sim::findObject((const char *) itemType));
does not work when itemType is "tree2", and I have no idea why.
10/10/2003 (11:06 am)
The root of my question is that the following line of code works when itemType is "HealthKit:":theData = dynamic_cast
But the very similar line
theData = dynamic_cast
does not work when itemType is "tree2", and I have no idea why.
#8
Sorry if I am a bit of a novice at this stuff, but I am just starting.
HealthKit does show up in the hashtable in gNameDictionary, tree2 doesn't.
When I go into the editor I can place either kind of object, but when I look at the scripts, I found that there was a defined datablock called HealthKit, and there isn't one called tree2. I tried adding a datablock called tree2, but am still not having any luck.
10/10/2003 (4:10 pm)
I'm now trying again with findObject, and what I am finding is that the tree2 object is not included in the gNameDictionary.Sorry if I am a bit of a novice at this stuff, but I am just starting.
HealthKit does show up in the hashtable in gNameDictionary, tree2 doesn't.
When I go into the editor I can place either kind of object, but when I look at the scripts, I found that there was a defined datablock called HealthKit, and there isn't one called tree2. I tried adding a datablock called tree2, but am still not having any luck.
#9
try this:
This removes the need for a cast.
10/10/2003 (9:26 pm)
Steve,try this:
StaticShapeData *foo; Sim::findObject(itemType, foo);
This removes the need for a cast.
#10
Thanks for the suggestion, I do like that call better, as it makes the code a little simpler to read and a little clearer, however, it didn't solve my problem. It seems to have something to do with the hash table in the gNameDictionary object, as I mentioned above. It looks like the tree2 object is put into a different hash table then the one referenced by gNameDictionary. I will keep looking into this, and post the answer here if I figure something out, in case anyone else is having the same issue.
10/13/2003 (9:46 am)
@BenThanks for the suggestion, I do like that call better, as it makes the code a little simpler to read and a little clearer, however, it didn't solve my problem. It seems to have something to do with the hash table in the gNameDictionary object, as I mentioned above. It looks like the tree2 object is put into a different hash table then the one referenced by gNameDictionary. I will keep looking into this, and post the answer here if I figure something out, in case anyone else is having the same issue.
#11
Thanks for the help,
10/13/2003 (11:50 am)
Figured it out this morning. The problem was the datablock for the tree2 object. I had created a datablock for tree2, but I added it too early. (Still haven't spent too much time on the scripting.) So it was being cleared by the mission initialization. Once I added the datablock in a more correct place, my adding of a tree from c++ code is working well.Thanks for the help,
#12
10/13/2003 (11:42 pm)
No problem. Glad to hear you solved your problem!
Torque Owner Steve Lamperti
Imagine That!