Enter/Exit Vehicle and Sphere Events

Started by Vortrex, August 29, 2019, 03:26:20 AM

Previous topic - Next topic

Vortrex

ATTENTION: This resource is now obsolete. Please check https://github.com/VortrexFTW/gtac_vortrex/tree/master/v-events for the new version, included in my basic resources.


Here's a few events for entering and exiting vehicles and spheres:
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)


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);
});

bluesn0w

#1
Damn, those are some new cool events

Thanks for posting.

Vortrex

#2
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.

Vortrex

#3
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.

Vortrex