Main      Site Guide    
Smash Tutorial

Example: Using String Encoding


Using strings in Smash offers a range of possibilities that may not be immediately obvious. Here are a few uses:

Manipulating Option Text

For starters, string encoding can be used when you want the text for an option dependent on the state of the game. In the following example, the player can paint an object one of several different colors. The text for the option to pick it up includes the color it is currently painted:

* p1 Paint the egg red. v color = c:red * p2 Paint the egg orange. v color = c:orange * p3 Paint the egg yellow. v color = c:yellow * p4 Paint the egg green. v color = c:green * p5 Paint the egg blue. v color = c:blue * p6 Paint the egg purple. v color = c:purple * t Take the {'color} egg. a egg

In your inventory, the egg will just appear just as "Egg," but the color can be included in the description of the egg, which appears when you select it within your inventory. Here's the objectactions.sma function that will do that:

~ egg p The egg is painted {'color}.

Breaking the Eight-Character Limit, Part 1

Smash strings being limited to eight characters can be inconvenient, but it's quite possible to overcome. Here is how we would code our painted egg example from above if we wanted to include such colors as "chartreuse" and "vermilion." Remember that 0 is equivalent to an empty string.

* p1 Paint the egg red. v color1 = c:red v color2 = 0 * p2 Paint the egg vermilion v color1 = c:vermi v color2 = c:lion * p3 Paint the egg green. v color1 = c:green v color2 = 0 * p4 Paint the egg chartreuse. v color1 = c:chart v color2 = c:reuse * p5 Paint the egg heliotrope. v color1 = c:heliotro v color2 = c:pe * t Take the {'color1}{'color2} egg. a egg

String-Based Hall of Fame Entries

Games with string-based custom Hall of Fame columns are easier to set when you use string encoding. Let's say you have a military game in which the object is to complete a certain mission. You win if you complete it at all, but how well you do on the mission determines the rank you are promoted to in the end. We might want to record this rank in the Hall of Fame.

Furthermore, let's say that you can choose one of several different missions. In mission 1, you rescue a kidnapped official; in mission 2, you spy on an enemy; in mission 3, you bomb enemy headquarters. It would make sense to include this information in the Hall of Fame as well.

First let's code this the traditional route: we'll make a variable called "rank" that stores 1 if the player is a captain, 2 if he's a major, 3 if he's a colonel, and 4 if he's a general. Likewise, a variable "mission" will be 1 for the rescue, 2 for the spying, and 3 for the bombing. Here's how we would code that:

* r Return home in victory. p The mission successfully completed, you return home to honor and glory. c rank=1,mission=1 w General|Rescue Mission c rank=2,mission=1 w Colonel|Rescue Mission c rank=3,mission=1 w Major|Rescue Mission c rank=4,mission=1 w Captain|Rescue Mission c rank=1,mission=2 w General|Spying Mission c rank=2,mission=2 w Colonel|Spying Mission c rank=3,mission=2 w Major|Spying Mission c rank=4,mission=2 w Captain|Spying Mission c rank=1,mission=3 w General|Bombing Mission c rank=2,mission=3 w Colonel|Bombing Mission c rank=3,mission=3 w Major|Bombing Mission c rank=4,mission=3 w Captain|Bombing Mission

Obviously, this is kind of an unwieldy mess, and it gets much, much worse as the number of possible ranks or missions increase, or if you had a third string-based Hall of Fame column.

Well if we change the way our "rank" and "mission" variables work, so that "rank" contains the strings "General", "Colonel", "Major", or "Captain", and "mission" contains "Rescue", "Spying", or "Bombing", then we can radically condense this code:

* r Return home in victory. p The mission successfully completed, you return home to honor and glory. w {'rank}|{'mission} Mission

Typing

Smash does not have any mechanism whereby a user can type in text. However, the use of string encoding can provide a means for simulating this. Let's say that you were writing a game which contains a computer terminal, and one of the puzzles is to figure out the password that would gain access to the system. You can't provide a menu option for every possible password -- there are far, far too many possibilities, and if you limited the number of choices to a few random possibilities, the player could simply try them all and skip the part of the game where you find out what the password really is.

Here is a snippet of code for a "computer" location that lets you type in text. For simplicity, we will assume the computer's keyboard only has five keys: A, B, C, backspace, and enter.

. You are sitting at a computer terminal. On the screen is a password entry box. c chars_typed , You have entered the following text: {'typed} * ta Type "A" c chars_typed < 8 s typed ++ c:A s chars_typed + 1 C p The password entry box won't accept any more characters. * tb Type "B" c chars_typed < 8 s typed ++ c:B s chars_typed + 1 C p The password entry box won't accept any more characters. * tc Type "C" c chars_typed < 8 s typed ++ c:C s chars_typed + 1 C p The password entry box won't accept any more characters. * bk Type backspace. c chars_typed s typed >> 8 s chars_typed - 1 C p The computer beeps. * rt Type return. c typed = c:CABBA p You have gained access to the computer! v access = 1 C p The computer beeps and says that you have entered the wrong password. s typed = 0 s chars_typed = 0

The mechanics here are pretty simple. The variable "typed" keeps track of what we've typed so far. "chars_typed" keeps track of how many characters we've typed so far. When you hit backspace, the rightmost character is removed (to remove characters off the end of a string, right-shift by 8 times the number of characters you want to remove). When you hit enter, the variable "typed" is compared with the real password, which in this case is "CABBA", and if you got the password right, you get access to the system.

The same mechanism can be used to enter your name at the beginning of a game, so that the computer can refer to you by name thereafter.

Breaking the Eight-Character Limit, Part 2

Breaking the eight-character limit with the previous password-entry example can be done using the method described earlier, but it's tricky when you're dealing with strings that do not have any upper bound at all. The egg color example from earlier worked because the upper limit on the length of a color was 16 characters, so two string variables sufficed. What if you want to allow the player to "type" in any arbitrary number of characters?

The easiest way to do this is to only store one character per variable and have an array of variables which store the characters entered, as in the following excerpt of code:

. You are sitting at a computer terminal. On the screen is a password entry box. c chars_typed , You have entered the following text: , s i = 0 L i < chars_typed ; {'char_{i}} s i + 1 * ta Type "A" s char_{chars_typed} = c:A s chars_typed + 1 * tb Type "B" s char_{chars_typed} = c:B s chars_typed + 1 * tc Type "C" s char_{chars_typed} = c:C s chars_typed + 1 * bk Type backspace. c chars_typed > 0 s chars_typed - 1 C p The computer beeps. * rt Type return. c chars_typed = 5 , char_0 = c:C , char_1 = c:A , char_2 = c:B , char_3 = c:B , char_4 = c:A p You have gained access to the computer! v access = 1 C p The computer beeps and says that you have entered the wrong password. s chars_typed = 0

As you can see, the code for entering characters is pretty simple; complexity is added only when printing out the text (it loops through all the characters, printing each as it goes) and checking the final result of the text (the length and each character must be checked independently).

Note that when we type backspace or return, we adjust "chars_typed" to reflect the number of characters now in the buffer, but technically the variables are still set for what was previously typed. This is slightly inefficient with regard to the size of the player's save file and could slow the game down a bit. We can fix this problem by changing the behavior of the backspace and return keys as follows:

* bk Type backspace. c chars_typed > 0 s chars_typed - 1 s char_{chars_typed} = 0 C p The computer beeps. * rt Type return. c chars_typed = 5 , char_0 = c:C , char_1 = c:A , char_2 = c:B , char_3 = c:B , char_4 = c:A p You have gained access to the computer! v access = 1 C p The computer beeps and says that you have entered the wrong password. s i = 0 L i < chars_typed s char_{i} = 0 s i + 1 s chars_typed = 0

This is functionally equivalent to the previous example, just more efficient with regard to speed and save file size.