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.
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.
About the author
#2
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);
}
}
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
It works pretty well and means if I need extra controls in the text it's easy to add.
Nic.
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.
Torque Owner Dustin Sims
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...)