Order values in an array

This forum is currently in read-only mode.
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I have to apologize for having so many questions these past few days - I don't mean to clutter the forum, but I'm very much an amateur when it comes to this stuff, haha.

    So what I'm looking to do is determine turn order for a turn-based kind of game. The idea I have is taking all the speed stats of the players/enemies and storing them in an array at the coordinates (1, 1), (1, 2), etc. I want to make the value at (1, 1) the largest, then (1, 2) the next largest, and so on. Then, I'd have a value called "Turn" which stands for whatever the turn number is - after each turn, I'd add 1 to it, and the game would check which value in the array is at (1, "Turn") to find out who goes next (obviously it'd need to be a bit more intricate than this, since the values need to refer back to whoever has that particular speed stat, but... this is just a start, haha).

    This is my current idea, anyway. Regardless of whether I stick with this method or not, I'm going to need some way to order the speed stats - I'm figuring an array would make sense here, but I'm not entirely sure.

    Also, so I don't have to make any other topics for this (and the questions are sort of related), I've been looking into the "S" plugin because it sounds like something that could be very much helpful to my cause of making an RPG... but I'm not entirely sure how it works, it seems fairly detailed and complicated. Are there any caps out there that demonstrate its functionality or anything? Or tutorials or something? I couldn't find anything particularly helpful through searching, but maybe I just wasn't looking close enough.

    Thanks guys, appreciate the help you've been giving me.

  • Not sure what you mean in terms of the array there. Can you explain more of the game mechanics you're trying to achieve and I might be able to think of another way to do it?

  • Sure. Basically what I'm trying to do is determine turn order for an RPG battle system. As in, one character takes his move, then the next, then the next, etc. The order that they take their turns in is determined by a "Speed" statistic. Whoever has the highest takes their turn first, then next highest goes next, and so on. Once the last turn is taken, it repeats from the beginning.

    Using an array for this might not even be possible - I've never used the array object, and though I basically understand what it does, this might not be an ideal use for it. My idea was to take each individual speed statistic and order them in an array from highest to lowest. The value at (1, 1) would be the highest speed stat, the value at (1, 2) would be the second highest, (1, 3) the third highest, etc. In the game, I would check whose speed stat is at (1, 1) to determine the first turn, (1, 2) to determine the second turn, and so on.

    The biggest thing here is being able to take a bunch of values and order them from highest to lowest. If I could do that, then I think I would be ok.

  • Regarding sorting of arrays you might want to download my example game "Verve!".

    It contains a lot of helpful tricks and general algorithms. And of course it also shows a way of sorting an array. It uses an optimized algorithm for sorting large arrays in a very short time. If you just have something up to 10 or so entries, you might just want to use bubble sort:

    Basically compare a number with all the numbers in the array. If it is higher then you found the right position. If it is lower compare with the next position, etc.

    How to re-arrange/re-structure the array to insert the number is shown in "Verve!"

    Link: Verve!

  • The simpler comparing method would probably be fine - I don't think there will ever be more than 5-10 values I'm going to have to sort.

    It makes sense, but I'm having trouble imagining how to construct that with events - I hate to ask for more, but could you maybe give me an example or demonstration of how it could be done? Arrays are still kind of a new concept to me, so... yeah, haha. And Verve's events for sorting seem kind of above my level of comprehension at the moment... but I'm gonna keep looking at it, it seems like I could learn quite a bit from it.

    Thanks man.

  • Here's an example. It is more simple in terms of learning curve. It uses two arrays. The first array contains the values that need to be sorted and the second array will contain the sorted values. During the process of sorting the content of the first array will be destroyed.

    Therefor you should either use that first array as a cache, or copy the resulting second array back to the first, whatever is the easiest for you (or makes more sense for your game's needs).

    sort_array.cap

  • Awesome, I'll have a look at this. Thanks so much man!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You're welcome <img src="smileys/smiley1.gif" border="0" align="middle" />

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