Values in Array not set properly sometimes?

0 favourites
  • 7 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • 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):

    <img src="http://img5.fotos-hochladen.net/uploads/baublespawnarra7vnw03z4gq.jpg" border="0" />

    (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):

    <img src="http://img5.fotos-hochladen.net/uploads/baublespawnarrazodc81psrv.jpg" border="0" />

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

    <img src="http://img5.fotos-hochladen.net/uploads/arrayonespawne7s2gkh4yip.jpg" border="0" />

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

    Thanks in advance!

  • 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.

  • 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.

  • 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.

  • 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).

  • Try this:

    RandomArraySample.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you very much to both of you!

    It seems the "For each" loop fixed the left over entries in the array. :)

    <img src="http://img5.fotos-hochladen.net/uploads/spawningfixed2xzq63vey5l.jpg" border="0" />

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

    <img src="http://img5.fotos-hochladen.net/uploads/spawningfixed1p70feicvrs.jpg" border="0" />

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