Lesson 9: Strings Revisited
A C compiler.
Instructions on using the LCCWin32 Compiler.
A common complaint about QuakeC was the lack of any decent string handling routines. With Quake2 using C :) lets now explore what we can do. For those of you that wanted a bot that could understand what the player said, you could now write a parser and actually make the bot do so!
In most cases we simple want to concatenate strings together. We can do this several ways, depending upon what the strings are. Firstly lets consider our basic string:
now suppose we want to add mystring onto the end of my_other_string we would normally use the function:
char *strcat( char *strDestination, const char *strSource );
which concatenates the string strSource onto the end of the string strDestination so for our example we have:
In other cases we may wish to place the contents of a variable using a string, in which case we would normally use the function:
int sprintf( char *buffer, const char *format [, argument] ... );
which is effectively the same as our printf except the first arguement is a string we wish to print to. We could you this to build up a line of information about a character, whoose attributes are stored in the variables health and ammo i.e.
int ammo; int health; char name; char message; sprintf(message, "%s, your health is %d, and you only have %d ammo left...\n", name, health, ammo);
Would set the string message to: Bob, your health is 10, and your only have 20 ammo left... (assuming the variables had those values).
Copyright © 1997, John Crickett & Neil Henderson.