# How do I cycle through my array/dictionary using a button?

0 favourites
• 11 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
• I'm trying to work out how I display text from the 'next' cell down or across in the array when the player clicks on the 'next item' button. The array is basically storing 'available items' in a player shop, and when the player clicks on 'next item', the text above should list the next item.

• Use array.At(X,Y,Z)

If you have the names stored on x just do Array.At(variable) and every time the button is clicked, increase variable by 1. At the end just make sure that the variable loops back to 0 or stops increasing once you reach the end of the array.

variable > array.width - 1

Set variable to 0

• Here you have an easy way to achieve this:

https://www.dropbox.com/s/riibqex9fb8wt ... .capx?dl=0

• Here you have an easy way to achieve this:

https://www.dropbox.com/s/riibqex9fb8wt ... .capx?dl=0

Thanks,

Would you mind explaining this code please?

(itemNR+1)<=2 ? itemNR+1 : 0

I know itemNR is the global variable, but I don't understand the sequence or other numbers/symbols here.

Thank you for your help - it will assist me greatly!

• itemNR tells you which item is currently selected. "Set item to: (itemNR+1)<=2 ? itemNR+1 : 0" lets you go through your list. It goes like this: 0 (remember that it's the first index in the array), 1, 2... and in my example you don't have 3rd array index, so you have to return to 0 index. So the exact performance is:

• on touched Sprite the condition is checked: (2+1)<=2; it's not true, so Construct sets itemNR value to 0.

Take a look at Operators part here:

https://www.scirra.com/tutorials/77/nat ... onstruct-2

• itemNR tells you which item is currently selected. "Set item to: (itemNR+1)<=2 ? itemNR+1 : 0" lets you go through your list. It goes like this: 0 (remember that it's the first index in the array), 1, 2... and in my example you don't have 3rd array index, so you have to return to 0 index. So the exact performance is:

- on touched Sprite the condition is checked: (2+1)<=2; it's not true, so Construct sets itemNR value to 0.

Take a look at Operators part here:

https://www.scirra.com/tutorials/77/nat ... onstruct-2

Ok thank you so much! I'll take a good look at this.

• Use array.At(X,Y,Z)

If you have the names stored on x just do Array.At(variable) and every time the button is clicked, increase variable by 1. At the end just make sure that the variable loops back to 0 or stops increasing once you reach the end of the array.

variable > array.width - 1

Set variable to 0

Hi,

Would you mind explaining this please:

variable > array.width - 1

Set variable to 0

• itemNR tells you which item is currently selected. "Set item to: (itemNR+1)<=2 ? itemNR+1 : 0" lets you go through your list. It goes like this: 0 (remember that it's the first index in the array), 1, 2... and in my example you don't have 3rd array index, so you have to return to 0 index. So the exact performance is:

- on touched Sprite the condition is checked: (2+1)<=2; it's not true, so Construct sets itemNR value to 0.

Take a look at Operators part here:

https://www.scirra.com/tutorials/77/nat ... onstruct-2

Thanks I understand it now <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

Do you know how I can use the array.At expression to point the return value to a specific line on the array, say the second or third row?

Also, how do I cycle through the Y axis instead of the X? Some of my buttons will cycle downwards through the array instead of across.

I'm trying to work it out myself but I keep getting incorrect expression etc....

• Arrays are not the easiest thing to work with, but once you understand how they work they can get really useful.

[quote:2469aufe]Would you mind explaining this please:

variable > array.width - 1

Set variable to 0

So the variable is used to define where you are currently at inside the array. However, arrays use zero-based indexes, aka. the first value is stored on 0, not on 1. What the check does (assume the array has a width of 10):

``````IF the variable is bigger than the array.width - 1 --> set variable to 0[/code:2469aufe]
We need the -1 on the array width, else we would allow the variable to increase up to 10, and array.At(10) would return nothing, since the array has stored the values on 0-9. What this ends up doing is that it will loop back to the first item once you reach the last item.

[quote:2469aufe]Do you know how I can use the array.At expression to point the return value to a specific line on the array, say the second or third row?

array.At(X,Y,Z) --> array.At(0,2) will return the value on the 3rd row.
X = columns
Y = rows
Z = depth (3d arrays)``````
• Do you know how I can use the array.At expression to point the return value to a specific line on the array, say the second or third row?

1 dimensional array: Array.At(1) - returns value at element of index 1

2 dimentional array: Array.At(1,3) - return value at element of index 1 on the X-axis and index 3 on the Y-axis

3 dimentional array: Array.At(1,3,5) - return value at element of index 1 on the X-axis and index 3 on the Y-axis and index 5 on the Z-axis

I highly recommend you reading manual (https://www.construct.net/pl/make-games ... onstruct-3) when you don't get something. You can find there many really useful information. At the beginning you probably should read these two parts:

https://www.construct.net/pl/make-games ... ves/events

https://www.construct.net/pl/make-games ... nd-guides/

It would allow you to get better understanding how evertyhing works in Construct and prevent you from making mistakes that results from many misconceptions <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

Also, how do I cycle through the Y axis instead of the X? Some of my buttons will cycle downwards through the array instead of across.

You make the same things as in my capx but the only difference is that you change Y index instead of X index. So it would be "Set text to Array.At(0,itemNR)" if you want to cycle through elements of index 0 on X-axis.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• > Do you know how I can use the array.At expression to point the return value to a specific line on the array, say the second or third row?

>

1 dimensional array: Array.At(1) - returns value at element of index 1

2 dimentional array: Array.At(1,3) - return value at element of index 1 on the X-axis and index 3 on the Y-axis

3 dimentional array: Array.At(1,3,5) - return value at element of index 1 on the X-axis and index 3 on the Y-axis and index 5 on the Z-axis

I highly recommend you reading manual (https://www.construct.net/pl/make-games ... onstruct-3) when you don't get something. You can find there many really useful information. At the beginning you probably should read these two parts:

https://www.construct.net/pl/make-games ... ves/events

https://www.construct.net/pl/make-games ... nd-guides/

It would allow you to get better understanding how evertyhing works in Construct and prevent you from making mistakes that results from many misconceptions <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink" />

> Also, how do I cycle through the Y axis instead of the X? Some of my buttons will cycle downwards through the array instead of across.

>

>

You make the same things as in my capx but the only difference is that you change Y index instead of X index. So it would be "Set text to Array.At(0,itemNR)" if you want to cycle through elements of index 0 on X-axis.

Wow it's working now!!

• 11 posts