Help to calculate rank/position in racing game

Hello everyone, I want to ask for some help regarding how to calculate position in racing game.

I have made an array that has numbers from 0 to no limits (like this; [0,1,2,3]), this number is taken from every kart. This number is what helps to determine the position (1st, 2nd, so on), the bigger the number the better the position.
The problem I’m facing is that I can’t get to assign the number to the karts again but in the ordered format (instead of 8,5,3,2, I want 1st,2nd,3rd,4th). Any help is appreciated.

I have included an attachment if you didn’t understand.

Use the Array Tools extension. It has a sort function you can run over the array.

I did sort it.
I just want to know how do I loop for every kart and assign them the ranking. By the way, every kart has a positionNumber(the number that is also in the array of numbers).
So maybe a loop that checks if the instance variable is equal to item in array then assign the kart a ranking based on the order of number, but I don’t know how to implement such kind of loops in Gdevelop event sheet.

A python pseudo code to explain what I’m trying to do:

while True:
  for x in len(karts): 
    for y in len(PositionNumber) # PositionNumber is an array and a global variable
      if karts[x].positionnumber == PoistionNumber:
        karts[x].rank = y # every kart should have a rank, becuase every kart has theri position number in the global array

I think you meant this in your pseudo-code:

if karts[x].positionnumber == PoistionNumber[y]:

If so, then you’ll want something like this:

1 Like

But what if the value didnt match?
It will skip it, right?
if yes then how do I make it not to skip karts that didnt match but try another kart or position y

Yes, that kart with its number not in the array will be skipped. In which case you’ll need to create another set of rules covering this scenario. The events in my screen snip are based on the pseudo code you gave.

Ideally you will have populated the kartNumberArray with all the kart numbers, which will avoid that scenario…

I am thinking of implementing it in structures. The key will be the name of the kart. The position number will be the item. After I calculate it, the after I calculate it, I give it to the kart name, which is the key. But I don’t know how to implement that.

why gdevelop is extremely complex and confusing about structures and arrays. Other variables are okay, but structures and arrays are extremely complex. I don’t understand how they are implemented, how do you add, how do you subtract. there’s a. Maybe it should be improved by documentation. The current is not clear

Structures and arrays are fairly straight forward. The complexity can come in the implementation of them. Not designed correctly and they’ll be a nightmare to work with.

An array is just a list of values. Think of it as a stack, with the first item on the bottom, to the last item at the top of the stack. To add an item, you put it at the top of the stack. If you remove an item from anywhere in the stack, everything on top drops a spot.

A structure, aka a dictionary in other programming languages, holds information in a key-value pairing. Using the key, you get it’s corresponding value.

If your structure looks like:

Then you can access the values with:



1 Like