Explode() and Simple Scripting

The exciting-sounding explode() function you find in many languages is far more mundane than it sounds. explode() simply takes a string and a delimiter and breaks it up into a list of strings using the delimiter. In other words:

explode("abc,123",",") yields ["abc","123"]

There is no real equivalent to this in Lingo, although you can set the itemDelimiter and refer to and count the items of a string. Unfortunately, you can't just get a list that you can work with directly. Below is the code to do it for you which you can cut and paste:

Here, t is the text string you want to explode, delimiter is the delimiter you want to split the text string using, and terminator is an optional line terminator, handy for stripping off comments and such at the ends of lines.

Note that this explode() routine strips out empty strings, so that if you have multiple delimiters in a row, or delimiters at the front or end of the string, you don't get empty strings in the returned list. This may be unexpected behavior if you're used to an implementation of explode() in another coding environment.

This code also has a handy feature in that if an item starts with a bang ("!"), it takes the value() of the item (sans the bang) and passes it back. If you don't want your explode() function to do that, just strip out that piece, or don't start items with a bang.

Simple Scripting with Explode()

I often use explode()-ed strings as the basis of a quick-and-dirty internal scripting language for games. Using a text member and the explode() function, you can quickly make a set of commands to follow that separate your code from higher-level scripts. This is especially handy if you want to just edit text files to change the game behavior, rather than recompiling.

For instance, you might want to have cutscenes in your game. If it's simple and doesn't require you to develop an editor for cutscenes, you may be able to get away with simple text scripts like so:

In this case, you can easily use explode() to convert each tab-delimited line into a command and its parameters. (Even if you don't use this technique in your final game, it's a handy thing to have around during development.) Here's code that will do this for you:

If you were to pass the above script to explodeLines(), it would return a list of commands back to you. The commands would look like this:

You can now easily step through this script by checking the first element as the action to take, and the following elements as parameters for that action. Because of the "--" terminator and collapsing empty entries, you can also include as many tabs as you want and lingo-style comments at the end of lines or in their own lines.

Made on Mac