Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Arrays...
Arrays...
2002-12-28, 4:36 PM #1
I don't understand arrays [http://forums.massassi.net/html/frown.gif]. How do arrays work? DataMaster didn't help.

------------------
LEET OWNAGE
2002-12-29, 5:57 PM #2
ummmmm...
help = good


------------------
LEET OWNAGE
2002-12-29, 6:28 PM #3
Hmm.


In a nutshell, arrays a an easy way to cut down the size of cogs, but refferencing multiple variables in a single statement.

Example:

Code:
Symbols
Message Activated
Surface Switch

Int Var0
Int Var1
Int Var2
Int Var3
Int Var4

Int I=0   Local

End
Code

Activated:

For(I=0;I<=4;I=I+1) Printint(Var0);

Stop;

End




That cog, upon activation of the switch, will print out all the values of the "Var" series of ints to the console. Without the array, each variable would have to be printed seperatly.

In that example, I used For(), but that isnt the only way it could be done.

Code:
Activated:

While(I <= 4) { 

Printint(Var0);

I=I+1; }

Stop;



That will accomplish the same thing, I just used a different loop type.

The For() command is used because it combines the array operator (the I variable) directly.


For(I=0; I <= 4; I=I+1)


I=0; This part is setting the inital value of "I". We could start it at, say, 2, so only the values 2 - 4 would be printed...


I <= 4; This is the condition. Every time the loop is run, it checkes to see if the condition is true. If it is, it will go through with the loop. If it is not, the loop will terminate.


I=I+1 This is the Index increment. Every time the For() loop reaches the end of the code block, and starts over, this calculation is performed. It is not performed the first time the loop runs, but every time after that. On the last run, the variable is set to 5, making the condition return false, thereby terminating the loop.


Thats the For() command, in a nutshell.


Arrays in JK arent really arrays as all (in the classic sense).

Code:
Startup:


Symbols
Message Startup

Flex Gravity_easy
Flex Gravity_med
Flex Gravity_hard

End
Code
Startup:

Setgravity(Gravity_easy[Getdifficulty()]);

Stop;
End



The above is a valid array.


The array is composed of the three Flex variables, hte 'Gravity' series. In the Setgravity() command, only one variable is being called. Which one, depends on the difficuly. If hte difficulty is on easy, a '0' is returned from the 'Getdifficulty()' command, thereby calling the 'Gravity_easy' variable. If the difficulty was on hard, a '2' would be returned, calling the 'Gravity_hard' variable. That is because arrays in JK are nothing more than symbol offsets.


Code:
Symbols
Message Startup

Int Var0
Int Var1
Int Var2
Int Var3
Int Var4

End
Code

Startup:

Printint(Var0[2]);

Stop;

End



Var2 would be printed to the console. That is because the command is referencing 'Var0', with an offset of 2, which lands on Var2.

Code:
Symbols
Message Startup

Int Var4
Int Var3
Int Var2
Int Var1
Int Var0

End
Code

Startup:

Printint(Var4[2]);

Stop;

End



Again, Var2 would be called. Contrary to popular belief, the above is a valid array.


In a nutshell, to use an array..


1) You can use an array for just about anything. you dont need to use For() or While().

2) The array format is "Variable[offset]" The square brackets indicate an array operation.

3) Go back and re-read the Datamaster now. It should make more sense.
And when the moment is right, I'm gonna fly a kite.
2002-12-29, 7:04 PM #4
thankyouthankyouthankyouthankyou very much

------------------
LEET OWNAGE

↑ Up to the top!