How do I loop through an array?

0 favourites
From the Asset Store
SynthWave Loop Pack
$4 USD
60% off
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies. For use in game and custom works.
  • I have created a set of array values as follows:




    I want to loop through each values "2,1,3) on the click of a button so each values is used in a global variable.

    On button click

    For unknown reason, this only picks the first value which is 2 and ignores the rest 1,3.

  • Move the values to 1,0 2,0 and 3,0. You're running through each X but only use X=0

  • I did try that at first but in debug mode, the array shows you this:

    Data: 2,0,0

    instead of 2,1,3

  • You didn't say how you are adding data to the array, share those events

  • I am adding the values like this for a test.

  • And let's see a screenshot of the array in debug view

  • Array in debug mode

    I just replaced the values 2,1,3 with 2,4,6

  • The array looks correct so the problem is whatever you're doing with that function and you have a wait before it that's never good because the loop can finish before you run the function the first time.

  • Ah I see what you mean. So how do I tell the loop to wait for the first loop to finish before the second etc?

  • Does it need to wait? What are you doing with the function because I thought you are just setting array value to a global variable.

  • The function is to play a sound.

    I have a function that has 3 sub-events

    Each sub event is checking for the value of the global variable called buttonclicked. So if buttonclicked = the first array value 2, then it plays a sound I need it to play.

    So I expect it to play sound 2, followed by sound 3 etc.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could probs do it all in one function, check for value at X of the array which is a global variable counter starting at 0, lets call it arrcount. In the function set a local variable to, then run your sound checks. Then wait as long as you want, add 1 to arrcount and call the function again.

  • Oh and add a while arrcount is less than something so it doesn't keep calling the function.

  • I have tried your suggestion but stuck with one issue. I created 3 functions each to play different sounds.

    Func_1 - Play sound 1

    Func_2 - Play sound 2

    Func_3 - Play sound 3

    On button click AND system for Array.At(arrcount) from 1 to 5 - xxxx

    I am stuck here on how to call the correct func using this "Func_"&"Array.CurValue"

  • No need to use functions, just conditions within that one function, if local var is 1 then play sound 1 etc

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