GE Lua Documentation

Press F to search!

getClosestVehicle

Definition


-- @/lua/ge/ge_utils.lua:571

function getClosestVehicle(requesterID, callbackfct)
  local vehr = getObjectByID(requesterID)
  if not vehr then return end
  local pos1 = vec3(vehr:getPosition())

  local minDist = 9999999
  local minVehId = nil
  for tid, veh in activeVehiclesIterator() do
    if tid ~= requesterID then
      local pos2 = veh:getPosition()
      local dist = (pos1 - pos2):length()
      if dist < minDist then
        minDist = dist
        minVehId = tid
      end
    end
  end
  if not minVehId then
  vehr:queueLuaCommand(callbackfct .. '(-1, -1)')
  else
  vehr:queueLuaCommand(callbackfct .. '(' .. minVehId .. ',' .. minDist .. ')')
  end
end

Callers