Game Development Community

sprintf function

by Bryan "daerid" Ross · in Torque Game Engine · 12/03/2001 (12:14 pm) · 2 replies

Anybody have a ready made sprintf function for the TGE?

What I'm thinking of is the Tribes 1 sprintf function.

To show you what I mean:

sprintf( "The %1 jumped over the %2 %3", "fox", "lazy", "dog" );

#1
12/03/2001 (3:17 pm)
In the C++ code, you can use the function dSprintf() in the same fashion as you describe. In the Torque scripts, you might try using the concatenation character @ to build a string:

"The " @ %animal @ " jumped over the " @ %thing1 @ " " @ %thing2 @ "."
#2
12/03/2001 (5:49 pm)
n/m

Coded it meself:

ConsoleFunction(sprintf,const char *,1,10,"sprintf(string,arg1,arg2...arg9);")
{
	argc;
	char * ret = Con::getReturnBuffer(1024);
	ret[0] = '[[60c216ed3c392]]'; //initialize and terminate the string
	char chr[2];
	const char *ptr = argv[1];
	while( *ptr != '[[60c216ed3c392]]' )
	{
		if( *ptr == '%' )
		{
			if ( ptr[1] >= '1' && ptr[1] <= '9' )
			{
				chr[0] = ptr[1];
				if ( ( dAtoi(chr) + 1 ) < argc )
				{
					dStrcat( ret, argv[ dAtoi(chr)+1 ] );
					ptr+=2; // skip the %n
					continue;
				}
			}
		}
		chr[0] = *ptr;
		chr[1] = '[[60c216ed3c392]]';
		dStrcat( ret, chr );
		ptr++;	
	}
	return ret;
}