Procedural generation number of arrays slowing down system

Get help using Construct 2

Post » Sat Oct 21, 2017 3:12 pm

Maybe I'm going about this the wrong way with Construct 2. What I'm trying to do is procedurally generate a world. Starting with an imported heightmap and using the canvas plugin I pull the needed color data into an array the same size as the image. Using that I then go through and generate a second array for each cell in the first array and that's where the system starts to slow down. Testing this process a 100x100 image uses an array with a total size of 10,000 cells which means I have to generate 10,000 smaller arrays which works fine if those arrays are below 25*25 cells. Anything above that and the system eventually grinds to a halt. If I am not mistaken, it's the number of items in total which are slowing down the system.

Anybody have any tips or ideas on how to handle generating larger worlds with construct 2 in this fashion? Am I going about this in the wrong way?
B
5
Posts: 13
Reputation: 255

Post » Sat Oct 21, 2017 3:23 pm

Create a plugin to generate the data. Don't use events to do it. Using a plugin will allow you to run some javascript which will run faster than if you do it through events.
B
49
S
23
G
66
Posts: 1,151
Reputation: 39,103

Post » Sat Oct 21, 2017 4:09 pm

Wouldn't the plugin end up generating the same amount of arrays, thus ending up in the same situation just quicker? To be honest plugin creation is not something I have really looked into before and my JS abilities are akin to writing with my left hand so I may be interpreting this wrong.
B
5
Posts: 13
Reputation: 255

Post » Sat Oct 21, 2017 4:28 pm

The thing about loops is that they have to finish before the next event can happen.
That is why it gets bogged down.
Image ImageImage
B
173
S
50
G
194
Posts: 8,570
Reputation: 121,376

Post » Sat Oct 21, 2017 4:37 pm

Why do you need a 25x25 array for each cell of the first array? That's 625 values for a single pixel of your heightmap.

And do you really need to keep all this data in memory at once? Can't you process one pixel at a time and re-use the same 25x25 array?
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
17
S
9
G
127
Posts: 1,765
Reputation: 67,851

Post » Sat Oct 21, 2017 4:55 pm

Newt- As far as I can tell it isn't getting bogged down in the loop. I have it set so it iterates in portions, rather than all at once so I can provide a visual feedback to the progress instead of waiting and wondering if it's working. That part is working fine. The issue is that it starts off quickly, and the begins to get slower and slower every loop as it proceeds through.

dop2000 - 25x25 is just where it starts to faulter. I'm really trying to aim for 100x100. Think of each pixel of the heightmap as a chunk of the map. The idea is to take every chunk and expand it into a second array so that chunk is in reality 100x100 tiles. I do realize this is an absurd amount of data. As far as reusing the array I could, but wouldn't that wind up in losing the processed data. I suppose I could dump it to json after ever single cell expansion of the main map. Would be a lot of files being created however.
B
5
Posts: 13
Reputation: 255

Post » Sat Oct 21, 2017 6:21 pm

It doesn't sound like an absurd amount of data. As you sure reading each pixel of the 100x100 image isn't the issue? That's a slow action.
Javascript can be a much faster at running but you lose the ease of interacting with the rest of C2. I wouldn't recommend it. Using just javascript with some other game library and not use c2 at all would be simpler i think.

Anyways, if doing everything all at once locks up the browser you could redo your logic to do the generation over multiple ticks instead of all at once.

so instead of this:

start of layout
array: for each xy
--- do something

you could do something like this:

global number y=0

y<array.height
--- for "x" from 0 to array.width-1
------ do something
--- add 1 to y
B
95
S
34
G
128
Posts: 5,493
Reputation: 81,674

Post » Sat Oct 21, 2017 7:13 pm

Yup, that's what I've currently go it doing so I can provide visual feedback on where it is in the process. The problem is accrued slowdown (I think due to the number of arrays being generated) It will generate the first three or four thousand (the first 30-40 lines of the image running from y 0 to image height) after that it gets progressively slower until it just stops around 47.
B
5
Posts: 13
Reputation: 255

Post » Sat Oct 21, 2017 7:19 pm

Upon further poking around it isn't the amount of arrays but the size as it's eating up tons of ram (which is to be expected with them sitting around in memory). I suppose the only way to get around this would be to save the array to JSON after it's generated. EDIT: After experimenting with saving the file through the JSON format there's the issue of it prompting every file save. Don't think anyone want's to click 10000 times.
B
5
Posts: 13
Reputation: 255

Post » Sat Oct 21, 2017 7:42 pm

Sounds like a logic issue. It shouldn't get progressively slower and use more and more ram. It should stay constant.

100x100x25x25 is nothing.
B
95
S
34
G
128
Posts: 5,493
Reputation: 81,674

Next

Return to How do I....?

Who is online

Users browsing this forum: AbusiveChAoS and 10 guests