Passing Objects from Script to C++ ?
by Brandon Pollet · in Torque Game Builder · 06/15/2005 (4:00 pm) · 5 replies
I have an object that is created in script and needs to be passed to C++ code when a collision occurs. Currently I have implemented a
console method that takes the object as a parameter and I am trying to cast the argument to an Object pointer like this...
As you can see from the commented code I have tried this with a cast and with the Sim::findObject function but both methods crash T2D.
Any ideas what I am doing wrong? How do you pass an object from script to c++?
Thanks.
console method that takes the object as a parameter and I am trying to cast the argument to an Object pointer like this...
ConsoleMethod(fxSceneGraph2D, setBlock, bool, 5, 5, "Set the block)
{
U32 xCoord = (U32)argv[3];
U32 yCoord = (U32)argv[4];
xCoord = xCoord % 5;
yCoord = yCoord % 5;
yCoord = object->mSizeOfBoardX * yCoord - 1;
U32 elem = yCoord + xCoord;
QuamBlock2D* nBlock = (QuamBlock2D*)argv[2];
//QuamBlock2D* nBlock = (QuamBlock2D*)(Sim::findObject(argv[2]));
return object->setBlock(nBlock, elem);
}As you can see from the commented code I have tried this with a cast and with the Sim::findObject function but both methods crash T2D.
Any ideas what I am doing wrong? How do you pass an object from script to c++?
Thanks.
About the author
Brandon earned a Master's of Science in Computer Science from the University of Tulsa in 2005 before they asked him to leave. Since then he has worked in web development and mobile development all while honing his game design/programming skills.
#2
And I believe this is what you are talking about for the marco... this comes from QuamBlock2D.h
I thought that a CONOBJECT was visible to both areas of the engine, but I'm really not that clear on the whole thing.
06/15/2005 (9:34 pm)
This is how the Object is created (in script) that is passed in argv[2]$player = new QuamBlock2D() { scenegraph = t2dSceneGraph; } ;And I believe this is what you are talking about for the marco... this comes from QuamBlock2D.h
class QuamBlock2D : public fxSceneObject2D
{
typedef fxSceneObject2D Parent;
QuamBlockDatablock2D* mConfigDataBlock;
...
public:
QuamBlock2D();
virtual ~QuamBlock2D();
....
DECLARE_CONOBJECT(QuamBlock2D);I thought that a CONOBJECT was visible to both areas of the engine, but I'm really not that clear on the whole thing.
#3
06/15/2005 (9:41 pm)
Also, I don't know if this matters or not but this is how I am calling the function from script...function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
if (%srcObj == $player)
{
//set the Block in the data structure
%position = %srcObj.getPosition();
%posX = getWord(%position, 0);
%posY = getWord(%position, 1);
t2dSceneGraph.setBlock(%srcObj, %posX, %posY);
}
...
}
#4
The answer here really depends on where the crash is happening. From what I can see, it may be in the mysterious "setBlock" C++ call. Assuming that the "QuamBlock2D" is valid, you need to make a few changes to your method. The first is that you're casting a string argument directly into an integer which is a bad idea as you'll get crazy coords from this conversion. You can use "dAtoi(argv[3])" for this to do the conversion from "ascii to integer" which is a T2D version of the standard "atoi()" call.
Also, as Stephen mentioned, your casting of the arg directly to a class pointer is wrong whereas the comment-out version is correct although I'd always recommend checking the resultant value before using it, maybe using a "dynamic_cast<>()" as in this example from T2D...
If this crashes then I'd check your code in the "setBlock()" call or look at the stack-trace.
Hope this helps,
- Melv.
06/16/2005 (3:18 am)
Brandon,The answer here really depends on where the crash is happening. From what I can see, it may be in the mysterious "setBlock" C++ call. Assuming that the "QuamBlock2D" is valid, you need to make a few changes to your method. The first is that you're casting a string argument directly into an integer which is a bad idea as you'll get crazy coords from this conversion. You can use "dAtoi(argv[3])" for this to do the conversion from "ascii to integer" which is a T2D version of the standard "atoi()" call.
Also, as Stephen mentioned, your casting of the arg directly to a class pointer is wrong whereas the comment-out version is correct although I'd always recommend checking the resultant value before using it, maybe using a "dynamic_cast<>()" as in this example from T2D...
//-----------------------------------------------------------------------------
// Add to Scene.
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, addToScene, void, 3, 3, "(t2dSceneGraph) - Add to SceneGraph.")
{
// Find t2dSceneGraph Object.
t2dSceneGraph* pT2DSceneGraph = dynamic_cast<t2dSceneGraph*>(Sim::findObject(argv[2]));
// Validate Object.
if ( !pT2DSceneGraph )
{
Con::warnf("t2dSceneObject::addToScene() - Couldn't find/Invalid object '%s'.", argv[2]);
return;
}
// Add to Scene.
pT2DSceneGraph->addToScene( object );
}Here's a version that should work, assuming the problem isn't in the "setBlock" C++ function call...ConsoleMethod(fxSceneGraph2D, setBlock, bool, 5, 5, "Set the block)
{
QuamBlock2D* nBlock = dynamic_cast<QuamBlock2D*>(Sim::findObject(argv[2]));
if ( !nBlock )
{
Con::warnf("fxSceneGraph2D::setBlock() - Couldn't find/Invalid object '%s'.", argv[2]);
return;
}
U32 xCoord = dAtoi(argv[3]);
U32 yCoord = dAtoi(argv[4]);
xCoord = xCoord % 5;
yCoord = yCoord % 5;
yCoord = object->mSizeOfBoardX * yCoord - 1;
U32 elem = yCoord + xCoord;
return object->setBlock(nBlock, elem);
}If this crashes then I'd check your code in the "setBlock()" call or look at the stack-trace.
Hope this helps,
- Melv.
#5
Also, you were right about this being a problem with the setBlock function. Apparently I am having trouble with an iterator.
Here is the code....
I'm sure this probably isn't the best way to insert something into a Vector but it was all I could figure out. Am I creating the iterator correctly? I am using the extended Vector class that is included in the engine, is there another way to insert an object into a Vector given an element location?
Thanks for your help guys I really appreaciate you both answering my questions.
06/16/2005 (12:30 pm)
Thanks for the info Melv, I have a Java background so I'm still picking up all the C++ syntax. Also, you were right about this being a problem with the setBlock function. Apparently I am having trouble with an iterator.
Here is the code....
bool fxSceneGraph2D::setBlock(QuamBlock2D* newBlock, U32 dsElement)
{
//Given the object newBlock and the Element loaction
//Get an Iterator for the Vector
Vector<QuamBlock2D*>::iterator p = FQGameBoard.begin();
//insert puts the object right before the iterator
for(int x = 0; x< dsElement+1; x++)
{
p++;
}
//insert this object into the Vector FQGameBoard
FQGameBoard.insert(p, newBlock);
return true;
}I'm sure this probably isn't the best way to insert something into a Vector but it was all I could figure out. Am I creating the iterator correctly? I am using the extended Vector class that is included in the engine, is there another way to insert an object into a Vector given an element location?
Thanks for your help guys I really appreaciate you both answering my questions.
Torque 3D Owner Stephen Zepp