Jump to content

Cruise Control


Captien

Recommended Posts

Hello Everyone,
 

Today I would like to introduce a Tutorial about how to do Cruise control for your vehicle.

The script generally sets your vehicle on a constant velocity without the need for you to accelerate, and you can have full control of the vehicle again by press the brakes or just run out of the car ;D (joke).

Anyways it's simple

Server-side:

Spoiler

mp.events.add({
	'playerEnterVehicle': (player) => {
		player.call('VehicleEnter', [player.seat]);
    },
  
  	'playerExitVehicle': (player) => {
		player.call('VehicleExit', [player.seat]);
    }
});

This generally 2 events that calls the client side events and tells them about if the player entered the vehicle and is a Driver or not.

Client-side:

Spoiler

let localPlayer = mp.players.local,
    vehSeat, vehMaxSpeed = null,
    state = 0;

// Thanks to kemperr
mp.gui.execute("const _enableChatInput = enableChatInput;enableChatInput = (enable) => { mp.trigger('chatEnabled', enable); _enableChatInput(enable) };");
mp.events.add('chatEnabled', (isEnabled) => {
    mp.gui.chat.enabled = isEnabled;
});

mp.events.add({
    'VehicleEnter': (seat) => {
        if (seat === -1 && localPlayer.vehicle) { // if he/she was driver
            vehSeat = seat;
            vehMaxSpeed = mp.game.vehicle.getVehicleModelMaxSpeed(localPlayer.vehicle.model);
            mp.game.graphics.notify(`Press ~r~C if you want to start Cruise control.`);
        }
    },
    'VehicleExit': (seat) => {
        if (seat === -1) {
            state = 0;
        }
        vehSeat = -2;
    },

    'render': () => {
        if (state == 1 && localPlayer.vehicle) { // if cruise was triggered
            let currentvelo = localPlayer.vehicle.getVelocity();

            currentvelo.x = currentvelo.x * 1.1;
            currentvelo.y = currentvelo.y * 1.1;

            localPlayer.vehicle.setVelocity(currentvelo.x, currentvelo.y, currentvelo.z) // set velocity to the current one.

            if (localPlayer.vehicle.hasCollidedWithAnything() || localPlayer.vehicle.isInAir()) // check if vehicle did collision or went off the ground
            {
                state = 2; // stop cruise
                toggle();
            }

            if (buttonchecker()) { // if space, enter, F, S buttons were triggered
                state = 2; // stop cruise
                toggle();
            }
        };
    }
});

mp.keys.bind(0x43, true, _ => { // binding C to the cruise speed
    if (vehSeat === -1 && localPlayer.vehicle && !mp.gui.chat.enabled) { // if he/she was driver and chat isn't active.
        if (state == 0 || state == 2) { // if cruise was disabled
            state = 1;
            toggle(); // start the cruise
        } else if (state == 1) // if cruise was enabled
        {
            state = 2;
            toggle(); // stop the cruise
        }
    } else {
        return false;
    }
});

function toggle() { // Toggler of the Cruise speed.
    if (localPlayer.vehicle) {
        switch (state) {
            case 1:
                {
                    let speed = localPlayer.vehicle.getSpeed();
                    localPlayer.vehicle.setMaxSpeed(speed);
                    mp.game.graphics.notify(`Your cruise speed was set to ~b~${(speed * 3.6).toFixed(0)}~b~ km/h`);
                    break;
                }
            case 2:
                {
                    localPlayer.vehicle.setMaxSpeed(vehMaxSpeed);
                    mp.game.graphics.notify(`Cruise speed is now ~r~disabled! Drive safely!`);
                    state = 0;
                }
        }
    }
};

function buttonchecker() { // if user presses space, s , enter, or F returns true
    return (mp.keys.isDown(32) || mp.keys.isDown(83) || mp.keys.isDown(70) || mp.keys.isDown(13))
};

 

 

Thank you and i wish you enjoyed the Tutorial. Wait for more soon!

Edited by Captien
Improved structure, added current rage compatibility
  • Like 7
Link to comment
Share on other sites

  • 8 months later...
function buttonchecker() { // if user presses space, s , enter, or F returns true
  	return (mp.keys.isDown(32) || mp.keys.isDown(83) || mp.keys.isDown(70) || mp.keys.isDown(13))
}

Works, but less code. More of an optical thing.

But thanks man, i was looking for this.

You have multiple errors:

  • DriverSeat is -1
  • in player.call() you have to pass variables in an array (click)
Link to comment
Share on other sites

2 hours ago, paccoderpster said:

function buttonchecker() { // if user presses space, s , enter, or F returns true
  	return (mp.keys.isDown(32) || mp.keys.isDown(83) || mp.keys.isDown(70) || mp.keys.isDown(13))
}

Works, but less code. More of an optical thing.

But thanks man, i was looking for this.

You have multiple errors:

  • DriverSeat is -1
  • in player.call() you have to pass variables in an array (click)

Back in 2017 player call didn't require brackets, hence why the code may not be 100% correct now.

  • Like 1
Link to comment
Share on other sites

On 8/30/2018 at 9:26 PM, paccoderpster said:

function buttonchecker() { // if user presses space, s , enter, or F returns true
  	return (mp.keys.isDown(32) || mp.keys.isDown(83) || mp.keys.isDown(70) || mp.keys.isDown(13))
}

Works, but less code. More of an optical thing.

But thanks man, i was looking for this.

You have multiple errors:

  • DriverSeat is -1
  • in player.call() you have to pass variables in an array (click)

Thanks for your buttonchecker function suggestion, it was added to the new updated code

  • Like 1
Link to comment
Share on other sites

  • 4 months later...
  • 4 weeks later...

maybe such thing like cruise control should be builded more realistic with the help of simulating user inputs for break and accelerate.

e.g. https://wiki.rage.mp/index.php?title=Controls::setControlNormal

As long as speed < speed it should be: set accelerate (not sure which value is correct)
and as long speed > speed it should be: break 

theoretically it should have a tolerance (so let's say if speed < setSpeed = acc; speed >= setspeed +5mph = do nothing; speed > setspeed + 5mph break)

also there should be a check on Controls for break and speeding to turn it off

 

just as a small idea 😉 

Link to comment
Share on other sites

4 часа назад, Geramy92 сказал:

maybe such thing like cruise control should be builded more realistic with the help of simulating user inputs for break and accelerate.

e.g. https://wiki.rage.mp/index.php?title=Controls::setControlNormal

As long as speed < speed it should be: set accelerate (not sure which value is correct)
and as long speed > speed it should be: break 

theoretically it should have a tolerance (so let's say if speed < setSpeed = acc; speed >= setspeed +5mph = do nothing; speed > setspeed + 5mph break)

also there should be a check on Controls for break and speeding to turn it off

 

just as a small idea 😉 

Pretty good idea

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...