Game Development Reference
In-Depth Information
We then enter a for loop that causes the lRow variable to iterate between 0 and
the number of populated rows in the spreadsheet. Within the loop we use the
worksheet object to access the spreadsheet cells an entire row at a time using
the row_values method.
Python has a built-in list type and this is what is being used to access all the cells on
the row in one go. The lCells variable will contain a list whose elements are each
cell in the row.
Finally we use the Python print command to display the entire list to standard
output. You can use print in Python to display just about any type, including lists,
in a human-readable form.
The UI example project that accompanies this chapter includes a Python script
that will take a spreadsheet as input and convert it into a simple datafile for each
language contained in the spreadsheet.
The datafiles list the number of strings in the file followed by a hash value generated
from the text identifier field (the first column of the spreadsheet) and the string itself.
It is fairly trivial to write C++ code to load this file into memory.
The use of a hashing function here means it is possible for two strings
to end up with the same hash value, causing a collision in the string
table that means the wrong string may get returned. In practice a
good hashing function will mean this hardly ever happens, but if you
start getting the wrong string returned this might be the cause. The
easiest way to rectify such a problem is just to rename one of the text
identifiers in the collision!
To access a particular text string, we use the identifier field in our code. A hash value
is generated from the identifier field and the list of string data is searched for that
hash value. If a match is found, the corresponding text string is returned, otherwise
an assert can be raised and a default string of text returned. The default text can be
something like "Missing String!", which makes it easier to track down problems such
as getting the identifier field wrong in code, or the string just not being present in the
text datafile when it should be.
Selecting the correct language to use at runtime
We now have the ability to supply our game with strings of text in multiple
languages, but how do we decide which of those languages to actually use? One
method of course is to implement a language select screen during startup of
our game and then load the relevant string table depending on the user's input.
However, there is a much nicer way available to us.
 
Search WWH ::




Custom Search