Sign in to follow this  
Captien

Cruise Control

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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.