How do I rank my objects by 1st, 2nd, 3rd, 4th place based on their score

0 favourites
  • 7 posts
From the Asset Store
Game rank and league badge creator mega asset pack
  • I'm working on a board game similar to Mario Party.

    throughout the game all 4 player characters should be ranked (1st, 2nd, 3rd, 4th place) based on their score (how I determine their score isn't relevant to this topic)

    For the life of me I can't figure out how to do this, I have tried a bunch of things and searched on google for a solution but found nothing.

    Right now I have all 4 player objects in a family and they get sorted by their score from highest to lowest and then a sub event picks the 1st, 2nd, 3rd, and 4th object in the family and sets their "placement" variable to 1, 2, 3, and 4

    and then a corresponding rank sprite will update to 1st 2nd 3rd or 4th depending on the player's placement variable.

    I think the problem here is that selecting the "0th,1st 2nd etc" instance of a family doesn't select any of the objects in that family but rather the family itself. and since there is only 1 instance of the family, it always sets all the players to 1st place

    that being said what would be the best way to go about setting the placement of all 4 of the players based on their score? would I even need a family to do this?

  • Yeah, you can't use "Pick Nth instance" in a "For each" loop, because it processes one instance at a time. You need to compare loopindex instead.

    For each playerfamily order by score
    ..Compare loopindex=0 : set placement to 1
    ..Compare loopindex=1 : set placement to 2
    ..Compare loopindex=2 : set placement to 3
    

    Or just do this:

    For each playerfamily order by score: set placement to (loopindex+1)
    
  • thanks, this seems to work, under the assumption that the player characters all have different scores.

    now the problem is that if multiple players tie in terms of their score, the loop index doesn't know that. lets say 3 players have the same score and are tied for 1st, those 3 players are not all going to be listed as first in the loop index.

    They are still randomly placed in the first second and third spot of the loop index. So once the loop index is ran, those 3 players will not all be listed as being in 1st place but rather randomly placed in 1st, 2nd and 3rd

    I tried implementing a fix using sub events but that did not seem to change anything

    here is a picture of what I have set up currently

  • First of all I wouldn't run this logic every tick as it looks like you are doing, just run it once to stop any confusion happening. At least until it is working correctly.

    Your events don't compare instances but also it is difficult to do this, you can't compare objectA to objectA when picking.

    My approach would be to bring the player scores into global variables. Set object.score for object.placement=1 to GV 1st and object.placement=2 to GV 2nd etc. First establish those positions because of course in the next section they would change.

    Then you could say for example if object.placement=2 and object.score= GV1 then set placement to 1. If object.placement=3 and object.score= GV2 then set placement to 2, if score = GV1 then set placement to 1 (top 3 tied). I would take this approach to get around picking issues with the same object type.

  • lilOptionz You don't need to pick instances inside the "For Each" loop. Like I said, this loop processes one instance at a time, so the instance is already picked! Just use "Compare Two Numbers loopindex=0" condition.

    Your events where you are trying to pick players by comparing loopindex make no sense. Also, expressions like loopindex(1)&loopindex(2) are totally wrong, loopindex doesn't take numerical argument.

    And as lionz mentioned, you shouldn't run this code on every tick, make sure it's executed once when needed, for example when a layout with scores is opened.

    Try changing the code like this:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • thanks guys I got it working. a bit clunky but it does the trick.

    I will post what I have done here so that anybody who searches for how to do this in the future can see my solution.

  • That's pretty terrible and makes little sense. Please change it like this:

    And if you not using those variables (1stplacescore, 2ndplacescore etc.) anywhere else, then you can even delete all sub-events. All you need is just the first line.

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