fivem lua could you give me an example of object oriented programming registercommand…
- User
fivem lua could you give me an example of object oriented programming registercommand that spawns a vehicle
- Assistant
Certainly! Here's an example of object oriented programming in FiveM Lua for a command that spawns a vehicle:
-- Define a VehicleSpawner class VehicleSpawner = {} VehicleSpawner.__index = VehicleSpawner -- Constructor function function VehicleSpawner.new(model, x, y, z, heading) local self = setmetatable({}, VehicleSpawner) self.model = model self.x = x self.y = y self.z = z self.heading = heading return self end -- Method to spawn the vehicle function VehicleSpawner:spawn() local vehicle = CreateVehicle(GetHashKey(self.model), self.x, self.y, self.z, self.heading, true, false) SetEntityAsMissionEntity(vehicle, true, true) SetVehicleHasBeenOwnedByPlayer(vehicle, true) SetVehicleNeedsToBeHotwired(vehicle, false) SetVehRadioStation(vehicle, "OFF") SetPedIntoVehicle(PlayerPedId(), vehicle, -1) end -- Register the command to spawn the vehicle RegisterCommand("spawnvehicle", function(source, args) local model = args[1] local x = tonumber(args[2]) local y = tonumber(args[3]) local z = tonumber(args[4]) local heading = tonumber(args[5]) local spawner = VehicleSpawner.new(model, x, y, z, heading) spawner:spawn() end, false) - User
create a registercommand that deletes the vehicle
- Assistant
Sure, here's an example of a command that deletes the nearest vehicle to the player using object oriented programming in FiveM Lua:
-- Define a VehicleDeleter class VehicleDeleter = {} VehicleDeleter.__index = VehicleDeleter -- Constructor function function VehicleDeleter.new() local self = setmetatable({}, VehicleDeleter) return self end -- Method to delete the nearest vehicle to the player function VehicleDeleter:delete() local playerPos = GetEntityCoords(PlayerPedId()) local vehicle = GetClosestVehicle(playerPos.x, playerPos.y, playerPos.z, 5.0, 0, 71) if DoesEntityExist(vehicle) then SetEntityAsMissionEntity(vehicle, true, true) DeleteEntity(vehicle) Notify("Vehicle deleted.") else Notify("No nearby vehicle found.") end end -- Function to show a notification message to the player function Notify(text) SetNotificationTextEntry("STRING") AddTextComponentString(text) DrawNotification(false, false) end -- Register the command to delete the nearest vehicle RegisterCommand("deletevehicle", function(source, args) local deleter = VehicleDeleter.new() deleter:delete() end, false)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.