How do I calculate the CPU's positions?

0 favourites
  • 4 posts
  • Hello, I am working on a 2D top-down racing game. I've implemented CPU opponents as well as a system for calculating the player's current position (1st, 2nd, etc.) But I've been struggling with how to calculate the CPU's current position.

    The system I made is that each racer (both the player and all of the CPUs) have an instance variable called PosScore. Basically, the higher the PosScore, the further you are in the race. For the player, I can just compare the PosScore against all the other CPU's PosScores in order to calculate your current position. But for the CPUs it's different because I have to compare the PosScore against both the player and all the other CPUs.

    The preferable solution would be for every CPU to have a "CurrentPosition" instance variable that updates accordingly like it does for the player.

    Here's how I'm calculating the PosScore variables for both the CPUs and the player:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you're saying the problem is when you include the player with CPU, it can be resolved by using a Family for example called 'Racers' that contains the player and CPU, with a family instance variable for position for example Racers.pos

  • If you're saying the problem is when you include the player with CPU, it can be resolved by using a Family for example called 'Racers' that contains the player and CPU, with a family instance variable for position for example Racers.pos

    Hey lionz, sorry for the late reply, but I wanted to ask how exactly I should change my code to account for a family instance variable? Also, should the family instance variable be for the PosScore or the current position?

  • If score is working as expected for CPU then you replace CPU with 'racers' family which includes CPU and Player. For position it looks like you already have the logic for each CPU ordered by score, so here you replace with 'racers' family again. In that loop though you should use a counter variable to set the positions, adding 1 to it in the loop, so this means the 'racer' with highest score become pos 1, second highest score is pos 2 etc. All the variables look important so score and position would move to family var as well as CurrentLap, NextCheckpoint etc

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