How do I prevent two of the same sprites from having the same value?

Not favoritedFavorited Favorited 0 favourites
  • 6 posts
From the Asset Store
Antisuspend Plugin for Construct 3 prevents the runtime from getting suspended.
  • Hi, I am currently making an F-Zero fangame (a racing game) and am having issues with creating the final position system (finding out whether a racer finished 1st, 2nd, 8th, etc.)

    I've had little issues with getting the player's position to work. I've created this code that triggers when the player crosses the finish line, and it basically counts how many Racers have finished the race in order to determine the player's final positon)

    (Some things to note: The "Racers" family only contains the PlayerMachine and CPU sprites, and "Final_Position" is a family variable. Also the code below continues in the exact same way up until CPU.PickedCount = 14, because this game has 15 racers.)

    I've tried using this same system for the CPUs, although I have to make it using the Racers family rather than just the CPUs, as the CPUs are competing against both each other and the player of course. I also want to implement a system where the boolean variable "FinishedRace" (which is also a "Racers" family variable) becomes true once the player reaches the finish line, making the CPU's final position whatever their current position (CurrentPositon) was in the race.

    But this system has proved to be very glitchy and unreliable with the CPUs. The main problem I'm running into is multiple racers having the same position at the end of the race. The most common one is both a CPU and the player ending with the same Final_Positon, but sometimes multiple CPUs have the same Final_Position as well. My guess as to why this happens for the CPUs is because of this set of code that makes the CPU's final position whatever their current position is when the player crosses the finish line, as mentioned earlier.

    I've been trying to find a way to have two instances of the same sprite compare each other, like having something like:

    If Racers.Final_Position = Racers.Final_Position, then add 1 to Racers.Final_Position

    But obviously that code wouldn't work, as Construct doesn't allow you to compare two of the same families in that way.

    Sorry if I'm not clear about something, but if any of you all could help it would be greatly appreciated! These CPU position bugs have been plaguing my game for a while now

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Delete most of this, create a global variable for position set to 1 by default, then say when Racer crosses the finishing line (lap > 4 ?), set finishing position to global variable and add 1 to global variable. If needed you can also use a condition 'final position = 0' to act as your trigger once, it will only set a position value when it is the default.

  • Delete most of this, create a global variable for position set to 1 by default, then say when Racer crosses the finishing line (lap > 4 ?), set finishing position to global variable and add 1 to global variable. If needed you can also use a condition 'final position = 0' to act as your trigger once, it will only set a position value when it is the default.

    Thank you, I can see how this would work mathematically. Although I am having issues with it.

    I did some debugging and found that PositionCalc (the global variable you told me to make) always ends at 4 at the end of the race for some reason. There seems to be an issue with how the variable gets increased

    ^ How the CPUs get PositionCalc added

    ^ How the Player's PositionCalc gets added

    ^ How the CPUs (the ones who haven't finished the race yet) get their PositionCalc added (should I do it like this? Or should I go back to the original "set Final_Position to Current_Position for these CPUs? Because I'm not sure)

    Anyways I'm still pretty stumped on this, if you or anyone else could help me out still that would be great

  • I expected you to do it with Racers family that includes player and cpu, that will reduce risk of bugs. Also you should drop the trigger once and use the variable as the condition as mentioned above, either position=0 or finishedrace=false will do. Should be one easy line/event for everything.

    Does it stop at 4 when the player finishes and 2 CPU? It is always 4 ? Make the single event as I described with the fam and you won't have to track so many increases to the variable. The results screen is new, you can add debugging to find out if that does anything, maybe the picking is off. Reduce opacity of Racers that are finished to clearly determine who has finished and compare with the variable, do they match, and do this without the results screen first to find the cause.

  • Thank you, I've implemented this script into my game but unfortunately I'm still running into some issues. The Final_Position variable for the player works perfectly, but there are some issues with the CPUs. Firstly, now the PositionCalc variable is still very static, this time being stuck at 2 rather than 4. But the bigger issue is that the CPU's Final_Position is almost predetermined in a way. Like CPU instance #0 always ends up with a Final_Positon of 15, CPU instance #1 ends up with 14, and so on. I'd imagine this is because there isn't any code that makes the CPU's Final_Position become its current position once the player crosses the finish line. Is there any way I can still implement that? Or is there just an issue with this one script that I made that's causing this to happen?

    ^ The one script I made that now involves the Racers family

    Sorry if I'm not super clear, I can send a c3p file if you'd like me to, but I just wanted to say thank you for your patience and help so far! I do think we're getting somewhere

  • I think that problem is based on logic of the current position during the race which was never really discussed. So they don't finish the race? Could you speed up the CPU so they finish, or wait? Or you could do - for each Racer ordered by current position, and not finished, set final position to global var and add 1.

    Also if that global var is stuck at 2 that means just 1 Racer completed the laps ? confusing. I would check the variable in debug view to see if it is adding 1 as racers finish and setting their final position. And what is cpu_alive ?

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