For each not working properly

0 favourites
  • 5 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • So I'm trying to make little critters that evolve a neural network. The simulation works good but I'm having a problem with the Arrays that make up their brains. I'm trying a for each loop to randomly pick parts of the array to mutate but for some reason it only does this to one animal instead of all 5 of them

    Here's a link to a capx https://drive.google.com/open?id=1Qtyxv ... am-mpcUCHK (the code in question is at the very bottom of the event sheet)

    I think it's something with how each works with arrays maybe? Because I tried to see if I could change all of the sprites frames using it and that works, it's only the arrays that seem to be having an issue.

  • I'm not sure what's going on in your code, but the For each loop is working and it does change each of the 5 Brain arrays.

    You can check it yourself - run the game in Debug mode, select one of the arrays and see the values changing every 5 seconds.

    You can also add Browser->Log to this loop to output debug messages.

    For example: Browser Log Brain.UID, you'll see that all 5 Brain UIDs are processed.

  • I'm sorry, that was the wrong version. Try this capx https://drive.google.com/file/d/1TniM_Y ... sp=sharing

    You'll see that for the animals brains only one of them is randomized and the rest all stay the same

  • bbjGames

    "For Each" loop is working and it does process each of the Brain instances.

    The problem is with the sub-events. I believe you need to reset the variables to their initial values (x=4, y=0, z=-1) in event #36.

    Alternatively, you can move variables declaration inside the "For each" loop, then they will be reset automatically.

    You might also want to reset them in between "Repeat 30 times" and "Repeat 16 times" loops.

    The "mutate" variable always equals 5, so all events with condition "mutate<=2" will never work.

    There may be other mistakes in those loops, I really don't understand what are you doing there.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I set mutate to 5 to make sure it was obvious that the loops wasn't working properly. I moved the variables down to the repeat loop and now it works great, thanks a lot. I feel like that was sort of a dumb mistake on my part but at least now I know for next time

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