Best way to compare 4 variables

Get help using Construct 2

Post » Thu Dec 08, 2011 6:59 pm

I have four variable's:

PlayerPosition
NPC1Position
NPC2Position
NPC3Position

Every time they overlap a waypoint there next waypoint is increased until they hit the last one and the it resets to one, This is how i am trying to determine the players current position(for a racing game).

But how can i determine which position he is in from this?
I know how to determine if he's in last place or first place, but i need to know if he's in second or third also..

Anyone have some knowledge they wish to share?
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Thu Dec 08, 2011 8:35 pm

Don't understand why you have 3 NPC object instead of 3 instances of the same object
Forgot to ask last time I saw your capx
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Dec 08, 2011 9:57 pm

Simply because that is the only way i could figure out how to add a logical timer board type thing, I was planning on having the times of each npc, and position at the end of the race(This part i can do) Im just struggling on the logic for calculating the players position during the race.
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Fri Dec 09, 2011 8:03 am

hmmm With your waypoint detector system (the orange sprites) you just have to compare laps and waypoint. But indeed you can't easily evaluate pixel by pixel when a car goes in front of another one. As in a race the space continuum is bent by the shape of the track.

BUT that does not explain why you use different object for NPC.
If you want different name, just use a variable with the name of the NPC
If you want different speed parameter, you can tweak each instance
If you want to count the number of lap or current waypoint for each NPC, just create an instance variable which will old the count for each.

This way you can calculate position easily :

[code]NPC: X greater than FinishLine.X
NPC: is overlapping FinishLine
   -> NPC: set backward to true
NPC: X less than FinishLine.X
NPC: is overlapping FinishLine
NPC: [INVERT] is backward
   -> NPC: set crossing to true
NPC: [INVERT] is overlapping FinishLine
    NPC: is crossing
       -> NPC: add 1 to lap
    Every tick
       -> NPC: set crossing to false
       -> NPC: set backward to false[/code]backward booleans are use to check if things goes in the right direction when you cross the finish line.

For waypoint you will just have to check
[code]NPC is overlapping waypoint
   -> NPC: set trackPosition to waypoint ID[/code]
Then you can calculate relative position with this formula:
[code]Every tick
    -> NPC: set racePosition to NPC.lap*waypoint.Count+NPC.trackPosition[/code]
if your NPC crossed the line 3 times and is at the 5th waypoint and you have 20 waypoint, his position will be:
3*20+5 = 65
Then you just have to go through all your NPC to check position.
Unfortunately we still don't have a foreach ordered by.
So you will have to do a for loop between 0 and the max number of possible lap*number of waypoint.
For instance if you have a race which last nbLap laps
[code]Global variable inc=1
for 0 to nbLap*Waypoint.Count
    Local variable check=0
    NPC: racePosition equal loopindex
        -> NPC: set place to inc
        -> System: set check to 1
    Player: racePoistion equal loopindex
        -> Player: set place to inc
        -> System: set check to 1
    System: check equal 1
        -> System: add 1 to inc[/code]
This assume you run the same kind of calculation for the player object.
Which makes me think that you could also have the player object be the same as the other NPC but just deactivate NPCs by using a boolean you set in property panel.
[code]NPC: is Player
-> activate the race behavior
-> deactivate the bullet behavior
NPC: [INVERT] is Player
-> activate the bullet behavior
-> deactivate the race behavior
-> run the AI[/code]
This way you can really have less event to maintain
(Of course if there were families... You could just put NPC and Player into the same family and run all the above stuff just for the family)Yann2011-12-09 08:07:46
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Dec 09, 2011 2:04 pm

Wow!! thanks, This is really in depth and usefull, im sure to allot of people.

I manged to Get the lap counter part for the NPC's working(I Think)

But the second part confused me a little.

I changed it to instances of the one NPC, the others are still there just toggled disabled -- encase i cannot get this working.

Can you look at my capx and let me know what i did wrong?

Projectsmitchell2011-12-09 14:05:19
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Fri Dec 09, 2011 6:26 pm

Last edited by Yann on Sun Sep 14, 2014 10:50 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Dec 09, 2011 8:04 pm

:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Fri Dec 09, 2011 8:04 pm

encase that didn't show my appreciation, THANK YOU!!! :D :D :D
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Fri Dec 09, 2011 8:05 pm

Thanks for making the timer better aswell(bet you though i wouldn't notice ) ;-D
B
29
S
12
G
7
Posts: 740
Reputation: 7,849

Post » Sat Dec 10, 2011 1:51 am

Oh no I know you would.
That I didn't really know is if you would really understand all that I did (: (but that's a bit pretentious isn't it? :D)Yann2011-12-10 02:14:13
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: Callan S, getplay, Kagubro and 25 guests