GE Lua Documentation

Press F to search!

_spawnCCallback

Definition


-- @/lua/ge/extensions/core/vehicle/manager.lua:98

local function spawnCCallback(objID, vehicleDir, configDataIn, reloading)
  if profileVehicleLoading and simpleProfilerStart then
    simpleProfilerStart(false)
  end
  profilerPushEvent('spawn')
  local vehicleObj = scenetree.findObject(objID)
  if not vehicleObj then
    log('E', 'loader', 'Spawning vehicle failed, missing vehicle obj: '..dumps(objID, vehicleDir, configDataIn))
    additionalVehicleData = nil
    return
  end


  profilerPushEvent('spawn/materials')
  loadVehicleMaterialsDirectory(vehicleDir)
  loadVehicleMaterialsDirectory('/vehicles/common/')
  profilerPopEvent('spawn/materials')

  -- makes the object available for every call, etc
  be:addObject(vehicleObj, false)

  local timer = hptimer()
  local jbeamLoader = require("jbeam/loader")
  log('D', 'vehicleLoader', 'partConfigData [' .. type(configDataIn) .. '] = ' .. dumps(configDataIn))
  local vehicleConfig = extensions.core_vehicle_partmgmt.buildConfigFromString(vehicleDir, configDataIn)

  extensions.hook("onSpawnCCallback", objID)

  if additionalVehicleData and not additionalDataId or additionalDataId == objID then
    vehicleConfig.additionalVehicleData = additionalVehicleData
    additionalVehicleData = nil
  end

  if debugEnabled then
    vehicleConfig.additionalVehicleData = vehicleConfig.additionalVehicleData or {}
    vehicleConfig.additionalVehicleData.debugEnabled = debugEnabled
  end

  local luaVMType = 0 -- 0 = vehicle, 1 = object pool, 2 = none

  local vehicleBundle

  local status, err = xpcall(function () vehicleBundle = jbeamLoader.loadVehicleStage1(objID, vehicleDir, vehicleConfig) end, debug.traceback)
  vehicles[objID] = vehicleBundle

  if not vehicleBundle then
    log('E', 'loader', 'Spawning vehicle failed, missing stage 1 data: '..dumps(objID, vehicleDir, configDataIn))
    if err then log('E', 'loader', err) end
  end
  log('D', 'loader', "GE load time: " .. tostring(timer:stopAndReset() / 1000) .. ' s')

  --jsonWriteFile('jbeam_loading_NEW_stage1.json', vehicleBundle, true)
  local spawnPhysics = M.autoSpawnPhysics
  if vehicleObj.NoPhysics == 'true' then
    log('I', '', 'NOT spawning the physics ...')
    spawnPhysics = false
  end

  -- this will finish the 3d meshes and alike
  profilerPushEvent('finishConstructionGESide')
  vehicleObj:finishConstructionGESide()
  profilerPopEvent('finishConstructionGESide')

  vehicleSpawned(objID) -- callback to main function

  -- this enables the UI to react on the changed vehicle
  if vehicleBundle and vehicleBundle.vdata then
    guihooks.trigger('VehicleChange', vehicleBundle.vdata.vehicleDirectory, spawnPhysics)
  end

  if vehicleObj.autoEnterVehicle ~= "false" then -- and not reloading
    -- TODO: FIXME: do not call enterVehicle if the player is already in the vehicle or we reload: "and not reloading"
    local player = vehicleObj.autoEnterVehiclePlayer ~= "" and tonumber(vehicleObj.autoEnterVehiclePlayer) or 0
    be:enterVehicle(player, vehicleObj) -- will trigger onVehicleSwitched
  end
  vehicleObj:setDynDataFieldbyName("autoEnterVehicle", 0, "")
  vehicleObj:setDynDataFieldbyName("autoEnterVehiclePlayer", 0, "")

  if spawnPhysics then
    spawnPhysicsForVehicle(objID, vehicleObj, vehicleBundle)
  end

  -- remove the additionalVehicleData, because it's only supposed to be temporary
  if vehicleBundle and vehicleBundle.config then
    vehicleBundle.config.additionalVehicleData = nil
  end

  profilerPopEvent('spawn')

  if profileVehicleLoading and simpleProfilerStop then
    simpleProfilerStop()
    local vehName = vehicleObj and vehicleObj.jbeam or (vehicleBundle and vehicleBundle.vdata and vehicleBundle.vdata.vehicleDirectory) or 'Unknown'
    -- capture cache usage stats
    local jbeamStats = jbeamIO and jbeamIO.getLastStartLoadingStats and jbeamIO.getLastStartLoadingStats() or nil
    local jbeamCached = jbeamStats and jbeamStats.cachedHits > 0
    -- mesh cache summary via new API (may be nil if not available)
    local meshCacheSummary = nil
    if vehicleObj and vehicleObj.getMeshCacheRebuildSummary then
      local ok, summary = pcall(function() return vehicleObj:getMeshCacheRebuildSummary() end)
      if ok and type(summary) == 'table' then meshCacheSummary = summary end
    end
    extensions.utils_simpleProfiler_report.createReport('vehicle_loading', 'Vehicle Spawn', {
      vehicleName = vehName,
      jbeamCached = jbeamCached,
      meshCacheSummary = meshCacheSummary,
    })
    simpleProfilerDestroy()
  end
end

Callers