UTIL VarArgs: Difference between revisions
		
		
		
		
		
		Jump to navigation
		Jump to search
		
				
		
		
	
| No edit summary |  (Formatting, more examples) | ||
| Line 1: | Line 1: | ||
| {{wrongtitle|UTIL_VarArgs}} | |||
| '''UTIL_VarArgs''' is a [[UTIL]] provided to build a C-String similar to printf. This is only available on the '''server'''. It is useful for quickly building a composite string. | |||
| == Usage == | |||
| < | <source lang=cpp> | ||
| //----------------------------------------------------------------------------- | |||
| // Purpose: Prints a message to console | |||
| // Input  : format - See examples | |||
| // Output : a char-array that is easily used in Msg, etc. | |||
| //----------------------------------------------------------------------------- | |||
| char *UTIL_VarArgs( char *format, ... ) | |||
| { | |||
| 	va_list		argptr; | |||
| 	static char		string[1024]; | |||
| 	va_start (argptr, format); | |||
| 	Q_vsnprintf(string, sizeof(string), format,argptr); | |||
| 	va_end (argptr); | |||
| 	return string;	 | |||
| } | |||
| </source> | |||
| == Examples == | |||
| <source lang=cpp> | |||
| int iCreepers = 3; | int iCreepers = 3; | ||
| char * pFortress = "My Awesome Fort."; | char * pFortress = "My Awesome Fort."; | ||
| char * pOutput = UTIL_VarArgs( "There are %i creepers outside of your %s.", iCreepers, pFortress ); | |||
| Msg("%s", pOutput ); | |||
| //Prints: There are 3 creepers outside of your My Awesome Fortress. | |||
| </source> | |||
| <source lang=cpp> | |||
| //This example uses a UTIL_VarArgs to prefix a string, and then execute a console command. | |||
| void CPointDevShotCamera::DevShotThink_TakeShot( void ) | |||
| { | |||
| 	// Take the screenshot | |||
| 	CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost(); | |||
| 	if ( !pPlayer ) | |||
| 		return; | |||
| 	engine->ClientCommand( pPlayer->edict(), UTIL_VarArgs( "devshots_screenshot \"%s\"", STRING(m_iszCameraName) ) ); | |||
| 	// Now take the shot next frame | |||
| 	SetThink( &CPointDevShotCamera::DevShotThink_PostShot ); | |||
| 	SetNextThink( gpGlobals->curtime + (DEVSHOT_INTERVAL - 1) ); | |||
| } | |||
| </source> | |||
| < | <source lang=cpp> | ||
| char *  | //This example gets the current map name, and appends it onto the end of a prefix string | ||
| </ | //It's used to find per-map files, such as custom soundscapes. | ||
| const char *mapname = STRING( gpGlobals->mapname ); | |||
| const char *mapSoundscapeFilename = NULL; | |||
| if ( mapname && *mapname ) | |||
| { | |||
| 	mapSoundscapeFilename = UTIL_VarArgs( "scripts/soundscapes_%s.txt", mapname ); | |||
| } | |||
| </source> | |||
| [[Category:Programming]] | |||
| [[Category:UTIL]] | |||
Revision as of 19:43, 11 April 2011
Template:Wrongtitle UTIL_VarArgs is a UTIL provided to build a C-String similar to printf. This is only available on the server. It is useful for quickly building a composite string.
Usage
//-----------------------------------------------------------------------------
// Purpose: Prints a message to console
// Input  : format - See examples
// Output : a char-array that is easily used in Msg, etc.
//-----------------------------------------------------------------------------
char *UTIL_VarArgs( char *format, ... )
{
	va_list		argptr;
	static char		string[1024];
	
	va_start (argptr, format);
	Q_vsnprintf(string, sizeof(string), format,argptr);
	va_end (argptr);
	return string;	
}
Examples
int iCreepers = 3;
char * pFortress = "My Awesome Fort.";
char * pOutput = UTIL_VarArgs( "There are %i creepers outside of your %s.", iCreepers, pFortress );
Msg("%s", pOutput );
//Prints: There are 3 creepers outside of your My Awesome Fortress.
//This example uses a UTIL_VarArgs to prefix a string, and then execute a console command.
void CPointDevShotCamera::DevShotThink_TakeShot( void )
{
	// Take the screenshot
	CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
	if ( !pPlayer )
		return;
	engine->ClientCommand( pPlayer->edict(), UTIL_VarArgs( "devshots_screenshot \"%s\"", STRING(m_iszCameraName) ) );
	// Now take the shot next frame
	SetThink( &CPointDevShotCamera::DevShotThink_PostShot );
	SetNextThink( gpGlobals->curtime + (DEVSHOT_INTERVAL - 1) );
}
//This example gets the current map name, and appends it onto the end of a prefix string
//It's used to find per-map files, such as custom soundscapes.
const char *mapname = STRING( gpGlobals->mapname );
const char *mapSoundscapeFilename = NULL;
if ( mapname && *mapname )
{
	mapSoundscapeFilename = UTIL_VarArgs( "scripts/soundscapes_%s.txt", mapname );
}