Stringtable.js

From Spheriki

Jump to: navigation, search

Stringtable version 1.1 (Light)

This String Table script is a system to allow your game to interact with strings from an external file. This is highly useful to support multiple languages.

version 1.1 brings a few important changes. If the string is not found it will now return the argument (useful for debugging). The script has also been optimized somewhat.

How to use:

  1. Copy this script into a .js file in your game. If you choose to save it in a new file, make sure you use EvaluateScript("filename.js").
  2. You will need to create a stringtable file, which looks like:
stringname=stringcontents
  1. Now you need to call LoadStringTable(filename) when you load your game. The filename should be in the /stringtables/ directory in your game.
  2. This script uses the shortcut function $(). This is used as a replacement for a direct string. So to use a string called "mystring", you write $("mystring") instead. If the key "mystring" exists in the stringtable, its value will be returned, otherwise it it will return "mystring".

Functions:

  • LoadStringTable(filename): load a string table. Always call this before you use any strings!
  • $(stringID): returns the string in the stringtable with the key "stringid".
var cST = []

function LoadStringTable(fname){
   var file = OpenFile("../stringtables/"+fname); // Open file
   var keys = file.getNumKeys(); // Get number of keys
   for(i=0;i<keys;i++){
     cST[file.getKey(i)] = file.read(file.getKey(i), "Stringtable error!");
   }
   file.close();
}

function $(str){
   if(cST[str] == undefined){
      return str;
   }else{
      return cST[str];
   }
}

Personal tools