Plan for real script arrays

From OHRRPGCE-Wiki
Jump to: navigation, search
Obsolete.png
This article (or part thereof) is obsolete. It may have made sense in the past, but it does not make sense now. It is kept here only for historic curiosity and/or posterity.

See Plan for dynamic types in HamsterSpeak instead

A proposed interface for script arrays:

variable(a)
a := create array(5)

would create an array of 5 elements numbered 0-4 and return a handle to it.

delete array(a)

would erase the array and mark the handle as unused.

read array(a, 0)

Would return element 0 from the array identified by the handle in the variable a.

a[0]

Hspeak could magically convert a[0] into read array(a, 0)

write array(a, 0, n)

would write value n into element 0 of the array identified by the handle in variable a.

a[0] := n

would it be practical for hspeak to magically convert this into write array(a, 0, n)? I am not as certain about this one as I am about the reading "magic". What would be the complications involved with distinguishing between := as it is used as a normal set variable operator, and := as used to assign to an array. Would looking for ]:= be sufficent to handle all cases?

array size(a)

would return the number of elements (5)

ubound(a)

would return the index of the last element (4)

variable(i)
for(i,0,ubound(a)) do, begin
  show value(a[i])
end

The above would be a way of looping through a variable.

variable(i)
for each(i, a) do, begin
  show value(i)
end

The above would be a nifty way of doing loops, but I don't have a clear idea in my head yet of how it would be best implemented.

delete element(a, 3)

would delete the 3 index from the array, moving down all the rest of the values, and decreasing the size of the array by 1

insert element(a, 3, n)

would insert an element at the 3 index, moving up all the rest of the values, and increasing the size of the array by 1

append element(a, n)

would add an element to the end of the array, increasing its size by 1

prepend element(a, n)

would just be another way of writing insert element(a, 0, n)


In game, arrays would be allocated whenever create array was called. The array handle would just be a number, which would increment with each new array. It would not re-use old handle numbers that had been freed by delete array since that could easily lead to strange errors. Attempts to use an out-of-bounds array index would produce an error message. Attempts to use an invalid array handle would produce an error message.