Moving an object using script
by Infinitum3D · in Torque Game Engine · 07/02/2007 (3:08 pm) · 5 replies
Can the rotation or position or location of a shape be changed using script?
I have a simple cube that I'm using for a door. I want it to move onCollision. I've seen resources with calls to the server and source changes, but a simple script should work.
It seems very easy.
If
player collides with door
then
door position = new door position
Here's my door.cs with datablock and function
Everything works EXCEPT the position doesn't change. It compiles into a .dso, my echos show up in the console, but the door does NOT change postion. You can see where I've commented out many attempts. I've tried this using %location, rotation, position, etc. Everything I could think of.
Is it a simple syntax error, or are doors really that difficult to code. All I want in for a cube to change it's location on collision. I can get it to go away using %obj.delete(); so I know I can manipulate it.
Thanks,
Tony
I have a simple cube that I'm using for a door. I want it to move onCollision. I've seen resources with calls to the server and source changes, but a simple script should work.
It seems very easy.
If
player collides with door
then
door position = new door position
Here's my door.cs with datablock and function
Quote:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
//--create the datablock for a door
datablock StaticShapeData(DoorItem)
{
category = "Doors";
shapeFile = "~/data/shapes/doors/door.dts";
};
//--create the door in-game
new DoorItem()
{
dataBlock = "DoorItem";
position = "222.2 -727.3 108.1";
rotation = "0 0 1 130";
scale = ".2 .2 .17";
};
//--collision detection to open door
function DoorItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
echo("Player collides with door!");
//%obj.position = ("222.2 -727.3 110");
//%DoorItem.rotation = "0 0 1 40"
//%obj.location = ("222.2 -727.3 111")
//%obj.delete();
%DoorItem.position = "222.2 -727.3 111";
echo("Door raises!");
}
}
Everything works EXCEPT the position doesn't change. It compiles into a .dso, my echos show up in the console, but the door does NOT change postion. You can see where I've commented out many attempts. I've tried this using %location, rotation, position, etc. Everything I could think of.
Is it a simple syntax error, or are doors really that difficult to code. All I want in for a cube to change it's location on collision. I can get it to go away using %obj.delete(); so I know I can manipulate it.
Thanks,
Tony
#2
edit: I'm getting a console error
Tony
07/02/2007 (5:57 pm)
Thanks Peter! Can setPosition affect rotation also?Quote:
%DoorItem.setPosition("222.2 -727.3 111 0 0 1 180");
edit: I'm getting a console error
Quote:Unable to find object: ' ' attempting to call 'setPosition'
Tony
#3
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
//--This is my attempt to create a door using the simplest code possible.
//--This creates a door at a pre-determined (hard-coded) location,
//-- and rotates it open (into a hard-coded location) on collision.
//--There is no animation. One second the door is closed, the next it is open.
//--This is the very basics of a door script. It works but should be expanded upon.
//--create the datablock for a door
datablock StaticShapeData(DoorItem)
{
category = "Doors";
shapeFile = "~/data/shapes/doors/door.dts";
};
//--create the door in-game
new DoorItem()
{
dataBlock = "DoorItem";
//transform = "222.2 -727.3 108.1 0 0 1 130";
position = "222.2 -727.3 108.1";
rotation = "0 0 1 130";
scale = ".2 .2 .17";
};
//--collision detection to open door
function DoorItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
echo("Player collides with door!");
%obj.setTransform("222.2 -725 108.1 0 0 1 180");
echo("Door opens!");
}
}
//--TODO - modify the hard-coding so that a door can be placed using the World Editor.
//-- I think this can be done using the 'getTransform' and 'setTransform' features.
07/03/2007 (1:23 pm)
This works.//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
//--This is my attempt to create a door using the simplest code possible.
//--This creates a door at a pre-determined (hard-coded) location,
//-- and rotates it open (into a hard-coded location) on collision.
//--There is no animation. One second the door is closed, the next it is open.
//--This is the very basics of a door script. It works but should be expanded upon.
//--create the datablock for a door
datablock StaticShapeData(DoorItem)
{
category = "Doors";
shapeFile = "~/data/shapes/doors/door.dts";
};
//--create the door in-game
new DoorItem()
{
dataBlock = "DoorItem";
//transform = "222.2 -727.3 108.1 0 0 1 130";
position = "222.2 -727.3 108.1";
rotation = "0 0 1 130";
scale = ".2 .2 .17";
};
//--collision detection to open door
function DoorItem::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "Player")
{
%client = %col.client;
echo("Player collides with door!");
%obj.setTransform("222.2 -725 108.1 0 0 1 180");
echo("Door opens!");
}
}
//--TODO - modify the hard-coding so that a door can be placed using the World Editor.
//-- I think this can be done using the 'getTransform' and 'setTransform' features.
#4
07/03/2007 (1:49 pm)
Im sure theres a resource out there :)
#5
- Another thing... to get it to show up in the world editor I think you just need to use DoorItem::onAdd and do the new DoorItem() stuff in there... take a look at the fps.starter and I think there's a StaticShape::onAdd that will add anything you want by default (might be a file called staticshape.cs ?).
07/05/2007 (10:37 am)
- Also I think you can trigger an animation instead of moving the door. - Another thing... to get it to show up in the world editor I think you just need to use DoorItem::onAdd and do the new DoorItem() stuff in there... take a look at the fps.starter and I think there's a StaticShape::onAdd that will add anything you want by default (might be a file called staticshape.cs ?).
Torque 3D Owner Peter Simard
Default Studio Name
%DoorItem.setPosition("222.2 -727.3 111");