# Distance drove

## Recommended Posts

How to start count how many km the player drive and exec some function after he drove X km?

For e.g I want the script to exec something after the player drove 5+ km...

Thank you!!!

##### Share on other sites

is it literally driving 5 km or should the player be 5 km away from a certain point?

For the last one something like

`waitUntil {player distance *startingpos* > 5000};`

might do.

##### Share on other sites
49 minutes ago, JSD said:

is it literally driving 5 km or should the player be 5 km away from a certain point?

For the last one something like

```
waitUntil {player distance *startingpos* > 5000};```

might do.

Thank you JSD!!!! But it is the first option: literally driving 5 km. The starting pos don't matter, for ex. the player can go ahead 3km and came back 2km, this will get 5km. Do u knew any way, please?

##### Share on other sites
Just now, joew said:

Thank you JSD!!!! But it is the first option: literally driving 5 km. The starting pos don't matter, for ex. the player can go ahead 3km and came back 2km, this will get 5km. Do u knew any way, please?

I'm not sure about any actual way of doing it but you may just check the position every few seconds, get the distance from the last position and then add the distance to some variable. I'm not too good at this :P

This may do?: (Haven't tested or anything)

```_distance = 0;
_oldPos = position player;
waitUntil {
// wait a few seconds
sleep 5;

// get the traveled distance and add it to the distance variable
_traveledDistance = _oldPos distance position player;
_distance = _distance + _traveledDistance;

// set position for next cycle or whatever it's called
_oldPos = position player;

// go ahead if the distance is over 5000
_distance > 5000;
};```

• 1

##### Share on other sites
1 hour ago, JSD said:

I'm not sure about any actual way of doing it but you may just check the position every few seconds, get the distance from the last position and then add the distance to some variable. I'm not too good at this :P

This may do?: (Haven't tested or anything)

```
_distance = 0;
_oldPos = position player;
waitUntil {
// wait a few seconds
sleep 5;

// get the traveled distance and add it to the distance variable
_traveledDistance = _oldPos distance position player;
_distance = _distance + _traveledDistance;

// set position for next cycle or whatever it's called
_oldPos = position player;

// go ahead if the distance is over 5000
_distance > 5000;
};```

Thank you @JSD!!! I will test it and reply here when I'm done!

##### Share on other sites

I once made this mileage counter:

Maybe that's something for you.

##### Share on other sites

You can set up a trigger and add distance every 0.5 sec

```fnc_chkMileage =
{
params ["_veh", "_limit", ["_driven", 0]];
private _counter = createTrigger ["EmptyDetector", [0,0,0], false];
_counter setVariable ["data", [_veh, _limit, _driven, getPosWorld _veh]];
_counter setTriggerStatements
[
'
thisTrigger getVariable "data" call
{
params ["_veh", "_limit", "_driven", "_lastPos"];
_newPos = getPosWorld _veh;
_driven = _driven + (_lastPos vectorDistance _newPos);
_this set [2, _driven];
_this set [3, _newPos];
_driven >= _limit
}
',
'
hint "distance complete";
deleteVehicle thisTrigger;
',
''
];
};

// example

[car, 1000] call fnc_chkMileage;```

After about 1000m  the hint will pop up

• 1