PDA

View Full Version : Arrays!



Saffith
11-01-2006, 04:27 PM
I'm sure it's something that's been considered already, but we could really do with arrays. I realize variable-size arrays are probably pretty difficult, considering how variables work, but even fixed-size arrays would be quite helpful.

There are two specific situations I've already encountered in which they'd be useful:
- Setting the amount of damage done to an FFC-based enemy by different levels of weapons (especially helpful since we may not know in advance how many levels there are)
- Defining songs and recording notes for a playable ocarina

Fixed-size arrays shouldn't be hard to implement, I think. Just like a series of distinct variables. As far as access, array[i] could, at worst, be handled internally the same way as

if(i==0)
array[0];
else if(i==1)
array[1];
// etc

C-Dawg
11-01-2006, 04:33 PM
I offer my infintely less wise concurrance to this request.

I've run into at least two situations where fixed-size arrays would be very handy.

(1) Coding a "snake" like entity. The script would calculate the movement of the snake, but instead of applying it directly to the FFCs making up the snake's body, it puts the resulting data into a queue. Each tic, the next movement data for the snake moves into the queue and pushes the other data over one. Last, each segment of the snake takes it's position / velocity from a different element in the queue. The result is that the body of the snake follows the head around the room.

(2) Saving Link's inventory. I'd like to be able to mirror link's inventory in an array, so in addition to viewing items on the subscreen, there would be a second, invisible array keeping track of what Link has picked up. This would let you switch items on and off at the user's whim. In particular, this would allow multiple playable characters, each who can use only certain items, and let the user switch between those characters as necessary.

DarkDragon
11-02-2006, 08:10 AM
I'd thought about arrays when I was first designing ZScript and postponed working on them because they are complicated. Very rudimentary, static, fixed size arrays, such as

ffc script foo {void run() {
int anarray[3];
anarray[someMethodReturningInt()]=42;
}
}
would not be terribly difficult to implement.

One thing to keep in mind is that the arrays will have to be stack-allocated, and the stack is currently capped at 256 elements. The stack is used as follows:
-Each local variable in the current method and all methods in the call hierarchy takes one slot.
-Each parameter of the current method and all methods in the call hierarchy takes one slot.
-For each method in the call hierarchy, two additional slots are taken up - one for the return value, and one for the old value of the stack frame pointer. This adds up when recursively calling functions.
-Evaluation of expressions requires up to two temporary slots for storing intermediate values.

Thus to ensure the script doesn't crash due to stack overflow the total space reserved for arrays will have to be kept small. Alternatively, we could add a free store common to all scripts and talk about dynamic allocation or even garbage collection :(

itsyfox
01-16-2007, 10:46 PM
There is *sometimes* a way, depending on how strict a language is with both global/function-local variables/arrays, and also how strict with declaration.

It would be easier if you could redefine an array after you initially declare it.

But I'm pretty sure if you do, for instance,

int myvar = 2;

int myvar = 34;

It yells at you. I could be wrong. Maybe it only yells when you try to change types.

Anyways. If you can't redeclare an already existing array, if you can UNDECLARE it then you can use a temporary array to store all it's values, undeclare it, declare it at the new length, add the values back in, and any extra values you were wanting to add in. You could remove values by a similar process, only this time you'd be copying a few *less* values from the temp array back to the new, smaller array.

You'd of course want to undeclare the temp array. But you could potentially make array_add and array_remove sort of functions this way.

With a little more work, even array_sort and beyond...

Anyways. Just something I've done to dynamicize my arrays in languages that didn't come with dynamic arrays, and I thought I'd share. Arrays at all would be YES, most DEFINITELY AWESOME. ^_^