Open
Conversation
Vehicle State
Boss-Man-Dev
requested changes
May 15, 2025
Collaborator
There was a problem hiding this comment.
cfg.lua
looks good, only thing to suggest is maybe more spawn locations for vehicles.
client.lua
- function Vehicle:spawnVehicle
- added in local function for location checking. helps to prevent spawning vehicles in the same location
- updated plateText to use state if present.
- updated setVehicleState logic
function Vehicle:spawnVehicle(model, state, position, rotation)
self:despawnVehicle(model)
local function isLocationClear(x, y, z, radius)
local veh = GetClosestVehicle(x, y, z, radius or 3.0, 0, 70)
return not DoesEntityExist(veh)
end
-- Load vehicle model
local mhash = GetHashKey(model)
RequestModel(mhash)
local i = 0
while not HasModelLoaded(mhash) and i < 10000 do
Citizen.Wait(10)
i = i + 1
end
if HasModelLoaded(mhash) then
local ped = GetPlayerPed(-1)
local x, y, z
if position and position.x and position.y and position.z then
x, y, z = position.x, position.y, position.z
else
x, y, z = table.unpack(GetEntityCoords(ped))
end
-- Check if location is clear before spawning
if not isLocationClear(x, y, z, 3.0) then
print("[Vehicle] Spawn location is blocked.") --debug
return -- stop spawning to avoid overlap
end
local nveh = CreateVehicle(mhash, x, y, z + 0.5, 0.0, true, false)
-- Set rotation and heading if provided
if rotation then
SetEntityQuaternion(nveh, table.unpack(rotation))
else
SetEntityHeading(nveh, GetEntityHeading(ped))
end
-- Finalize vehicle setup
SetVehicleOnGroundProperly(nveh)
SetEntityInvincible(nveh, false)
-- Put the player inside the vehicle if no position is provided
if not position then
SetPedIntoVehicle(ped, nveh, -1)
end
-- Set vehicle plate
if not state.custom.plate_txt then
SetVehicleNumberPlateText(nveh, "P "..vRP.EXT.Identity.registration)
else
SetVehicleNumberPlateText(nveh, state.custom.plate_txt)
end
-- Set vehicle ownership
SetEntityAsMissionEntity(nveh, true, true)
SetVehicleHasBeenOwnedByPlayer(nveh, true)
-- set decorators
DecorSetInt(nveh, "vRP.owner", vRP.EXT.Base.cid)
self.vehicles[model] = nveh
-- Set vehicle previous state or current
-- customizations on purchase should be empty
if state and (not state.customization or next(state.customization) == nil) then
self:setVehicleState(nveh, self:getVehicleCustomization(nveh))
else
self:setVehicleState(nveh, state)
end
-- Mark the model as no longer needed and trigger event
SetModelAsNoLongerNeeded(mhash)
vRP:triggerEvent("VehicleVehicleSpawn", model)
end
end
- moved local custom = {} to be above function Vehicle:getVehicleCustomization(veh).
not needed above that function
-- VEHICLE STATE
local custom = {}
function Vehicle:getVehicleCustomization(veh)
server.lua
- payment system not currently implemented in vrp3 just yet.
- altered local state. set them to either vstate.customization or {} if table and default false on locked
- didnt implement de-spawn function on sell.
local function m_buy(menu, model)
local user = menu.user
local uvehicles = user:getVehicles()
local veh = menu.data.vehicles[model]
if not veh then
return vRP.EXT.Base.remote._notify(user.source, "Vehicle not found.")
end
-- Uncomment when implementing payment
-- if not user:tryPayment(veh.price) then
-- return vRP.EXT.Base.remote._notify(user.source, "Not enough money")
-- end
local shop_cfg = self.cfg.vehicleshops[menu.data.shop_key]
if not shop_cfg or not shop_cfg.purchaseSpawn or #shop_cfg.purchaseSpawn == 0 then
return vRP.EXT.Base.remote._notify(user.source, "Vehicle shop configuration error.")
end
local spawn = shop_cfg.purchaseSpawn[math.random(#shop_cfg.purchaseSpawn)]
if not spawn or spawn.x == 0 then
return vRP.EXT.Base.remote._notify(user.source, "No valid spawn point configured.")
end
vRP.EXT.Base.remote.teleport(user.source, spawn.x, spawn.y, spawn.z, spawn.w)
-- Build and apply state
local vstate = user:getVehicleState(model)
local state = {
customization = vstate.customization or {},
condition = vstate.condition or {},
locked = vstate.locked or false
}
uvehicles[model] = 0
self.remote._spawnVehicle(user.source, model, state)
self.remote._setOutVehicles(user.source, { [model] = {} })
vRP.EXT.Base.remote._notify(user.source, "Purchased for $" .. veh.price)
user:actualizeMenu()
user:closeMenu(menu)
end
local function m_sell(menu, model)
local user = menu.user
local uvehicles = user:getVehicles()
local veh = self:getVehicleByModel(model)
if veh and uvehicles[model] then
local zone_type = menu.data.zone_type
local sell_cfg = self.cfg.sellvehicle[zone_type]
if sell_cfg then
local price = math.floor((veh.price or 0) * (sell_cfg.sellPrice / 100))
uvehicles[model] = nil
--user:addWallet(price) -- no current implementation
-- despawn vehicles
self.remote._despawnVehicle(user.source, model)
vRP.EXT.Base.remote._notify(user.source, "Vehicle sold for $" .. price)
user:actualizeMenu()
else
vRP.EXT.Base.remote._notify(user.source, "Invalid sell zone.")
end
else
vRP.EXT.Base.remote._notify(user.source, "You do not own this vehicle.")
end
end
Member
|
@IanisCatalin15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vehicle Shop + Sell Car + State