Problem resetting an array when restarting layout

0 favourites
  • 8 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Okay, bear with me: my situation is a little complex and I don't know which piece is breaking it.

    I am attempting to randomize the items in an array after it is populated via AJAX from a JSON file. To do that, I have created a separate array that I populate with random integers, ensuring no repeats. Then I put those values into place in the first column of the array I want to randomize. This has all been working well for me (although I'm open to suggestions on how to do it better!).

    Where the problem comes in is when I want to reset the layout. I can't get it to re-randomize. The randomizing array keeps all its values (and I randomize by continuing to fill it until there are no more 0s left) so the array I'm shuffling gets an identical shuffle to the last time.

    Seemed like the easy solution was to clear the randomizing array so it is full of zeroes again. But for some reason, this hangs the browser. I'm not sure if my logic is bad and I'm missing some error I've made, if the AJAX load (or something else) doesn't work like I expect with the layout reset, or if it's a bug.

    drive.google.com/file/d/0B7Gu37yDX14xTmFaWXNkODNRY1E Here is a .capx that extracts the relevant pieces from my current project. If you enable the disabled line at the end, you'll see the hang.

    Ideas?

  • If you integrating values from ajax probably you might be doing it on the start of the layout and when you reset them the condition to take values from ajax become true. you might put another condition like if global variable is equal to 1 then load from ajax and once loaded change the global value so that it won't repeat the step and the randomization keeps on.

  • I added a global variable so the load is only done once.

    [attachment=0:ljhsmx9x][/attachment:ljhsmx9x]

    Edit: sounds similar to the above suggestion.

  • Thanks for your help, guys. I tried the global variable approach, but I still get a hang when I try to run through the randomization part. codah, your capx doesn't hang, but it doesn't randomize when the layout reloads either. One tricky part is that I need to trigger the randomization to happen only once after the level starts (whether it's the first time or after the reset), but the first time that trigger is the AJAX finishing the load and subsequent times it needs to be... something else. Just tried it with a function call: the AJAX load calls the function and sets the global variable true, and the On Start will call the function itself if the global variable is true. I think that solves the triggering problem... but then it hangs again.

    Frankly, I don't think the AJAX load is the issue, although I could be wrong. It's something with the Randomizer Array. I know that While loop is prone to cause hangs if it's not set up right. But I can't figure out how I would get to that loop and not then have it carry on and do the randomization (which should eventually kill the While loop when there are no more empty cells).

    Would love to hear any other thoughts. Thanks for the help so far. (And thanks for the log trick, codah. That helps in this case where there are so many triggers and I can't use breakpoints.)

  • Yeah breakpoints are not that useful... I live by logging. I'll try to have another look at this if I get time.

  • Can a dev investigate this? The more I mess with it, the more I think it is a bug or at least a strange timing problem. Clearing the RandomArray should make it all ready to get populated again. But instead it hangs. That Clear command is definitely the culprit, or at least the key event that breaks something on reload that is fine the first time.

  • Update: So I solved my problem by using the RandomArray plugin created by a community member. () Thanks Joe7! The 'scramble' action lets me get around the Clear that breaks my old solution. Still might be worth the time of someone knowledgeable to see if what was happening is intended behavior for Construct.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The RandomArray plugin may solve the issue, but depending on your implementation, it may not be necessary....

    I've accomplished a solution without RandomArray and I think its straightforward and clean. Rather than fill the temp array used in the randomization process, you simply create a new one and fill it. After the randomization process, it's virtually nothing (0 length), so you destroy it. But, remember, this is just a working copy, so the original one is still there to create from.

    Another solution may be, to work with an empty array instead of presetting the arrays size. Then, instead of initializing values at different positions, you'd simply push the values into it. If this solution works, you don't ever have to recreate the initial temp copy of the array, but simply re-push initial values into it for the randomization process.

    Initially, I did see the array was still there, but of 0 length, but I failed to make the connect to the push method, because my brain was stuck on setting the values by index. So, think push, instead of set.

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