Clunky torqueScript, need help streamlining
by Michigan State University (#0021 · in Torque Game Engine · 05/26/2006 (8:07 am) · 12 replies
So, using a variation on the Object Selection, I'm able to select an object in my world. Now I would like to be able to have some of these object perform actions when selected, like have a chair recline, a bed raise and lower, etc. i've found a way to do this, but it's very clunky and means I have to edit my torquescript every time a new class of object. Hopefully someone with a better understanding of TorqueScript can find a simpler way to do this.
Notes: 1.) correlation is a dynamic field I added in the OnCreate function. 2.) I'm hoping to get rid of correlation altogether and have something simple like %tagetObject.playActivation(), where it selects the correct version of playActivation() based on the namespace of the object selected, but I don't know engouh about namespaces to do this by myself.
....Object Selection Code....
echo("I have selected " @ %targetObject);
selectObjectCorrelation(%targetObject);
}
function getCorrelation(%this)
{
return %this.correlation;
}
function selectObjectCorrelation(%this)
{
%correl = getCorrelation(%this);
if (%correl $= "bed")
{
playBedActivation(%this);
}
else if (%correl $= "recliner")
{
playChairActivation(%this);
}
}Notes: 1.) correlation is a dynamic field I added in the OnCreate function. 2.) I'm hoping to get rid of correlation altogether and have something simple like %tagetObject.playActivation(), where it selects the correct version of playActivation() based on the namespace of the object selected, but I don't know engouh about namespaces to do this by myself.
#2
I'm still not great with namespaces, but here's one way to approach it. You can name your objects when you define them by including the name in parentheses:
%object = new Item(bedObject)
Then you can define methods within that object's namespace, like such:
bedObject::playActivation(%this)
Do that for each selectable object. Then, when an object is selected, you can just call %this.playActivation, and it will call the playActivation method for that specific object.
Someone please correct me if I'm wrong about that.
05/26/2006 (8:31 am)
I think you're on the right track with #2 there.I'm still not great with namespaces, but here's one way to approach it. You can name your objects when you define them by including the name in parentheses:
%object = new Item(bedObject)
Then you can define methods within that object's namespace, like such:
bedObject::playActivation(%this)
Do that for each selectable object. Then, when an object is selected, you can just call %this.playActivation, and it will call the playActivation method for that specific object.
Someone please correct me if I'm wrong about that.
#3
05/26/2006 (8:34 am)
You have the right solution, just didn't notice it. In your object definition just add a playAnimation method and then use %targetObject.playAnimation()
#4
I have tried matching a call from
%targetObject.playActivation()
to
function Bed::playActivation(%this)
(assuming i'm selecting the bed, of course), but it says it is an unknown command.
Is there something I have to set up in the datablock to be able to use the Bed Namespace?
I'm using
datablock ItemData( Bed )
{
category = "Hospitol";
shapeFile = "~/data/shapes/Animated_bed/animated_bed.dts";
};
As my datablock.
05/26/2006 (9:26 am)
Syntaxually, how do I arrange it?I have tried matching a call from
%targetObject.playActivation()
to
function Bed::playActivation(%this)
(assuming i'm selecting the bed, of course), but it says it is an unknown command.
Is there something I have to set up in the datablock to be able to use the Bed Namespace?
I'm using
datablock ItemData( Bed )
{
category = "Hospitol";
shapeFile = "~/data/shapes/Animated_bed/animated_bed.dts";
};
As my datablock.
#5
I would name the datablock BedData and the item Bed. Then when you define Bed::playActivation() it will be in the item's namespace.
When you make the call %targetObejct.playActivation() it's looking for the function within the item's namespace.
05/26/2006 (9:34 am)
You defined the Bed's datablock with the name "Bed", so the function Bed::playActivation is now defined within the datablock's namespace, not the item's.I would name the datablock BedData and the item Bed. Then when you define Bed::playActivation() it will be in the item's namespace.
When you make the call %targetObejct.playActivation() it's looking for the function within the item's namespace.
#6
05/26/2006 (9:36 am)
Or, if you wanted to keep the playActivation() method in the datablock's namespace, you could just do the following after you select an object:%data = %targetObject.getDatablock(); %data.playActivation();
#7
05/29/2006 (9:24 pm)
Sounds like someone is trying to get us to do his homework for him.... naughty naughty ;)
#8
when I look at the heirarchy of my object, it gives me:
Item --> Shapebase --> GameBase --> SceneObject ... etc.
So, if I name my function Item::playactivation(); everything runs smoothly.
However, that wouldn't be an acceptable solution, because every item being selected is an item, and will all use the same Item::playActivation() function.
So, the real question is how to set up my objects so that they know that they are in the namespace
Bed --> Item --> Shapebase ...etc.
I know the datablock is embedded deeper, but I need to call the item itself, and not the datablock it was created from.
(P.S. No homework, I've actually got a job working on Torque game design for the summer)
05/30/2006 (10:01 am)
Thanks for all the help so far, I'm getting close I think, but I'm still stuck. when I look at the heirarchy of my object, it gives me:
Item --> Shapebase --> GameBase --> SceneObject ... etc.
So, if I name my function Item::playactivation(); everything runs smoothly.
However, that wouldn't be an acceptable solution, because every item being selected is an item, and will all use the same Item::playActivation() function.
So, the real question is how to set up my objects so that they know that they are in the namespace
Bed --> Item --> Shapebase ...etc.
I know the datablock is embedded deeper, but I need to call the item itself, and not the datablock it was created from.
(P.S. No homework, I've actually got a job working on Torque game design for the summer)
#9
You can name your objects when you define them by including the name in parentheses:
Then you can define methods within that object's namespace, like such:
If you do the above, then your heirarchy will be: bedObject -> Item -> Shapebase ...etc.
05/30/2006 (10:10 am)
My earlier post pretty much covers that:You can name your objects when you define them by including the name in parentheses:
%object = new Item(bedObject)
Then you can define methods within that object's namespace, like such:
bedObject::playActivation(%this)
If you do the above, then your heirarchy will be: bedObject -> Item -> Shapebase ...etc.
#10
05/30/2006 (10:46 am)
*Edit: Apparently I spoke too soon. I realized that my new cs file never compiled, making me think it worked and it didn't. I've done exactly as written, (I'll provide the code below) and it still tells me that it is an item.//Create the Object
function ItemData::create( %data )
{
echo( "ItemData::create for Animated_bed called --------------------------" );
%obj = new Item(bedObject)
{
dataBlock = %data;
rotate = false;
static = false;
};
%obj.status = 0;
return %obj;
}
//Select the Object. This works, except the function call at the end. It can't find bedObject::playActivation();
function serverCmdFindObject(%client)
{
%StartPos=%client.player.getEyePoint();
%EyeVec = %client.player.getEyeVector();
%selectRange = 30;
%EyeVecScaled = VectorScale(%Eyevec, %selectRange);
%rangeEnd = VectorAdd(%StartPos, %EyeVecScaled);
echo("StarPos: " @ %StartPos @ " EyeVecScaled " @ %EyeVecScaled @ " RangeEnd: " @ %rangeEnd);
%searchMasks = $TypeMasks::ItemObjectType | $TypeMasks::ShapeBaseObjectType | $TypeMasks::StaticObject;
%scanTarg = ContainerRayCast(%StartPos, %rangeEnd, %searchMasks);
echo(" ScanTarg = " @ %scanTarg);
if (%scanTarg)
{
%targetObject = firstWord(%scanTarg);
}
echo("I have selected " @ %targetObject);
//selectObjectCorrelation(%targetObject);
%targetObject.playActivation();
}
//activate the object.
function bedObject::playActivation(%this)
{
//blah blah blah
}
#11
The common practice for this type of behaviour grouping is to implement your common functionality in a datablock namespace (such as bedObjectData for example), use the bedObjectData:: namespace for your script method implementation, and then call the functionality on the datablock's namespace:
-- when you call your ItemData::Create, do it with a %data of bedObjectData.
-- implement your ::playActivation() on the bedObjectData namespace (bedObjectData::playActivation() )
-- call it such: %targetObject.getDataBlock().playActivation().
Alternatively, you could copy the code from the c++ ::onAdd() method for either ScriptObject or Gui objects that link the namespace to the name itself, but keep in mind that when you name objects, the names need to be unique, so you would need to implement a different namespace for every bed in your game. If you are running a hotel, that could be 100+ namespaces, each with their own ::playActivation() method.
05/31/2006 (11:25 am)
Namespace links for Item objects do not by default include the object's "name" itself--unlike Gui and Script objects in TGE.The common practice for this type of behaviour grouping is to implement your common functionality in a datablock namespace (such as bedObjectData for example), use the bedObjectData:: namespace for your script method implementation, and then call the functionality on the datablock's namespace:
-- when you call your ItemData::Create, do it with a %data of bedObjectData.
-- implement your ::playActivation() on the bedObjectData namespace (bedObjectData::playActivation() )
-- call it such: %targetObject.getDataBlock().playActivation().
Alternatively, you could copy the code from the c++ ::onAdd() method for either ScriptObject or Gui objects that link the namespace to the name itself, but keep in mind that when you name objects, the names need to be unique, so you would need to implement a different namespace for every bed in your game. If you are running a hotel, that could be 100+ namespaces, each with their own ::playActivation() method.
#12
05/31/2006 (12:05 pm)
That's helpful, Stephen. It's interesting, I actually had realized that and changed my code around exactly that way, but then forgot about the Item namespace issue here.
Torque 3D Owner Tom Bampton
Note that you'd have to either rename chair to recliner or recliner to chair.