Problem resetting an array when restarting layout

Discussion and feedback on Construct 2

Post » Thu Jun 26, 2014 6:23 am

Message: DrDeleto can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
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.

https://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?
B
9
S
2
Posts: 13
Reputation: 799

Post » Thu Jun 26, 2014 6:55 am

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.
Every Game is a Good Game it's just few people like and few does not! Keep coding people some day you will be a "NOTCH".
B
21
S
6
G
3
Posts: 85
Reputation: 4,607

Post » Thu Jun 26, 2014 7:11 am

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

ArrayClear.capx


Edit: sounds similar to the above suggestion.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Fri Jun 27, 2014 4:30 am

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.)
B
9
S
2
Posts: 13
Reputation: 799

Post » Fri Jun 27, 2014 7:33 am

Yeah breakpoints are not that useful... I live by logging. I'll try to have another look at this if I get time.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sat Jun 28, 2014 6:08 am

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.
B
9
S
2
Posts: 13
Reputation: 799

Post » Sun Jun 29, 2014 12:44 am

Message: DrDeleto can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Update: So I solved my problem by using the RandomArray plugin created by a community member. (viewtopic.php?t=63557&start=0) 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.
B
9
S
2
Posts: 13
Reputation: 799

Post » Fri Mar 31, 2017 5:53 pm

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.
B
5
Posts: 5
Reputation: 235


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests