[SOLVED] Help with an array-sorting common event

Hell there! I'm currently working on my own battle system, one that can make battles on the map itself. I'm currently using common as I'm not the brightest with JavaScript as of now.
Anyway, the idea here is that it checks which players or enemies are active, puts them in an array with a certain value so their corresponding switches can be triggered later, and then it would reorganize the order of the values based on their speed.

So far, the first two points are working properly, but the last part where it reorganizes the values is the one where I'm having trouble with. I'm using a selection sort algorithm (I was referencing this for those who are curious) made in events to do all of this, but for some strange reason, whenever the game runs and it goes to the sorting part, it freezes the game. It doesn't give me an error nor does the console in the DevTools give me an error; it just flat out freezes. I can't if it's lagging so hard because of the events I laid out, or if I'm doing something wrong. But nevertheless, I could use some feedback.

eventcode.png

If there's any feedback that you can give me or any other alternative to this, I would love to know. Thank you!
You got infinite loops. You have to break out of a loop for it to end. There's a specific event command for that. Remember to break out of both loops.

For debugging you can also add wait commands. This should allow you to use the debugging tools to inspect how the code works.
Just for info, arrays in JS have a built-in sort method:
E.g. to sort ascending an array of numbers stored in game variable 32:
  • Script: $gameVariables.value(32).sort((a, b) => a - b);
I'm not clear what values you expect to be in your array. If they're actor/enemy instances:
  • Script: $gameVariables.value(32).sort((a, b) => a.speed() - b.speed());
You can still do this manually, just thought I'd mention it. 👋
Just for info, arrays in JS have a built-in sort method:
E.g. to sort ascending an array of numbers stored in game variable 32:
  • Script: $gameVariables.value(32).sort((a, b) => a - b);
I'm not clear what values you expect to be in your array. If they're actor/enemy instances:
  • Script: $gameVariables.value(32).sort((a, b) => a.speed() - b.speed());
You can still do this manually, just thought I'd mention it. 👋
I didn't know this line exists, that saves me a lot of trouble honestly! Though, now that I did it (which I copy-pasted to make sure it works), it now says it's not a function??? Which is bizarre
Screenshot 2024-06-23 210806.png


I didn't know this line exists, that saves me a lot of trouble honestly! Though, now that I did it (which I copy-pasted to make sure it works), it now says it's not a function??? Which is bizarre
Screenshot 2024-06-23 210806.png
Nevermind, I figured out the problem. Thanks!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-help-with-an-array-sorting-common-event.169730/