[SOLVED]Help understanding arrays.

Get help using Construct 2

Post » Sun Jun 28, 2015 8:34 pm

Hello World,

So I have to use an array for my score values and this is how I plan to set it up:
50 levels=50 array width
3 difficulty levels=3 array height
3 global variables: "Difficulty", "LevelNumber", "Score"
Event: on end of layout Set value at(LevelNumber, Difficulty) to Score

So what I have achieved there(unless I am mistaken) is that every time the layout ends the game checks to see what level you are on, what difficulty you have chosen, and then saves the current score in the appropiate place(level&difficulty).

I have 2 problems so far:
1. Is there any way to make it so that the score will be saved only if it's value is higher then what is already in that slot in the array?
2. I am confused by the following quote from the manual:
"Note like the rest of Construct 2, indices are zero-based, so the first element is at 0. In this example, Array.At(0, 0) would return the first number in the grid."
What exactly does that mean? If I put 3 for my array height, does that mean I have 4 columns, one for 0, one for 1, one for 2 and one for 3? And where exactly do I store my values... cause I would like level 1 to be stored at x 1 in the array. So if you play level 1 on easy difficulty(lowest possible) then the value would be store at x:1 and y:1 in the array; would all 50 levels fit then if I don't use the 0,0 thing?
Sigh, I am totally confused by this part, guess that's obvious.....

Please help, thank you.
Last edited by VIKINGS on Thu Aug 06, 2015 12:50 pm, edited 1 time in total.
B
21
S
11
G
6
Posts: 414
Reputation: 5,335

Post » Sun Jun 28, 2015 8:46 pm

1. Check the value at (LevelNumber, Difficulty) -- using a comparison -- before setting the new score to that location.

2. Zero indexing has nothing to do with size. An array of width 3 would have three columns, at indices 0, 1, and 2. If your level and difficulty numbers start at 1 -- that is, they're one-indexed -- then a score's corresponding location in the array would be (LevelNumber - 1, Difficulty - 1).
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,116

Post » Sun Jun 28, 2015 9:02 pm

1. K, I figured out how to check it, I was missing it cause I was looking at actions, had to add a subevent first. Thank you @linkman2004

2. Well that's what I am confused about, if I start both of them at 1(and keep going) will all 50 levels fit in an array with width=50? Do I have to start at 0,0 for them to fit or make the array width=51/height=4 if I want to start them both at 1?
B
21
S
11
G
6
Posts: 414
Reputation: 5,335

Post » Sun Jun 28, 2015 10:29 pm

If you want to start indexing at 1, you have to add one to the width, as by default every array starts indexing at 0. As long as you just add one to the width, everything will be fine. You will just be skipping index 0.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Sun Jun 28, 2015 10:36 pm

I normally ignore row zero in my levels array

My 100 level array is 100,5,1

Basically just X,Y no Z

key - val1, val2, val3, val4, val5

You could do 50,6,1

Key - lvl1, score1, lvl2, score2, lvl3, score3
OR
Key - lvl1, lvl2, LVL4, score1, score2 score3

++++++++++++++++++++++++++++++++++++

Example
Lookup scores of level 25

Array Layout = Key - lvl1, score1, lvl2, score2, lvl3, score3

Key 25 - Level 1 and Score = 0,0 and 0,1
Key 25 - Level 2 and Score = 0,2 and 0,3
Key 25 - Level 3 and Score = 0,4 and 0,5

However, I would store the score separate from the Level array
Lookup Level array and then lookup Scores array to get values
If you put scores in the same array as the levels and then update the game (adding more levels) people could lose their progress
B
9
S
3
Posts: 146
Reputation: 1,150

Post » Sun Jun 28, 2015 11:10 pm

Ok @blackhornet one more time just to be clear, so I add one just to the width, or to height as well?
Lool sorry @frozenpeas but what you wrote there didn't really help with my confusion.;)
B
21
S
11
G
6
Posts: 414
Reputation: 5,335

Post » Mon Jun 29, 2015 12:40 am

If you have a two dimension array, then you add one to each dimension, if you prefer starting at 1. So yes, add one to both. Then your first index is [1,1], last is [50,3].

It's better in the long run if you get used to dealing with indexing from 0 - you just subtract one when dealing with "Level 1", but if you don't want to deal with it, just add one when setting your bounds.
Last edited by blackhornet on Mon Jun 29, 2015 6:05 pm, edited 1 time in total.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Mon Jun 29, 2015 9:19 am

Ok, cool. Thank you very much guys.
B
21
S
11
G
6
Posts: 414
Reputation: 5,335


Return to How do I....?

Who is online

Users browsing this forum: Microbex, PROxMTswatTR and 12 guests