Game Development Community

Torque 3D Gui Editor help

by William Goh (Joondalup IT TAFE) · in Torque 3D Professional · 08/15/2009 (7:31 am) · 4 replies

Hi, I need help with the Gui editor and is a beginner to Torque. I want my game to start and consist of two button controls which is for BoomBot and the ForgeSoldier. When I click on the BoomBot button it would allow me to play as the BoomBot character and vice versa. So far iv only entered the Gui Editor and clicked on the GuiButtonCtrl twice and named one of them ForgeSoldier and the other BoomBot. What else would I need to do?

#1
08/15/2009 (8:25 am)
Hello William,

First of all, you'll need to send the selection to the server. To do that, you could use the commandToServer function. Do a search for it to see examples of how it is used. I'll also explain.

When clicking one icon, send over the name of the datablock of BoomBot, when the other button is clicked, send the name of the ForgeSoldier datablock to the server. They are called BoomBotData and ForgeSoldierData. You can find the declarations in art/datablocks/player.cs.

So on the button's Command you'd put something like choosePlayerType("BoomBotData"). You'd need to create this client side function to pass data to the server (scripts/client/client.cs is a good enough place for this, but you should create its own file and exec that within the client init phase):
function choosePlayerType(%playerDatablock)
{
   commandToServer('SetPlayerDatablock', %playerDatablock);
}

Notice the use of ' instead of " for strings. This is important, it means that the name of the function to be called on the server is a tagged string. The network only sends it once, and only its id the next time from then on to preserve bandwidth.

On the server side, in server/scripts/commands.cs, you'll need to implement the function that this client calls:

function serverCmdSetPlayerDatablock(%client, %playerDatablock)
{
   $Game::DefaultPlayerDataBlock = %playerDatablock;
}

$Game::DefaultPlayerDataBlock is the global server variable that will affect what object you spawn as a player when you enter the game.

We still need to launch the mission. We can instruct the client from the server side to start the mission once the datablock has been set by adding one more line to the previous function:

function serverCmdSetPlayerDatablock(%client, %playerDatablock)
{
   $Game::DefaultPlayerDataBlock = %playerDatablock;
   commandToClient(%client, 'startSinglePlayer');
}

Notice, that the first parameter of a server side command function such as this one will always be the client connection. This means that this identifies the user who has called that server command. This makes it very easy to send a command back to the client. Being the server, we'll need to select the client we send the command to. commandToServer has no %server parameter, because there's only one server, so its pretty clear which one you want to instruct to do something.

Of course, the client needs to accept this command. On the client side, we'll create a function that will run when the server executes that commandToClient. scripts/client/client.cs will be a good place for this:

function clientCmdStartSinglePlayer()
{
   loadDefaultMission();
}

That starts loading the default mission.

A few notes:

It doesn't matter if you create a single player game. It's always better to think of your game as a server to which one client is connected. It will save you lots of trouble - not only when you want to make your game multiplayer.

The commandToClient and commandToServer targets get the clientCmd.. and serverCmd.. prefixes automatically added before your function's name.

Good luck, I hope this helps you some. Small tasks like this one will teach you a lot about Torque 3D.
#2
08/15/2009 (8:47 am)
Way to go Konrad! Glad I throught to refresh the page... saved me some typing ;)

Just to note that if you use the method that Konrad outlined and you set each button to affect the $Game::DefaultPlayerDataBlock variable: make sure that you do not assign a datablock to your spawnpoints when you add them to a level, otherwise that will override the global variable that you just set with your button choice.
#3
08/15/2009 (8:53 am)
Oh, right, thanks Michael for the correction!
#4
08/16/2009 (11:30 pm)
Thanks very much for the help. I'll try it out to see how it works.