Anim


Animation used for when the MDT is open/underlayed

local options = {
  base = {
    animation = 'base',
    dictionary = 'amb@world_human_tourist_map@male@base',
    flags = { loop = true, move = false },
    prop = {
      bone = 28422, -- Left hand
      model = 'drx_mdt_tablet',
      placement = {
        pos = vector3(0.0, 0.0, 0.0), -- Offset from bone
        rot = vector3(0.0, 0.0, 0.0), -- Rotation from bone
      }
    }
  },
  idle = {
    animation = 'idle_a',
    dictionary = 'amb@code_human_in_bus_passenger_idles@female@tablet@idle_a',
    flags = { loop = true, move = true },
    prop = {
      bone = 28422,
      model = 'drx_mdt_tablet',
      placement = {
        pos = vector3(-0.02, 0.0, 0.0),
        rot = vector3(0.0, 0.0, 0.0),
      }
    }
  },
  clear = false
}

local currentProp = nil

---@param flags table|nil
---@return number - animation flags
local function getAnimFlags(flags)
  if not flags then return 1 end
  local f = 1
  if flags.loop then f = f + 16 end
  if flags.move then f = f + 32 end
  return f
end

---@param name string  ("base", "idle", "clear")
function playTabletAnim(name)
  if name == "clear" or not options[name] then
    clearTabletAnim()
    return
  end

  local data = options[name]
  local ped = PlayerPedId()

  clearTabletAnim()

  RequestAnimDict(data.dictionary)
  while not HasAnimDictLoaded(data.dictionary) do Wait(0) end
  TaskPlayAnim(ped, data.dictionary, data.animation, 8.0, -8.0, -1, getAnimFlags(data.flags), 0, false, false, false)

  if data.prop then
    local model = GetHashKey(data.prop.model)
    if model == 0 then
      print(("ERROR: Model '%s' is invalid!"):format(data.prop.model))
      return
    end

    RequestModel(model)
    local timeout = 0
    while not HasModelLoaded(model) do
      Wait(50)
      timeout = timeout + 50
      if timeout > 5000 then
        print(("ERROR: Model '%s' failed to load after 5 seconds."):format(data.prop.model))
        return
      end
    end

    local obj = CreateObject(model, 1.0, 1.0, 1.0, true, true, false)
    local bone = data.prop.bone or 28422
    local placement = data.prop.placement or {}
    local pos = placement.pos or vector3(0,0,0)
    local rot = placement.rot or vector3(0,0,0)
    AttachEntityToEntity(
      obj, ped, GetPedBoneIndex(ped, bone),
      pos.x, pos.y, pos.z,
      rot.x, rot.y, rot.z,
      false, false, false, false, 2, true
    )
    currentProp = obj
  end
end

function clearTabletAnim()
  local ped = PlayerPedId()
  ClearPedTasks(ped)
  if currentProp and DoesEntityExist(currentProp) then
    DeleteEntity(currentProp)
    currentProp = nil
  end
end

RegisterNetEvent('drx_mdt:client:setTabletAnim', function(type)
  playTabletAnim(type)
end)

AddEventHandler('onResourceStop', function(res)
  if res == GetCurrentResourceName() then
    clearTabletAnim()
  end
end)

Last updated

Was this helpful?