Lets learn about lists.
There are a couple of lists you can use in cowscript. The most basic is a sring list you can get a random item from like this:
var $sound to (ding,dong,ping,thwack,boom);
Fun and useful but you cant do anything more than retrieve a random item from this kind of list.
For a more complex list you need the 'load' 'loop' and 'save' statements;
Here is a simple example where we add to a pocket variable everyting that is said or asked near our object:
##say:runsub recordit;
##ask:runsub recordit;
##recordit:
load $actor's tape;
loop paste $init_msg;
save $actor's tape;
say 'msg',"[$actor] recorded '$init_msg'";
First we 'load' the contents of our actor's pocket variable (called tape) into our list.
NOTE: you can only have one list in memory at a time, sorry about that but it keps the code simple as you dont need to specify which list your working with.
The load command also randomly picks one item from your list any puts it in $randomitem and stores the number of items in your list in $list_count;
Next we use the 'loop' to paste a new line at the end of it.
Finally we save the list back into the pocket variable and inform the world that we did something.
The 'loop' statement handles all of the list manipulation, load and save simple read and write the list - if you dont save it your changes will be lost.
Here is a sample list containing three items (lines of text):
small dog
large cat
huge mouse
Loop can have these params: cut|copy|paste or shift.
cut {text} = cuts out of the list any line that contains matching text.. the last line cut that matched is retained in the $list_clipboard variable.
Using our sample list, doing a loop cut cat; would cut out the 'large cat', reduce the list to only 2 items and leave the 'large cat' in out $list_clipboard.
If we loop cut ge; we would cut out both 'large cat' and 'huge mouse' and leave 'huge mouse' in the clipboard.
copy {text} = puts a copy of the list item that matches into the $list_clipboard but does not remove it.
Using our sample list, doing a loop copy cat; would not change the list and would leave the 'large cat' in our $list_clipboard.
paste {text} = adds the text as a new line at the end of the list
If we loop paste tiny rat; we would end up with 4 items in our list with 'tiny rat'
You can also paste variables and combinations like this:
loop paste $sound;
loop paste "$thisname made this $sound";
shift = shifts (cuts) the first item off the top of the list, in our sample list the 'small dog' goes into $list_clipboard and the $list_count is reduced by 1.
Finally you can manipulate each item in the list by naming a block of code to run after the 'loop' statement like this:
load $actor's tape;
loop sayit;
##sayit: say 'msg'"[$actor] says '$listitem'";
Here we run sayit for each line in our list, using the $listitem variable that holds the current item in the list at that time.
If your feeing advnterous you can make use of the fact that $list[0] is the first item in the list, and $list[1] is the second etc..
This might be handy if you want to process the items in your list slowly. Just keep a counter going, slowing increasing it until it is greater than the $list_count then you know you have fnished.
Here is a object that sequentially says the next line in a list every tickloc:
##tickloc:saysomething;
##saysomething:
load $actor's script;
var $thisline to $actor's lastline;
if $thisline eq '' then startatthetop else continue;
if $thisline >= $list_count then end else sayit;
##sayit:
say 'say',"[$actor] says '$list[$thisline]'";
add 1 to $thisline;
set $actor's lastline to $thisline;
##startatthetop: var $thisline to 0;
In this example we are saving two pocket variables in the actor. One called 'script' which holds our list and te oher called 'lastline' which keeps track of th last line read from the script.
Notice that we have to check that the $actor's lastline is empty, and if so we make the $thisline equal 0 so we get to read out the first line (which is $list[0]) instead of failing with $list[].
Also note, the continue and end blocks in the 'if' sarements are bogus names, I could ahve used 'peant and butter just as effectibly. These names just help anyone reding the code understand what will happen when the the condtions match or fail.