New script object?
by Tom Feni · in Torque Game Builder · 01/13/2006 (3:32 pm) · 7 replies
I have this called when I place a coin.. and I want it to save to a file and then load that file to replace the coins..
basically i want to create an editor to place coins and then save it to a level file that I can load up later..
right now its saving the file but not creating any usable data.. I know I did it wrong but I cannot find anything to show how to add a object as a script object..
any ideas on what I am doing wrong?
basically i want to create an editor to place coins and then save it to a level file that I can load up later..
right now its saving the file but not creating any usable data.. I know I did it wrong but I cannot find anything to show how to add a object as a script object..
any ideas on what I am doing wrong?
function Coin(%worldPos)
{
new ScriptObject(starCoin)
%starCoin = new t2dShape3D() { scenegraph = t2dScene; };
%starCoin.setShape("./shapes/starcoin.dts");
%starCoin.setCollisionActive(true, true);
%starCoin.setCollisionPhysics(true, true);
%starCoin.setCollisionCallback(true);
%starCoin.setLayer($playerLayer);
%starCoin.setGroup($starGroup);
%starCoin.setPosition(%worldPos);
// Orientate the shape in 3D.
%starCoin.setShapeRotation( "0 90 0" );
// Set Detail Level.
%starCoin.setDetailLevel( 0 );
%starCoin.setShapeScale( "0.1 1 0.1" );
%starCoin.setSize( 4 );
//%shape.setLayer($playerLayer);
//%shape.setGroup($playerGroup);
// Automatically rotate the shape in 3D.
%starCoin.setShapeAngularVelocity( "0 90 0" );
}
function starCoin::onAdd( %this )
{
new SimSet( gameData );
new SimSet( coins );
gameData.add(coins);
coins.add(starCoin);
gameData.save("berksquest/client/level.cs");
}About the author
#2
got everything loading and saving except for the collision...
here is whats going on
creating coins
saving coins
the keybind is in there.. :)
now loading coins
just wondering how to add the collision so it saves.. :)
and loads.. :)
tomfeni
01/14/2006 (11:39 pm)
Ok well almost there..got everything loading and saving except for the collision...
here is whats going on
creating coins
function sceneWindow2D::onMouseDown( %This, %mod, %worldPos, %clicks )
{
createCoin(%worldPos);
}
new SimSet( coinSimSet );
function createCoin(%worldPos)
{
%starCoin = new t2dShape3D(coins) { scenegraph = t2dScene; };
%starCoin.setShape("./shapes/starcoin.dts");
%starCoin.setCollisionActive(true, true);
%starCoin.setCollisionPhysics(true, true);
%starCoin.setCollisionCallback(true);
%starCoin.setLayer($playerLayer);
%starCoin.setGroup($starGroup);
%starCoin.setPosition(%worldPos);
//Orientate the shape in 3D.
%starCoin.setShapeRotation( "0 90 0" );
//Set Detail Level.
%starCoin.setDetailLevel( 0 );
%starCoin.setShapeScale( "0.1 1 0.1" );
%starCoin.setSize( 4 );
//Automatically rotate the shape in 3D.
%starCoin.setShapeAngularVelocity( "0 90 0" );
new scriptObject(Coins)
{
Coins = new t2dShape3D(Coins) { scenegraph = t2dScene; };
setShape = ("./shapes/starcoin.dts");
position = %worldPos;
setCollisionActive = (true, true);
setCollisionPhysics = (true, true);
setCollisionCallback = (true);
setLayer = $playerLayer;
setGroup = $starGroup;
//Orientate the shape in 3D.
setShapeRotation = "0 90 0";
//Set Detail Level.
setDetailLevel = 0 ;
setShapeScale = "0.1 1 0.1" ;
setSize = 4;
//Automatically rotate the shape in 3D.
setShapeAngularVelocity = "0 90 0" ;
};
coinSimSet.add(Coins);
}saving coins
function sKey(%val)
{
if (%val)
{
coinSimSet.save("BerksQuest/client/level.cs");
echo("set saved!");
}
}the keybind is in there.. :)
now loading coins
function loadData(%file, %setName)
{
%file = expandFileName(%file);
if(exec(%file))
{
%count = %setName.getCount();
for(%i=0;%i<%count;%i++)
{
%obj = %setName.getObject(%i);
%obj.setShape(%obj.setShape);
%obj.setPosition(%obj.position);
%obj.setShapeRotation(%obj.setShapeRotation);
%obj.setCollisionActive(%obj.setCollisionActive);
%obj.setCollisionPhysics(%obj.setCollisionPhysics);
%obj.setCollisionCallBack(%obj.setCollisionCallBack);
%obj.setLayer(%obj.setLayer);
%obj.setGroup(%obj.setGroup);
%obj.setDetailLevel(%obj.setDetailLevel);
%obj.setShapeScale(%obj.setShapeScale);
%obj.setSize(%obj.setSize);
%obj.setShapeAngularVelocity(%obj.setShapeAngularVelocity);
}
echo("set loaded!");
}
else
{
echo("invalid file name");
}
}
function t2dSceneObject::position(%this, %pos)
{
%this.position = %pos;
%this.setPosition(%pos);
}
new ActionMap(moveMap);
moveMap.bind(keyboard, s, "sKey");
moveMap.bind(keyboard, l, "lKey");
moveMap.push();
function lKey(%val)
{
if (%val)
{
%file = "BerksQuest/client/level.cs";
%setName = "coinSimSet";
loadData(%file, %setName);
echo("set loaded!");
}
}just wondering how to add the collision so it saves.. :)
and loads.. :)
tomfeni
#3
its not setting the collision right I would think.. :)
tomfeni
01/14/2006 (11:43 pm)
This is whats coming out..new t2dShape3D(coins) {
scenegraph = "t2dScene";
setGroup = "2";
setDetailLevel = "0";
setCollisionActive = "(true, true)";
setShapeAngularVelocity = "0 90 0";
setShape = "./shapes/starcoin.dts";
setCollisionPhysics = "(true, true)";
setLayer = "2";
setSize = "4";
setShapeScale = "0.1 1 0.1";
coins = "1703";
position = "-40.019073 30.509382";
setCollisionCallback = "(true)";
setShapeRotation = "0 90 0";
};its not setting the collision right I would think.. :)
tomfeni
#4
01/15/2006 (4:05 am)
I think you need to .setCollisionMasks when you load the coin :)
#5
so now we have a basic idea/tutorial on adding t2d3dshapes into a game, saving them and then loading them again..
so now if I can build a gui that allows yout o pick the shape and then place those.. kind of a pick and drop system.. I can build a level just by placing shapes and they will get added on mission load.. :)
hope this is helpful to others.. :)
01/15/2006 (12:46 pm)
That fixed it.. :)function loadData(%file, %setName)
{
%file = expandFileName(%file);
if(exec(%file))
{
%count = %setName.getCount();
for(%i=0;%i<%count;%i++)
{
%obj = %setName.getObject(%i);
%obj.setShape(%obj.setShape);
%obj.setPosition(%obj.position);
%obj.setShapeRotation(%obj.setShapeRotation);
%obj.setCollisionActive(true, true);
%obj.setCollisionPhysics(true, true);
%obj.setCollisionCallBack(true);
%obj.setLayer(%obj.setLayer);
%obj.setGroup(%obj.setGroup);
%obj.setDetailLevel(%obj.setDetailLevel);
%obj.setShapeScale(%obj.setShapeScale);
%obj.setSize(%obj.setSize);
%obj.setShapeAngularVelocity(%obj.setShapeAngularVelocity);
}
echo("set loaded!");
}
else
{
echo("invalid file name");
}
}so now we have a basic idea/tutorial on adding t2d3dshapes into a game, saving them and then loading them again..
so now if I can build a gui that allows yout o pick the shape and then place those.. kind of a pick and drop system.. I can build a level just by placing shapes and they will get added on mission load.. :)
hope this is helpful to others.. :)
#6
Thanks very much for sharing!
01/15/2006 (1:02 pm)
Ohhh yeah! Very useful indeed! I'll just have to see if I can get it plugged into my workflow (my programmer patner is busy these days) but it's exactly the sort of thing that wil make my project easier!Thanks very much for sharing!
#7
and it should be easy to just swap out the setShape with anything.. and when you save a set and then later save again it builds onto whats there..
so say you have 10 coins, then later place 10 more and save it adds to the total.. so it would have 20... and so on, which would be nice for say adding coins, swap out coins with some other shape and add those.. they will all save in level.cs..
later I will add level01, level02 and so on
so each level will have its own load file.. :)
tomfeni.. :)
01/15/2006 (2:14 pm)
No problem..and it should be easy to just swap out the setShape with anything.. and when you save a set and then later save again it builds onto whats there..
so say you have 10 coins, then later place 10 more and save it adds to the total.. so it would have 20... and so on, which would be nice for say adding coins, swap out coins with some other shape and add those.. they will all save in level.cs..
later I will add level01, level02 and so on
so each level will have its own load file.. :)
tomfeni.. :)
Torque 3D Owner Matthew Langley
Torque
function Coin(%worldPos) { %starCoin = new ScriptObject(starCoin); %starCoin.coin = new t2dShape3D() { scenegraph = t2dScene; }; %starCoin.coin.setShape("./shapes/starcoin.dts"); %starCoin.coin.setCollisionActive(true, true); %starCoin.coin.setCollisionPhysics(true, true); %starCoin.coin.coin.setCollisionCallback(true); %starCoin.coin.setLayer($playerLayer); %starCoin.coin.setGroup($starGroup); %starCoin.coin.setPosition(%worldPos); // Orientate the shape in 3D. %starCoin.coin.setShapeRotation( "0 90 0" ); // Set Detail Level. %starCoin.coin.setDetailLevel( 0 ); %starCoin.coin.setShapeScale( "0.1 1 0.1" ); %starCoin.coin.setSize( 4 ); //%shape.setLayer($playerLayer); //%shape.setGroup($playerGroup); // Automatically rotate the shape in 3D. %starCoin.coin.setShapeAngularVelocity( "0 90 0" ); }or like this
function Coin(%worldPos) { new ScriptObject(starCoin) starCoin.coin = new t2dShape3D() { scenegraph = t2dScene; }; starCoin.coin.setShape("./shapes/starcoin.dts"); starCoin.coin.setCollisionActive(true, true); starCoin.coin.setCollisionPhysics(true, true); starCoin.coin.setCollisionCallback(true); starCoin.coin.setLayer($playerLayer); starCoin.coin.setGroup($starGroup); starCoin.coin.setPosition(%worldPos); // Orientate the shape in 3D. starCoin.coin.setShapeRotation( "0 90 0" ); // Set Detail Level. starCoin.setDetailLevel( 0 ); starCoin.setShapeScale( "0.1 1 0.1" ); starCoin.coin.setSize( 4 ); //%shape.setLayer($playerLayer); //%shape.setGroup($playerGroup); // Automatically rotate the shape in 3D. starCoin.coin.setShapeAngularVelocity( "0 90 0" ); }You create a ScriptObject called startCoin but you reference a local variable called %starCoin...
you also can't set starCoint to a ScriptObject, then set it to another object, you need to set a property under StarCoin, like... starCoin.coin to the shape object.