The Topic
We'll be discussing one of the core concepts used by the FSO server, a feature of Lingo which is relied upon heavily to store and interact with almost all of the data which it processes. That feature is the itemdelimiter, and it's associated parts.
The Explanation
Let's say we wanted to write a function which would find the first place prize for the winner of the assassin games. First we'd need to open the file and get it's contents, and then we'd need to get out our data. So... on GetFirstPlaceAssassinPrize
set FileName = "C:\FSOServer\DAT\SETTINGS\AssassinPrizes.txt"
set AssassinPrizes = file(FileName).read
set ThePrize = "" -- uh... how do we get the prize out?
return ThePrize
end
The first two lines of the function set up a filename from which to read text, and then read in this text and put it in the variable AssassinPrizes. The last line of the function tells the server to put ThePrize where the function was used, more or less, allowing you to do things like put GetFirstPlaceAssassinPrize() .
So, next we have to figure out how to get the first prize out. The file we're using looks like this: 'Long Sword:1000 Gold:500 Gold:Knife'. This is where itemdelimiter comes in handy. If we set it to ':', then we can pull out the various pieces which are divided by :s. on GetFirstPlaceAssassinPrize
set FileName = "C:\FSOServer\DAT\SETTINGS\AssassinPrizes.txt"
set AssassinPrizes = file(FileName).read
set the itemdelimiter = ":" --set itemdelimiter to :
set ThePrize = item 1 of AssassinPrizes --and pull out the first item
return ThePrize
end
Cool; now we have a function which can get the first place prize out of AssassinPrizes.txt. So, how would change that prize to something different? Well, look at this function. on SetFirstPlaceAssassinPrize(TheNewPrize)
set FileName = "C:\FSOServer\DAT\SETTINGS\AssassinPrizes.txt"
set AssassinPrizes = file(FileName).read
set the itemdelimiter = ":" --set itemdelimiter to :
put TheNewPrize into item 1 of AssassinPrizes --and put in our new prize
file(FileName).write(AssassinPrizes) --write the new assassinprizes file
end
Alright, so we're using put to place the new prize passed to the function into the list of assassin prizes, and then writing the new data back out using the same filename that we read it in with.
But wait, didn't we use put before to output stuff to the server log? Put has more than one use; depending on how you use it, it will either output the string to the server log or place it into the data you give it.
Other similar features you might want to try is to replace the 'item' part of both of the functions to either 'line' or 'char', as these can be used in the same way expect for delimiting based on returns or individual characters, respectively. In addition, you can set the itemdelimiter to any value you want.
We now have two functions which let us read and modify the assassin game prize data from our scripts! The same techniques can be used to modify everything from character files to maps, or to create your own lists and other structures. In addition, we now know how to read and save things to files, which can be very useful.
|