[SOLVED]Help understanding arrays.

0 favourites
  • 8 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • 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.

  • 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).

  • 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?

  • 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.

  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • Ok, cool. Thank you very much guys.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)