Manipulating words in a string

Each element within a string that is separated from another element by spaces is considered to be a word. Using this concept of words can extremely useful for the reading and extraction of individual elements within a string. How so? Well, for example, let's say we’re making an RPG, and there is an enemy camp with patrolling goblins.. what could we do if we want these goblins follow a fixed, looped path?

A way to accomplish this by manipulating words in a string is to give each NPC a string, and then designate a direction they will move to as a word. The string's example contents might look something like this: {NW NW NE NE SE SE SW SW }.

From there, it's a simple matter of removing the first word from a string (in this case, NW), comparing it with conditions, and then moving the NPC one space NW. Afterwards, the word NW can be added to the end of the string to keep the cycle going. There are plenty more ways manipulating words in a string could be useful, and in the following tutorial we'll briefly explain the components of how to do so in more detail.


Tutorial 5: Planet eater

So what's a good example of how to extract entire words from a string, as well as show the characteristics of what's considered a word in DS..? How about something fun: we'll make a set of triggers to let us "eat" planets from the solar system, one by one. Muahaha!

So, first we create a list with the names of every planet in the solar system. Each planet is a word, separated from the others by another by a space. In other words (pun intended), each element (planet name) within this list is considered to be a word for DS purposes.

(0:0) When everything is starting up,

(5:250) set message ~SolarSystem to {Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune}.

Then we can make a command to eat planets! We need to let that imagination soar, after all.

(0:31) When someone says {@EatPlanet},

(1:76) and the length of message ~SolarSystem is greater than 0,

(5:277) remove the first word from message ~SolarSystem and put it in message ~EatPlanet.

(5:281) set variable %Planets to the length of message ~SolarSystem in words separated by spaces.

(5:200) emit message {You just ate the planet called ~EatPlanet and there are %Planets left in the solar system out of the original 8.} to the triggering furre.

So if we were to use the @EatPlanet command for the very first time, the first word in the message would be the very first planet name in the ~SolarSystem string, Mercury. Then the ~SolarSystem string would look like: {Venus Earth Mars Jupiter Saturn Uranus Neptune}, while ~EatPlanet would be: {Mercury}.

The value in the %Planets variable will depend on the number of words that are in the ~SolarSystem string, providing an accurate count of how many planets (words) there are left within the string.

Account E-Mail

Password