How do i use arrays?

Get help using Construct 2

Post » Tue May 22, 2012 12:08 am

I have a vague understanding of arrays. Could someone help me understand how they work. I feel i'm missing out on something big. Could you also give me a practical situation where arrays could be used. thx.

The only thing i know is it can hold numbers and strings. But how do i access them and or shuffle the numbers in an array?
B
22
S
5
G
3
Posts: 194
Reputation: 5,855

Post » Tue May 22, 2012 1:18 am

Think of the array as a huge grid, which holds values in each of its grid squares.
You can access the value in a specific grid square by using array.at(x,y,z)
Shuffling all the elements in the array is a bit complex, but I think there is an example of it somewhere...
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue May 22, 2012 3:10 am

Oh i see. Can an object have a list of arrays? Like if i was going to make a card game i would create an array starting from 0 to 21 right? Could i assign array 11 to the card Jack. Or 20 for A?

Why would i want to hold values in an array?
B
22
S
5
G
3
Posts: 194
Reputation: 5,855

Post » Tue May 22, 2012 3:12 am

i took a class where we learned how to push and pop arrays but apparently my mind went blank. I never really learned how to use it correctly.
B
22
S
5
G
3
Posts: 194
Reputation: 5,855

Post » Tue May 22, 2012 5:51 am

How about a Tic Tac Toe game where you want to keep track of which square contains what symbol? One option is to have a variable for each square. You could have variables Top-Left, Top-Middle, Top-Right, Middle-Left, Middle-Middle, Middle-Right, Bottom-Left, Bottom-Middle, Bottom-Right. And then if the 'x' player clicks on the symbol called 'Top-Left' I could have an event that sets the variable Top-Left to 'x'. And then if the 'o' player clicks on the symbol called 'Top-Right', I could have an event to set the variable Top-Right to 'o'. Oh, and now I have to test to see if anyone got three in a row. So what are my possibilities?

If Top-Left, Top-Middle, Top-Right are all 'o',
If Top-Left, Top-Middle, Top-Right are all 'x',
etc.

What happens now if I want to make a simple change in logic, or even worse, make a 10x10 tic tac toe game. So many more possibilites, the events would be endless!

OR I can just use an array that is 10x10 and use math to calculate where the player places an 'o' or an 'x'. Let's say the player 'x' clicks position 3,5. And then all I have to say is set Array(3,5) to 'x'.

That's one possible use of it, out of INFINITELY many! The more game development you do, the more uses you'll see of structures such as arrays. Just wait... In fact, the whole JavaScript game engine that Construct runs on is guaranteed to be full of arrays and loops through those arrays, because it has to keep track of several objects and set common properties on all of them. We are lucky that Construct handles all that stuff for us, so that we only have to use arrays for more specific applications.Dalal2012-05-22 05:56:37
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Tue May 22, 2012 1:13 pm

Does the Array manual entry help at all?
Scirra Founder
B
359
S
214
G
72
Posts: 22,951
Reputation: 178,578

Post » Tue May 22, 2012 1:30 pm

@Ashley , i know that manual will help but the problem its hard to know these stuffs for people that have no knowledge about codes...

here the topic i asked to explanation:
http://www.scirra.com/forum/arrays_topic50950.html

but for real when i hear in forums about it , i feel its easy and easy to understand it , but when i try it i see its very complex ...many times i tried to make the level page but i really got confuse.
also i have tried to see tutorial in making levels page or any array example but i couldn't see a good one.
B
48
S
15
G
6
Posts: 795
Reputation: 12,035

Post » Tue May 22, 2012 3:10 pm

i agree with Zsagerous. Explaining arrays can be a bit challenging. Thx dalal for the tic tac toe analogy. I'm starting to get it.
B
22
S
5
G
3
Posts: 194
Reputation: 5,855

Post » Tue May 22, 2012 3:12 pm

i agree im looking at the
http://www.scirra.com/tutorials/40/basic-loops-and-arrays

and its still not clear
B
10
S
1
G
1
Posts: 74
Reputation: 1,637

Post » Tue May 22, 2012 6:31 pm

A good way to learn how to use array might be to start with one dimensional array.
A one 1D-array is just like a simple indexed list.

Array are usefull because they provide a unique name (the name of the array) for more than one value.
Classical variables (instance, global, local, etc) don't, it's always one name <-> one value.

Using one name for many values helps to lessen the number of variable.
Imagine a list of enemy. With variable you'd have to do stuff like:[code]global variable enemy1 = "wolf"
global variable enemy2 = "bear"
global variable enemy3 = "cactus" // why not?
etc[/code]
There's no advantage and many drawback to this method

I'd use Array like this[code]+On Start of layout
-> enemy set size to (3,1,1)
-> enemy.At(0) = "wolf"
-> enemy.At(1) = "bear"
-> enemy.At(2) = "cactus"[/code](note that array are 0-based)

This way you can do stuff like[code]+On Start of layout
-> Text: set Text to "There are "&enemy.width&" types of enemy in this area:"&newline
+for each X element
    -> Text append " -"&enemy.CurValue&newline[/code]Note that:
- using foreach X element is the same as doing:[code]+for "" from 0 to enemy.width-1
-> Text append " -"&enemy.At(loopindex)&newline[/code]- using enemy.CurValue is the same as using enemy.At(enemy.CurX)

Once you're used to work with 1D array, you can begin to use 2D arrays. For instance you could store the area index in X index and use the Y indexing for the list of enemies.
Like[code]+On Start of layout
-> enemy set size to (2,3,1)
-> enemy.At(0,0) = "wolf"
-> enemy.At(0,1) = "bear"
-> enemy.At(0,2) = "cactus"
-> enemy.At(1,0) = "pony"
-> enemy.At(1,1) = "jar jar binks"
-> enemy.At(1,2) = "your stepmother"[/code]
And you could go really far with this idea if you want to even store the name of the area and some characteristics.
There you can do it by organising your array such as the 2 first Y indexes are for these data and the rest is for the list of enemies
[code]+On Start of layout
-> enemy set size to (2,5,1)
-> enemy.At(0,0) = "The Shire" //name of the area
-> enemy.At(0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,2) = "wolf"
-> enemy.At(0,3) = "bear"
-> enemy.At(0,4) = "cactus"
-> enemy.At(1,0) = "DreamLand" //name of the area
-> enemy.At(1,1) = "sunny" // type of weather
-> enemy.At(1,2) = "pony"
-> enemy.At(1,3) = "jar jar binks"
-> enemy.At(1,4) = "your stepmother"[/code]
Or you could build a 3D array to split things up a bit more
[code]+On Start of layout
-> enemy set size to (2,2,3)
-> enemy.At(0,0,0) = "The Shire" //name of the area
-> enemy.At(0,0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,1,0) = "wolf"
-> enemy.At(0,1,1) = "bear"
-> enemy.At(0,1,2) = "cactus"
-> enemy.At(1,0,0) = "DreamLand" //name of the area
-> enemy.At(1,0,1) = "sunny" // type of weather
-> enemy.At(1,1,0) = "pony"
-> enemy.At(1,1,1) = "jar jar binks"
-> enemy.At(1,1,2) = "your stepmother"[/code](overkill isn't it?)
Anyway in the end the most important thing to consider when using array is how you'll organize it.

And also, I never set so much data by end. I always end up putting all that in a string and parsing it (via tokenat and tokencount)
But that's another story (:
Yann2012-05-22 18:34:31
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: Calfuso, ETgirl79, totoe, Yahoo [Bot] and 16 guests