Game Development Community

setTransform()

by Shon T · in Torque 3D Professional · 05/15/2012 (7:17 pm) · 8 replies

I'm new to Torque Script and I was hoping setTransform() could be explained to me a bit.
I'm currently attempting to change which direction the player is facing depending on what directional key was pressed. i.e if the up arrow key is pressed then the player faces north.

I've tried this small code snippit:

if($leftPressed)
{
Player::setTransform(0 0 0); //No numbers have been entered
}

and I get this error when I press the left arrow key:

scripts/client/default.bind.cs (67): ::setTransform - wrong number of arguments (got 2, expected min 3 and max 3).
scripts/client/default.bind.cs (67): usage: Set the object's transform (orientation and position).@param txfm object transform to set

Could I get some help and clarification?

About the author

Just a guy with hobbies.


#1
05/15/2012 (7:37 pm)
Couple of things:

1). The typical syntax for calling setTransform() (or any method for that matter) is: <someObjectId>.<methodName>(params...);

In this case you need to know the name/id of the Player object you want to call setTransform() on. As an example, let's say you had a Player object named 'Player1', you'd use setTransform() like: Player1.setTransform(0,0,0);

2). setTransform() actually takes 7 parameters. The first three define the position (x, y, z) of the object. The last four define the rotation for the object in an axis-angle format (xAxis, yAxis, zAxis, rotation). This old post goes into more detail: http://www.garagegames.com/community/forums/viewthread/65545/1#comment-477675
#2
05/15/2012 (9:01 pm)
So what would be the correct way in finding the player object? I'm using the China Town demo to test out ideas and this one has me stumped yet. Following your directions I was able to eliminate the second error but I still am having trouble finding the object
#3
05/15/2012 (10:20 pm)
You have a few options depending on your project.

If you're going to stick strictly to singleplayer code, you can typically get by with directly referencing the LocalClientConnection object. LocalClientConnection is a special object that describes the player's connection to a local (non-dedicated) server. You can directly access your Player object via: LocalClientConnection.player (i.e. to use setTransform() it'd be: LocalClientConnection.player.setTransform(...)).

The proper way of doing things which works in all cases (singleplayer, multiplayer, dedicated, non-dedicated) is to respect the client/server separation and use commandToServer() to send commands from a client to the server.

In your case you want the player (a client) to be able to press a key and have their character rotate. Detecting the keypress is done on the clientside (notice default.binds.cs is in: /scripts/client/). Instead of trying to rotate the player object there, you'd instead send a command to the server requesting that the server rotate the player.

Here's some example code to hopefully clear things up:
// in default.binds.cs
if($leftPressed)
{
    // left key pressed, need to rotate the player so we'll send
    // a command (which we call RotateRequest) to the server
    commandToServer('RotateRequest');
}

// in scripts/server/commands.cs
function serverCmdRotateRequest(%client)
{
    // we've received a 'RotateRequest' command from the '%client'
    // client; %client.player is their Player object, so let's rotate it
    %client.player.setTransform(...);
}
#4
05/15/2012 (11:36 pm)
Thanks a heap Chris. You've been very helpful. I assume I'm going to have to write a bit of script and use getTransform to determine the players current position and then rotate it because right now it just teleports me to whatever integer I assign.
#5
05/15/2012 (11:43 pm)
Probably. You can use getWords() to pull out the xyz portion of the player's transform and pass that in when you call setTransform().

%pos = getWords(<yourPlayerObject>.getTransform(), 0, 2);

<yourPlayerObject>.setTransform(%pos SPC <your rotation>);
#6
05/16/2012 (12:00 am)
It works flawlessly, thank you so much
#7
05/16/2012 (5:58 am)
To get a list of players you can search in the SimSet named ClientGroup if you need to.

You can also use a trigger and use the entering object - when a player enters the trigger the setTransform() call can be applied directly.

For examples of some uses for these check out the FPS Tutorial, Lessons 3, 6 and 7 I think....
#8
05/16/2012 (2:51 pm)
I'm still working on that tutorial however I went off on a different tangent once I started following it. Curse this imagination.

How would I stop a player from facing/moving a direction if another direction was pressed? I thought adding $<direction>Pressed = 0; to the if function would for example prevent him from facing left while still moving right. Or if in a still position and facing right, the left button is pressed and the character begins to run right while facing left.