Game Development Community

syntax of schedule and commandToClient

by Jake H. · in Torque Game Engine Advanced · 04/22/2009 (3:45 pm) · 9 replies

Is there some way to nest a commandToClient into a schedule? Here's the code I have presently...TGEA keeps vomitting on the syntax, but I don't know how to correct.
function clientCmdtimePlayedIndicatorHUD(%this)
{
	timerDisplayHUD.setText($Sim::Time - $SpawnTime)
	schedule(500, 0, 'commandToClient(%this.client, ' @ timePlayedIndicatorHUD @ ', %this)');
}
Basically, I'm creating a client-side tick statement for PlayGui.gui

#1
04/22/2009 (6:29 pm)
Create a function that calls CommandToClient and call it in the schedule:

// On the server
function SendTimeUpdate(%client)
{
    schedule(100,0,"UpdateClientTime",%client);
}

function UpdateClientTime(%client)
{
   commandToClient(%client,'UpdateTime');
}

// On the client
function clientCmdUpdateTime()
{
  timerDisplayHUD.setText($Sim::Time - $SpawnTime)  
}
#2
04/22/2009 (7:01 pm)
Edit: Didn't see the "nesting" commandToClient part... Jaimie is right for the method, but I'll keep the info below which is still useful for syntax and Torquescript.

You can also try this:

%hud = "commandToClient(%this.client, 'timePlayedIndicatorHUD', %this)";
schedule(500, 0, %hud);

It's the single quote marks around the HUD (and the @'s, which you don't really need unless you're trying to translate a variable to a value in a string, which you don't need to do for a HUD name) that caused the parser to freak out. So if you put the string together in a variable and pass that, then you might be fine.

I haven't done it, but even if it doesn't work like that, you can pass this in the schedule which should also work:

'commandToClient(%this.client, 'timePlayedIndicatorHUD', %this)'

Note the ' marks around the HUD name. Hope that helps.
#3
04/22/2009 (7:02 pm)
@Jaimi
Wouldn't this only act once? The way I'm reading it (please correct me if I'm reading it wrong) is Torque would wait 100 ms to call UpdateClientTime. Then UpdateClientTime would call UpdateTime client-side. This function updates the gui. I don't see a loop/tick function...that's what I need the schedule for.

@Ted
That's brilliant! I only hope it works...:-P I'll try it later and update.
#4
04/22/2009 (7:32 pm)
Okay, so I got it to stop getting angry at the syntax. But my code still isn't working, and I'm just not seeing the error here...here it is in its entirety. I'm trying to display $Sim::Time - $SpawnTime (the time it took to go through the menu and actually spawn) on the HUD by repeatedly calling clientCmdtimePlayedIndicatorHUD every second.

spawnPlayer function
function GameConnection::spawnPlayer(%this)
{
	// Combination create player and drop him somewhere
	%spawnPoint = pickSpawnPoint();
	%this.createPlayer(%spawnPoint);
	$SpawnTime = $Sim::Time;
	startTimerHud(%this);
}

startTimerHud function
function startTimerHud(%this)
{
	timerDisplayHUD.setText($Sim::Time - $SpawnTime);
	commandToClient(%this, 'timePlayedIndicatorHUD', %this);
//	commandToClient(%this.client, 'timePlayedIndicatorHUD', %this);
}

HUD Timer Display function located in PlayGui.cs
function clientCmdtimePlayedIndicatorHUD(%this)
{
	timerDisplayHUD.setText($Sim::Time - $SpawnTime);
	error("got here once");
	%this.schedule(1000, 'commandToClient(%this, \'timePlayedIndicatorHUD\', %this)');

}

for the code immediately above this line, I'm not sure if I'm using schedule properly, but I've tried that version, AND...

schedule(1000, 0,'commandToClient(%this, \'timePlayedIndicatorHUD\', %this)');

this version, but to no avail. The code displays the correct time once, then never repeats.
#5
04/22/2009 (7:33 pm)
Yes. Modify the server command to reschedule itself. It seems quite wasteful to have the server do it though - If you want the client to update itself, it's much easier. Just call this once:

function UpdateTime()
{
   timerDisplayHUD.setText($Sim::Time - $SpawnTime) 
   $ClientUpdateTick = schedule(100,0,"UpdateTime");
}

edit:

To get it to started on the client, add this to Playgui.cs:
function PlayGui::onWake(%this)
{
  UpdateTime();
}

function PlayGui::onSleep(%this)
{
   cancel($ClientUpdateTick);
}
#6
04/22/2009 (7:40 pm)
@Jaimi
Ha ha! Works like a charm, mon ami! Ockham's razor, no? Will this work properly if multiple players spawn at alternate times? I guess that's why I was doing all the dodging between server and client...
#7
04/22/2009 (7:42 pm)
@Jake - it will work for multiple clients if you create $SpawnTime on the client.
#8
04/22/2009 (7:43 pm)
Excellent! Thank you! I was looking for the simplest possible way to accomplish this, and wound up getting too complicated after all.

Is it sufficient to create $SpawnTime at the SpawnPlayer function? Or should I put it more blatantly on the client side?
#9
04/22/2009 (8:38 pm)
@Jake - you need to put it on the client side. There's a client side function call "SyncClock()" that gets called from the server when you connect, in client/scripts/client.cs - it's perfect for stuff like this.

function clientCmdSyncClock(%time)
{
   // Time update from the server, this is only sent at the start of a mission
   // or when a client joins a game in progress.
}