Skip to content

Instantly share code, notes, and snippets.

@mabako

mabako/Events.nut

Created Sep 11, 2014
Embed
What would you like to do?
vehicle-events.nut
class Events
{
/*
* Add all Events & Execute the relevant ones for the players currently in game
*/
function Initialize()
{
Event.Add(Package.Current(), "stop", OnStop);
Event.Add(Player, "enterVehicle", OnEnterVehicle);
Event.Add(Player, "exitVehicle", OnExitVehicle);
OnStart();
}
/*
* Load all vehicles.
*/
function OnStart()
{
foreach(_, data in _sql._Select(CVehicle.GetQueryStub()))
CVehicle(data).Create();
print("[vehicles] Spawned " + vehicles.len() + " vehicle(s)");
}
/*
* Save all positions
*/
function OnStop(...)
{
foreach(vehicle in vehicles)
vehicle.Destroy();
}
/*
* Handle entering a vehicle
*/
function OnEnterVehicle(player, vehicle, door, seat)
{
if(vehicles.rawin(vehicle))
{
local v = vehicles[vehicle];
player.SendMessage("#999999* This " + v.GetName() + " " + v.GetOwnerForEntryMessage() + ".");
}
else
player.SendMessage("#999999* This is a Civilian Vehicle.");
}
/*
* Save vehicle position upon exiting it.
*/
function OnExitVehicle(player, vehicle)
{
if(vehicles.rawin(vehicle))
vehicles[vehicle].Save();
}
}
Event.Add(Package.Current(), "start", @(...) Events.Initialize());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment