Fix Spawning and Better Saving

This commit is contained in:
snoutie 2025-02-03 12:07:34 +01:00
parent 3aaaa3a15f
commit 9517395e1e

View File

@ -18,49 +18,79 @@ local M = {}
M.dependencies = { 'career_career', 'career_modules_computer', 'career_modules_inventory', 'career_modules_payment',
'career_modules_playerAttributes' }
local function tprint(tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
local function getVehicleSavesFolder(savePath)
return savePath .. "/career/_vehicle_saves"
end
local function getInventoryIdFolder(vehicleSavesPath, inventoryId)
return vehicleSavesPath .. "/" .. inventoryId
end
local function getInventoryIdFile(inventoryIdPath)
return inventoryIdPath .. "/save.json"
end
local function createFolderIfNeeded(path)
if not FS:directoryExists(path) then
FS:directoryCreate(path)
end
end
local function prepareFolders(inventoryId)
local _, savePath = career_saveSystem.getCurrentSaveSlot()
local vehicleSavesFolder = getVehicleSavesFolder(savePath)
createFolderIfNeeded(vehicleSavesFolder)
local inventoryIdFolder = getInventoryIdFolder(vehicleSavesFolder, inventoryId)
createFolderIfNeeded(inventoryIdFolder)
return getInventoryIdFile(inventoryIdFolder)
end
local function onSave(inventoryId)
local fileName = prepareFolders(inventoryId)
local vehicleId = career_modules_inventory.getVehicleIdFromInventoryId(inventoryId)
if vehicleId then
local object = be:getObjectByID(vehicleId)
object:queueLuaCommand('beamstate.save("' .. fileName .. '")')
end
end
local function onLoad(inventoryId)
local fileName = prepareFolders(inventoryId)
local vehicleId = career_modules_inventory.getVehicleIdFromInventoryId(inventoryId)
if vehicleId then
local object = be:getObjectByID(vehicleId)
object:queueLuaCommand('beamstate.load("' .. fileName .. '")')
end
end
local function spawnVehicle(inventoryId, callback)
if career_modules_inventory.getVehicleIdFromInventoryId(inventoryId) then
callback()
else
career_modules_inventory.spawnVehicle(inventoryId, nil, callback)
end
toprint = toprint .. string.rep(" ", indent - 2) .. "}"
return toprint
end
local function retrieve_favourite()
local fav_veh_id = career_modules_inventory.getFavoriteVehicle()
local vid = career_modules_inventory.getVehicleIdFromInventoryId(fav_veh_id)
if vid then
local old_veh = be:getObjectByID(vid)
old_veh:queueLuaCommand("beamstate.save()")
end
extensions.core_jobsystem.create(
function(job)
job.sleep(0.2)
local veh = career_modules_inventory.spawnVehicle(fav_veh_id, 2)
local vehh = be:getObjectByID(career_modules_inventory.getVehicleIdFromInventoryId(fav_veh_id))
local location = { pos = vehh:getPosition(), rot = quat(0, 0, 1, 0) * quat(vehh:getRefNodeRotation()) }
local garage = career_modules_inventory.getClosestGarage(location.pos)
freeroam_facilities.teleportToGarage(garage.id, vehh, false)
veh:queueLuaCommand("beamstate.load()")
onSave(fav_veh_id)
job.sleep(1)
spawnVehicle(fav_veh_id, function()
local veh = be:getObjectByID(career_modules_inventory.getVehicleIdFromInventoryId(fav_veh_id))
local location = { pos = veh:getPosition(), rot = quat(0, 0, 1, 0) * quat(veh:getRefNodeRotation()) }
local garage = career_modules_inventory.getClosestGarage(location.pos)
onLoad(fav_veh_id)
job.sleep(1)
freeroam_facilities.teleportToGarage(garage.id, veh, false)
end)
end
)
end
@ -84,10 +114,7 @@ end
local function onSaveCurrentSaveSlot(currentSavePath, oldSaveDate, vehiclesThumbnailUpdate)
local vs = career_modules_inventory.getVehicles()
for id, _ in pairs(vs) do
local vid = career_modules_inventory.getVehicleIdFromInventoryId(id)
local veh = be:getObjectByID(vid)
veh:queueLuaCommand("beamstate.save()")
onSave(id)
end
end