Game Development Community

Slight delay in my text writer...

by Nic Cusworth · in Torque Game Builder · 01/03/2007 (3:29 pm) · 5 replies

I've got a text box and I want to display the text in it a character at a time. I need some way to wait the update after each character.

Code looks like this:

for (%x = 0; %x < strlen(%wholeText); %x++)
{
%partText = getSubStr(%wholeText, 0, %x);
$textBoxText.text = %partText;
// NEED A WAIT HERE!!!!
}

I know I'm heading in the sticky area of wanting a WAIT function (which I REALLY REALLY DO WANT)... any nice work arounds for this that I'm missing?

Nic.

#1
01/03/2007 (5:07 pm)
I would do this...
Instead of Waiting which would halt other things going on, just use the schedule function..

function textWriter(%this)
{
%partText = getSubStr(%wholeText, 0, $TextIndex);
$textBoxText.text = %partText;

$TextIndex++;
%this.textwriterSchedule = %this.schedule(100, textWriter);

}

This would turn your Index variable into a Global, increment it each time, and schedule it to work again in xxx milliseconds..
You could even generate a random schedule time that would simulate typing or something...
Hope this helps. I am not the best at the schedule function and how %this is used so you will have to figure out the exact implementation for your purposes...
Also, you would need to perform the checks on that $TextIndex variable (compared to strlen...)
#2
01/04/2007 (12:41 am)
As I slept on it I realised this was pretty much how I'd have to do it. I guess I'm just used to working with game scripts that will let me perform a simple wait. I've no real idea how TGB is built but surely there some way to implement a command which works like a schedule but doesn't mean creating more and more functions.

Anyway. This works:

function t2dSceneGraph::displayText(%this, %index)
{
echo("displaying text");
%wholeText = "Here's some text";
%partText = getSubStr(%wholeText, 0, %index);
$textBoxText.text = %partText;
%index++;
if (%index < strlen(%wholeText))
{
%this.schedule(20, displayText, %index);
}
}
#3
01/04/2007 (1:29 am)
...and just in case anyone else wants this. I just re-wrote it so I can add control characters to the script.

function t2dSceneGraph::displayText(%this, %index, %partText)
{

	%wholeText = "SOME TEXT THAT COULD ALSO BE PASSED IN BUT I'M REFERENCING A GLOBAL";
	%curChar = getSubStr(%wholeText, %index, 1);

	if (%curChar $= "#")
	{
		echo("Found a line Break");
		%partText = %partText @ "\n";
	}
	else
	{
		if (%curChar $= ".")
		{
			%delay = 800;
		}
		else
		{
			%delay = 30;
		}
		%partText = %partText @ %curChar;
	}

	$textBoxText.text = %partText;
	%index++;
	
	if (%index < strlen(%wholeText))
	{
		%this.schedule(%delay, displayText, %index, %partText);
	}
}

It works pretty well and means if I need extra controls in the text it's easy to add.

Nic.
#4
01/13/2007 (4:07 am)
Totally awesome text script !
Thanks a lot, it really helps me, you rock ^^
#5
01/13/2007 (4:15 am)
Thank you :)

Nic.