Project Zomboid

Project Zomboid

Autotsar Trailers - Hauler
eromanov87 2 Dec, 2024 @ 8:36pm
Vehicle randomization on unload.
Would it be possible to save the vehicle data when running the context script to load the vehicle into a table and then use that saved data to unload the same vehicle again?

something like:
-- Table to store vehicle data ATA_ISVehicleMenu.savedVehicleData = {} -- Save vehicle data when loading onto the trailer function ATA_ISVehicleMenu.loadOntoTrailer(playerObj, vehicle, trailer) -- Create a table to store vehicle data local vehicleData = { type = vehicle:getScriptName(), condition = vehicle:getCondition(), fuel = vehicle:getFuel(), inventory = {}, -- Placeholder for inventory items } -- Save inventory items local container = vehicle:getPartById("Base.Container") if container then for i = 0, container:getItems():size() - 1 do local item = container:getItems():get(i) table.insert(vehicleData.inventory, { type = item:getType(), count = item:getCount() }) end end -- Save the data to the savedVehicleData table ATA_ISVehicleMenu.savedVehicleData[vehicle:getId()] = vehicleData -- Proceed with the original loading logic (if any) -- ... end -- Restore vehicle data when launching from the trailer function ATA_ISVehicleMenu.launchVehicle(playerObj, vehicle, wreckerinfo) local savedData = ATA_ISVehicleMenu.savedVehicleData[vehicle:getId()] if not savedData then -- If no data is saved, spawn a default vehicle -- Existing spawn logic here return end -- Use saved data to recreate the vehicle local spawnedVehicle = spawnVehicleAtPoint(savedData.type, vehicle:getSquare()) spawnedVehicle:setCondition(savedData.condition) spawnedVehicle:setFuel(savedData.fuel) -- Restore inventory items local container = spawnedVehicle:getPartById("Base.Container") if container then for _, itemData in ipairs(savedData.inventory) do for i = 1, itemData.count do container:addItem(itemData.type) end end end -- Remove saved data after spawning ATA_ISVehicleMenu.savedVehicleData[vehicle:getId()] = nil -- Additional spawning logic (if needed) -- ... end