How do I rank instance variables for each sprite?

0 favourites
  • 3 posts
From the Asset Store
Color Variable
$4.50 USD
10% off
Easily store, modify, read and manipulate colors with Color Variables!
  • So I'm currently working on a racing game, and the final position you score is determined by a family instance variable called Time_Spent_Racing, with the family being called "Racers" containing both the player and the CPUs. Time_Spent_Racing increases by 1 every tick, and the racer with the lowest Time_Spent_Racing gets their Final_Position instance variable set to 1 (1st place), and vice versa for getting last place.

    When a CPU crosses the finish line, their position should be calculated on the spot. If they've not crossed the finish line yet, their position should say "Running..." on the results screen until they've finished the race.

    I need a script that calculates each Racer's Time_Spent_Racing, and then ranking them from lowest to highest and then giving a respective Final_Position number (ie. 1st, 2nd, 3rd, etc.)

    I'm sure there's an easy way to do this, but I just haven't found it yet. If any of you would like me to elaborate further, I can.

    Thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can insert the racer's time and id into an array and sort the array.

    Although you wouldn't need to sort at all since it would be in order already if you push to the array (back) as they finish, since they would presumably be finishing in order...

    The position number would simply be the array index + 1

  • You don't need an array to display the scores (unless you switch to another layout of course).

    You can use "System for each ordered" loop to go through all instances in their finishing order, and display their scores on the board.

    For Each Car ordered by Car.Time_Spent_Racing
    .. Final_Position=0 : Text append Car.Name & " still running" & newline
    .. Else : Text append Car.Name & " position " & Car.Final_Position
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)