Sign in to follow this  
cmdflow

Acting with close vehicles

Recommended Posts

Hey,

does anybody have a working example how to interact with vehicles or other objects like peds when you are close to them?

I tried the following which won't return the id of police cars or peds but also that seems not to work:

// some keybind before so this is executed when you press L

let closestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70);
mp.game.graphics.notify("Closest Vehicle ID: " + closestVehicle);

which will give an id of the vehicle. But if i try to use that vehicle with another action it will not work. For simple test i tried to explode the vehicle with the following command

let vehicleAct = mp.vehicles.at(closestVehicle)
vehicleAct.explode();

How do i interact with the given id as this seems not to work? I need a solution in Javascript only.

 

Thanks and regards

Share this post


Link to post
Share on other sites

Might be worth trying to use

let vehicleAct = mp.vehicles.atHandle(closestVehicle);

These native functions return object handle id more likely than id in pool, also ive noticed that handle id changes when entity streams out and in.

  • Like 2

Share this post


Link to post
Share on other sites
let thenearestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70);
mp.game.graphics.notify("veh id: " + thenearestVehicle);

let testob = mp.vehicles.atHandle(thenearestVehicle);
testob.explode();

Nope. Neither works. 

Share this post


Link to post
Share on other sites

Just Explode it directly, No need to get its handle.

 

mp.game.vehicle.getClosestVehicle

gets the vehicle object direct, so just explode it directly

Share this post


Link to post
Share on other sites

No,

did some quick test getClosestVehicle returned for me typeof(thenearestVehicle) is number and JSON.stringify(thenearestVehicle) is 7682 which i consider to be handle id. When you add some more test code from wiki for example

testob.setTyreBurst(0, false, 1000);
testob.setTyreBurst(1, false, 1000);
testob.setTyreBurst(4, false, 1000);
testob.setTyreBurst(5, false, 1000);

you will see that the closest vehicle has all tires popped but did not explode, because .explode() method does not seem to be working even trying example from wiki

if( commandName === "exptest")
{
  var vehicle = mp.vehicles.new(989294410, localPlayer.position); //Spawns Voltic2 at 'player' position
  vehicle.explode();
}

car was spawned without explosion in current 0.3.5 release. So stuff with atHandle mentioned earlier is the right way to do this

 

edit: btw i made it explode using

testob.explodeInCutscene(true);

 

Edited by misco
made it explode with other function
  • Like 1

Share this post


Link to post
Share on other sites
4 часа назад, cmdflow сказал:
let thenearestVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 5, 0, 70);
mp.game.graphics.notify("veh id: " + thenearestVehicle);

let testob = mp.vehicles.atHandle(thenearestVehicle);
testob.explode();

Nope. Neither works. 

Use explode() in server-side

  • Like 1

Share this post


Link to post
Share on other sites
vor 11 Stunden schrieb Captien:

Just Explode it directly, No need to get its handle.

 

mp.game.vehicle.getClosestVehicle

gets the vehicle object direct, so just explode it directly

It is for a quick example. I am aiming to repair, lock and refuel vehicle from the outside where i need it's handle. Its no problem while sitting IN the vehicle but i want to lock/unlock it from outside. That's the goal.

Best regards

Share this post


Link to post
Share on other sites

This function is intended to simulate that the tires break when fired. getClosestVehicle() returns only one int, not the vehicle handle.

Here is my working client-side script:

mp.events.add('playerWeaponShot', (targetPosition) => {
    let nearestVehicle = mp.game.vehicle.getClosestVehicle(targetPosition.x, targetPosition.y, targetPosition.z, 2, 0, 70);

    if( nearestVehicle != 0 ) {
        // chance of damage
        if( Math.floor((Math.random() * 100) + 1) < 25 ) {
            for (var i = 0; i < 4; i++) {
                var DamagedWheel = Math.floor((Math.random() * 9));
                mp.vehicles.atHandle(nearestVehicle).setTyreBurst(DamagedWheel, true, 1000);
            }
        }
    }
});

But I think it's easier and safer to run these functions on the server side. Here is an example from my server:

mp.events.add('vehicle_toggle_lock', (Player) => {
    var NearbyVehicles = [];
    mp.vehicles.forEachInRange(Player.position, 2.5, (NerbyVehicle) => {
        NearbyVehicles.push(NerbyVehicle);
    });

	// sort the vehicles by range (0 is closest to the player)
    NearbyVehicles.sort(function(a, b){return b.dist(Player.position)-a.dist(Player.position)});

    if( NearbyVehicles.length > 0 )
    {
		if( NearbyVehicles[0].locked ) {
        	NearbyVehicles[0].locked = false;
        	Player.notify("You ~g~unlocked the vehicle.");
        	// MySQL_Conn.query("UPDATE vehicles SET veh_locked='0' WHERE vehicle=?", [NearbyVehicles[0].data.id]);
        } else {
        	NearbyVehicles[0].locked = true;
        	Player.notify("You ~r~locked the vehicle.");
        	// MySQL_Conn.query("UPDATE vehicles SET veh_locked='1' WHERE vehicle=?", [NearbyVehicles[0].data.id]);
        }
	}
});

 

Edited by Django93
added serverside script solution
  • Like 1

Share this post


Link to post
Share on other sites
1 час назад, Django93 сказал:

But I think it's easier and safer to run these functions on the server side. Here is an example from my server:

mp.events.add('vehicle_toggle_lock', (Player) => {
    var NearbyVehicles = [];
    mp.vehicles.forEachInRange(Player.position, 2.5, (NerbyVehicle) => {
        NearbyVehicles.push(NerbyVehicle);
    });

	// sort the vehicles by range (0 is closest to the player)
    NearbyVehicles.sort(function(a, b){return b.dist(Player.position)-a.dist(Player.position)});

    if( NearbyVehicles.length > 0 )
    {
		if( NearbyVehicles[0].locked ) {
        	NearbyVehicles[0].locked = false;
        	Player.notify("You ~g~unlocked the vehicle.");
        	// MySQL_Conn.query("UPDATE vehicles SET veh_locked='0' WHERE vehicle=?", [NearbyVehicles[0].data.id]);
        } else {
        	NearbyVehicles[0].locked = true;
        	Player.notify("You ~r~locked the vehicle.");
        	// MySQL_Conn.query("UPDATE vehicles SET veh_locked='1' WHERE vehicle=?", [NearbyVehicles[0].data.id]);
        }
	}
});

 

What for? If there is a special tool for this?

//client-side
let localPlayer = mp.players.local
let idVehicle = mp.game.vehicle.getClosestVehicle(localPlayer.position.x, localPlayer.position.y, localPlayer.position.z, 10, 0, 70)
let vehicle = mp.vehicles.atHandle(idVehicle)
mp.events.callRemote("lockVehicle", vehicle)


//server-side
mp.events.add("lockVehicle", (player, vehicle) => vehicle.locked = !vehicle.locked)

 

Share this post


Link to post
Share on other sites

On my server all data is stored in Mysql. When a player wants to lock or unlock a vehicle, it is checked if he has a key for the vehicle. But I cut the part out.

Everyone has to know that himself, how he wants to implement it. Both varieties work.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.