Values in Array not set properly sometimes?

Discussion and feedback on Construct 2

Post » Sat Dec 14, 2013 1:04 pm

Hi! I am working on a game right now and spawn "baubles". I used an Array to check if a spawnpoint is already occupied to prevent overlapping of baubles. First it seems to work fine and the array saves the data where baubles spawned and the array entries are set back to 0 when an bauble despawns or gets destryed by a touch. But when it runs for a while (normal speed or with increased speed in debgger) some values in the array stay at 1 and are not re-set.

This is my very first time I used an array, maybe I made a mistake somewhere? Can an experienced user take a look at the following code to see if I did something wrong, please?

Spawning (checks array if spawnpoint is empty or get a new one if already occupied):

(Note: only problem I see right now is the while loop, if all array entries are 1 it creates an endless loop - still thinking about a solution but it "should" never happen because baubles despawn after a few seconds)

Destroyin (resetting array value, same for despawning when lifetime reaches zero):


After letting it run for a whil this is shown in the debugger:

4 slots are occupied (entries with 1) but only 1 bauble is spawned right now.

Thanks in advance!Bl4ckSh33p2013-12-14 13:11:03
B
53
S
16
G
6
Posts: 718
Reputation: 9,150

Post » Sat Dec 14, 2013 4:54 pm

I can't say for sure, but I suspect it may be a case where two (or more) baubles get touched or die at the exact same time.

You may have to move the line where you set the array value to zero to a "for each bauble" sub event.

The only other issue I see is the else clause where you pick a new spawnpoint (if the first one is occupied). The code doesn't actually do anything with the new spawnpoint because the code above it wont run again until after another random 4 seconds - and it will pick a new spawn point then anyway. So, basically, if the spawnpoint is occupied, nothing will happen until the next time. If most of the spawnpoints were occupied, this would really slow down the rate that new baubles spawn because it could skip numerous chances to spawn new ones.
B
94
S
33
G
20
Posts: 438
Reputation: 17,605

Post » Sat Dec 14, 2013 5:07 pm

Thanks, I will try it with a "for each".

I try to figure out another way to handle the check if a spawnpoint is already occupied. But so far it seemed to work.
B
53
S
16
G
6
Posts: 718
Reputation: 9,150

Post » Sat Dec 14, 2013 5:20 pm

It may be hard to tell if it is skipping a spawn opportunity since the next one could come along any second. It would only be obvious later when the tree is getting pretty full.
B
94
S
33
G
20
Posts: 438
Reputation: 17,605

Post » Sat Dec 14, 2013 5:38 pm

What I would do at the top of the spawn code is first check if any spots are available, by making a string list of available ones in a loop (at the end of the loop you would have a string like "1|3|4|7|8|9|10")

if the string is empty ("") then you know there are no available spots so exit, otherwise choose a random spot out of the string using TokenAt and TokenCount. That way you only have to read through the array once, and pick one random number. Since you are only picking from available spots there is no possible way to choose a spot already taken (and you have handled the case where they are all taken).AllanR2013-12-14 17:48:26
B
94
S
33
G
20
Posts: 438
Reputation: 17,605

Post » Sat Dec 14, 2013 5:45 pm

ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Sun Dec 15, 2013 10:08 am

Thank you very much to both of you!
It seems the "For each" loop fixed the left over entries in the array. :)


And I modified your example a little bit and it works fine so far. Thanks for taking the time to create this!

B
53
S
16
G
6
Posts: 718
Reputation: 9,150


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 4 guests