PDA

View Full Version : Custom Arrays... How do they work?



Schwa
04-13-2008, 05:45 PM
After manually searching this website and Google for over an hour, I decided I'd finally ask this question in a new thread. I'm very disappointed that this website does not have a working Search Forum feature. -_-

Anyway... Custom Arrays are one of the new features added to ZC, obviously. My question is, how do I set them up and get them to work? Somewhere I read about Arrays being either one-dimensional or two-dimensional, but I swear for the life of me I couldn't re-locate that thread...

See, what I'm trying to do is make a script that creates an enemy on the screen for you to fight, and when you kill it, it never comes back. The way I thought I'd do this is by setting up a Global Boolean Array indexed by the current Screen and Map number, so if the Bool for the indexed screen is False, the enemy is spawned, and when you kill that enemy it sets it to True, making the enemy never come back.

I know mostly what code to write, but... Custom Arrays aren't mentioned on the ZC Wiki, so I have no idea what the code I'm supposed to write needs to look like. Could anyone help me out? (Also this info NEEDS to be written on the ZC Wiki so others like me can actually script stuff like this...)

Gleeok
04-13-2008, 06:59 PM
That was me that posted the thread about multi-dimnesional arrays. It's only a few away from this one. As far as I know they are indexed as:

int array[number];

I don't think you can bool arrays, however 0 and 1 is basically the same thing. I also haven't actually made one yet.



...Alright guys, first person to make a working array using zscript has to post it here!

Schwa
04-13-2008, 07:03 PM
There's another thing. The description in the Shardstorm Update list said something about the Declare() command, which was not in zscript.txt at ALL... It said something about using Declare() to tell the game to allow custom arrays, I think. But I'm probably wrong. >_<

Well, I guess I'll experiment around a little, but I'm scared of what will happen...

Gleeok
04-13-2008, 07:14 PM
* And finally, the additions of arrays to the scripting engine! Took a lot of work, but they're in. Now, each running script has the option via the global Declare() function to allocate for itself a block of memory. This block of memory is used as a scratch space for declaring and using arrays. One block is continually persistent as a 'global' memory and is used by default when an ffc or item script doesn't declare a piece of memory. Also included is a new section of memory in save files that allows you to store whatever miscellaneous data you like. ( jman2050, 2008-03-04 11:27:42 )


...Nuts...you're right. I'll mess with it and see what I can do to make sense of this nonsense. ;)



EDIT; Alright, I've made an array!!!


int poop[9];
ffc script test{
void run(){
for(int i; i<240;i++){
poop[1]=i; Trace(poop[1]);poop[2]=i+i; Trace(poop[2]);
}}}

And there you have it.

Schwa
04-13-2008, 08:27 PM
I'm trying to figure out what this does without running it...

From the looks of it, it prints each number from 0 to 240 to Allegro at once, and at the same time, that number x 2. Is that correct?

Thanks for showing me how to do this, but I have one question. What is the [9] for when declaring the Array? Is that the maximum value of the Array?

Edit: Hopefully you know what I mean by max value... Not the number of the variable. The number in the brackets. I wish I could explain it in actual programming lingo, to sound kewl and stuff, but I'm extremely new at this stuff... ^,^

Gleeok
04-13-2008, 08:44 PM
No. poop[9] just sounded funnier to me than poop[2]. :)

Schwa
04-13-2008, 08:47 PM
What I mean is, if you declare the array as poop[9] and then try to set a value to poop[12] for example, would it be an invalid script?

Gleeok
04-13-2008, 08:52 PM
Why not just set it to poop[12] to begin with?


Also I really don't get what what Declare() is for. It doesn't want to compile at all.

ScaryBinary
04-13-2008, 08:53 PM
In the C programming language, the number in the brackets specifies the number of elements in the array, so you could store 9 things in the poop[9] array. Also note that in C, the array elements start at 0, so even though the array is declared to have 9 elements, the elements are 0, 1, ...7, and finally 8 (in other words for any array "foo[N]", the first thing in the array is at "foo[0]" and the last thing in the array will be at "foo[N-1]").

Since ZScript is based on C, I'm guessing these rules apply to ZScript, but you could write a quick script and see what the compiler does if you try to do something like

int poop[9];
poop[42] = 1;

Schwa
04-13-2008, 09:00 PM
Okay, so my hunch was right. Awesome.

Here's one more question... Two-dimensional arrays. I remember reading something jman wrote that said ZScript supported 2D arrays, but I can't seem to find that post ANYwhere, so it may have been my imagination. My question is, if this is the case, how are they set up? I tried "array[#][#]", and I also tried "array[#, #]", neither of which worked.

ScaryBinary
04-13-2008, 09:08 PM
...a 2D array should be declared like

int poop[5][10];
(at least in C)...which you already tried....

Also, in reference to your first post, it seems like you should be able to have an array of bools (versus integers), but maybe the "maiden release" of arrays in ZScript has some limitations yet. I haven't downloaded that release yet to try it out.

*EDIT* Hmmm....just looked at the changelog and it had this comment in it (for build 776):

Array declarations now accept strings as arguments. More work will be done on the functionality of this, including the ability to display custom messages via scripting. Also on a related note, single-quote characters are accepted in ZScript as well, and are treated as constants based on the characters ASCII value.
...I'm not entirely sure what that actually means...

Schwa
04-13-2008, 09:43 PM
Well, I have some great news: Thanks to you guys's help, I managed to create an awesome script using an Array... It creates an enemy on the screen, and gauges its HP in the form of a rectangle bar. When you kill the enemy, it never comes back... ever. :D This is the first script I ever wrote on my own, and if it weren't for the Array, the bosses would come back every time you came back to the screen.

But it's not done yet, so I'd like to ask your help one more time, then I can post this in the Script Showcase forum... This question is unrelated to arrays though, but I figured I could just throw this question in this thread rather than start a new one for something so simple.

I need a way to set all the HP of each enemy on the screen to 0 at the same time. The method I tried last time didn't work, and now I know why... Enemy UIDs aren't sequential, and my script was treating them as if they were. The reason I need to figure out how to do this is so I can get the script to kill all other enemies on the screen when the "boss" dies-- the Ring Leader flag won't cut it, since the boss enemy is spawned by a script rather than placed in the room via the List, so it doesn't count as the Ring Leader for the room.

One way I'm thinking of is to set up a While Loop that starts a variable at 0, compares it to the Enemy IDs in the room, sets any matching enemy's HP to 0 unless it already is or it returns "undefined", then the variable increases by 1 and does it again... and the loop continues until there are no longer any enemies on the screen with HP above 0. I'm gonna give that a try and keep you updated...

Edit: Looks like I suck. *turns to you guys*

Snarwin
04-30-2008, 04:47 PM
Doesn't this work?

for(i=1; i<=Screen->NumNPCs(); i++)
{
Screen->LoadNPC(i)->HP = 0;
}

Waitframe();

Joe123
04-30-2008, 05:46 PM
No, it doesn't.