Working user scripting

The user scripting I mentioned before I went to Brussels is now basically done. The most amazing part is that I did most of it while I was in Brussels on a computer without PHP (amazing because apart from a few minor typos it actually worked).

The whole thing is actually simpler than I expected it to be and as such it can do stuff I didn’t plan. The first is maths expressions. This is something that may cause a problem however since it just just evals the code (after substituting variables and stripping possible harmful stuff).

The coolest feature I think though is handling aggregate data types. When you call a script you also pass it an array of variables. These variables can be of any type from integers or strings to arrays or objects (or even resources); it’s upto the functions you define to actually handle them. Now different elements of arrays and properties of objects can be accessed using a slghtly suspect-looking dot notation. So (from a working example I wrote) you could use something like $fleets.1.name which would access the name of the second element of the fleets array.

It’s worth pointing out that I decided to stick with numbering arrays from 0 since as a programmer that’s what I’m used to.

Anyway when I say that is accesses the name of the second element, it checks the type of the element and accessed it as an array ($fleets[1]['name']) or a property ($fleets[1]->name) as appropriate.

You can also do $fleets.$n.name to access the name of the nth element (although be warned that if n doesn’t reference a key of the fleets array then it won’t work obviously). Which brings me to biggest problem with it at the moment… everything fails silently if something isn’t right.

The way the scripting is called is also more flexible than I originally planned. The first way is:

$xgs->parseXGS('event', 'fleetentersector', $script, $vars);

$script is the a well formed XML script and $vars is an associative array of variables. Any script tags with an attributes of event equal to fleetentersector (<script event="fleetentersector">). This is was done mainly with Galaxia in mind so players could create different types of scripts other than ones designed to respond to events (i.e. use it just to create a batch command system).

The second far more flexible way to use it is:

$xgs->parseXPath($xpath, $script, $vars);

Where $xpath is an XPath expression selecting the root node of the script. Just to point out the first example actually just calls that with XPath expression "script[@$type='$att']" ($type is the attribute name and $att its value).