By save data do you mean to persistent storage with store
and restore
? If so you're limited to using variables, rather than trying to get creative like using a room as an array with tile names as values (which I think is possible!).
I have a pretty shaky implementation of an array using only variables and some game events as helper functions, with the caveat that you need to predefine a limit on the length of the array (with the amount of code increasing linearly it goes up).
Essentially it comes down to using one variable to store the length of the array and another variable for each positional value:
array_length = 3
array_0 = "foo"
array_1 = "bar"
array_2 = "baz"
I suppose the nice news is that you get to decide whether you want the array to be zero indexed or not As per any undeclared pulp script variable, any undeclared positional value will return 0
.
Unfortunately we can't just loop over the length of the array like:
i=0
while i<array_length do
// Do something
i++
end
Because we can't dynamically reference variable names (afaik), so instead all our array events need to manually check which position they are affecting:
on appendToArray do
array_length += 1
if array_length==1 then
array_0 = arg1
elseif array_length==2 then
array_1 = arg1
elseif array_length==3 then
array_2 = arg1
elseif array_length==4 then
array_3 = arg1
end
end
Hence the length of the array being hardcoded and the code required increasing as you raise that limit.
Custom events can't take arguments (again afaik), so instead we have to use variables as arguments and set these before calling the event handler. If you use names like arg1
, arg2
etc. I think it helps avoid any clashes in the global scope (at least for me!).
arg1 = "qux"
tell event.game to
call "appendToArray"
end
If you want to use more than one array (very reasonable) you could also pass the array as an argument to a single set of generic helper functions, but because you would need to manually split out which array you are affecting with a conditional anyway, I think it's probably easier to duplicate the helper functions for each array you add.
Anyway, you probably want an array so you can do something useful and array-like with it. Judge this solution for yourself, your mileage may vary!
on valueInArray do
if array_0 == arg1 then
return = 0
elseif array_1 == arg1 then
return = 1
elseif array_2 == arg1 then
return = 2
elseif array_3 == arg1 then
return = 3
else
return = -1
end
end
Similarly to using global variables in place of arguments, we have to use a global return variable in place of a return value.
arg1 = "baz"
tell event.game to
call "valueInArray"
end
say "{return}" // 2
You could then update that specific value in the array:
on updateArray do
if arg1==0 then
array_0 = arg2
elseif arg1==1 then
array_1 = arg2
elseif arg1==2 then
array_2 = arg2
elseif arg1==3 then
array_3 = arg2
end
end
if return > -1 then
arg1 = return
arg2 = "xyzzy"
tell event.game to
call "updateArray"
end
end
And we end up with an array like:
say "{array_length} {array_0} {array_1} {array_2} {array_3}" // 4 foo bar xyzzy qux
We could do plenty more besides.
This probably isn't the best way of implementing arrays, I'm no language author and it's very manual, but it's what I figured out when I was playing around