Page 1 of 1

Enter/Exit Vehicle and Sphere Events

Posted: Thu Aug 29, 2019 3:26 am
by Vortrex
Here's a few events for entering and exiting vehicles and spheres:

Code: Select all

addEvent("OnPedEnterSphere", 2);
addEvent("OnPedExitSphere", 2);
addEvent("OnPedEnterVehicle", 3);
addEvent("OnPedExitVehicle", 2);

addEventHandler("OnEntityProcess", function(event, entity) {
	if(entity.isType(ELEMENT_PLAYER) || entity.isType(ELEMENT_CIVILIAN)) {
		getElementsByType(ELEMENT_VEHICLE).forEach(function(vehicle) {
			if(entity.vehicle == vehicle) {
				if(entity.getData("in.vehicle") == null) {
					triggerEvent("OnPedEnteredVehicle", entity, entity, vehicle, getPedVehicleSeat(entity));
					triggerEvent("OnPedEnteredVehicle", vehicle, entity, vehicle, getPedVehicleSeat(entity));
					entity.setData("in.vehicle", vehicle);
				}
			} else {
				if(entity.getData("in.vehicle") == vehicle) {
					triggerEvent("OnPedExitedVehicle", entity, entity, entity.getData("in.vehicle"));
					triggerEvent("OnPedExitedVehicle", vehicle, entity, entity.getData("in.vehicle"));
					entity.removeData("in.vehicle");
				}
			}
		});

		getElementsByType(ELEMENT_MARKER).forEach(function(sphere) {
			if(sphere.position.distance(entity.position) <= sphere.radius) {
				if(entity.getData("in.sphere") == null) {
					triggerEvent("OnPedEnterSphere", entity, entity, sphere);
					triggerEvent("OnPedEnterSphere", sphere, entity, sphere);
					entity.setData("in.sphere", true);
				}
			} else {
				if(entity.getData("in.sphere") != null) {
					triggerEvent("OnPedExitSphere", entity, entity, sphere);
					triggerEvent("OnPedExitSphere", sphere, entity, sphere);
					entity.removeData("in.sphere");
				}
			}
		});
	}
});

function getPedVehicleSeat(ped) {
	for(let i=0;i<=3;i++) {
		if(ped.vehicle.getOccupant(i) == ped) {
			return i;
		}
	}
	return 0;
}
Just throw that into any client script. Once in place, you can use event handlers with it like you normally would.
Event names are OnPedEnterSphere and OnPedExitSphere for the spheres and OnPedEnterVehicle and OnPedExitVehicle for vehicles.

These are bindable, so you can use bindEventHandler with any ped object, if you want.

Handler arguments are the same for both sphere events: (Ped, Sphere)
For vehicle events, the enter event is: (Ped, Vehicle, Seat) and the exit event is (Ped, Vehicle)

Code: Select all

addEventHandler("OnPedExitSphere", function(event, ped, sphere) {
	message("Ped " + String(ped.id) + " entered sphere + " + String(sphere.id), COLOUR_YELLOW);
});

// In this section, "sphere" references an existing sphere in the game world.
bindEventHandler("OnPedEnterSphere", sphere, function(event, ped, sphere) {
	message("Ped " + String(ped.id) + " entered sphere + " + String(sphere.id), COLOUR_YELLOW);
});

addEventHandler("OnPedExitVehicle", function(event, ped, vehicle) {
	message("Ped " + String(ped.id) + " exited vehicle + " + String(vehicle.id), COLOUR_YELLOW);
});

Re: Enter/Exit Vehicle and Sphere Events

Posted: Fri Aug 30, 2019 2:23 pm
by bluesn0w
Damn, those are some new cool events
Thanks for posting.

Re: Enter/Exit Vehicle and Sphere Events

Posted: Sun Sep 01, 2019 9:02 am
by Vortrex
Thanks! I kinda just threw them together. We needed these badly.

Also, I wanted to provide an example of how people can make custom events using resources. Found the perfect opportunity.

Re: Enter/Exit Vehicle and Sphere Events

Posted: Wed Nov 27, 2019 10:25 pm
by Vortrex
I have updated this a bit. You can now bind OnPedEnterSphere and OnPedExitSphere with either a sphere or ped object and OnPedEnterVehicle and OnPedExitVehicle with either a vehicle or ped object. Before this update, only ped objects could be used as the binded source.