Skip to content

Commit

Permalink
feat: add new global type players #1
Browse files Browse the repository at this point in the history
  • Loading branch information
swkeep committed Feb 7, 2024
1 parent a7a74f1 commit 92aae4d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 9 deletions.
14 changes: 13 additions & 1 deletion interactionMenu/lua/client/interact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,28 @@ local function METADATA(t)
metadata.entityDetail = {
handle = t.entity,
networked = NetworkGetEntityIsNetworked(t.entity) == 1,
type = EntityTypes[GetEntityType(t.entity)],
model = t.model,
position = GetEntityCoords(t.entity),
rotation = GetEntityRotation(t.entity),
}

metadata.entityDetail.typeInt = GetEntityType(t.entity)
metadata.entityDetail.type = EntityTypes[metadata.entityDetail.typeInt]

if metadata.entityDetail.networked then
metadata.entityDetail.netId = NetworkGetNetworkIdFromEntity(t.entity)
end

local isPlayer = metadata.entityDetail.typeInt == 1 and IsPedAPlayer(t.entity)

if isPlayer then
metadata.player = {
playerPedId = t.entity,
playerIndex = NetworkGetPlayerIndexFromPed(t.entity),
}
metadata.player.serverId = GetPlayerServerId(metadata.player.playerIndex)
end

return metadata
end

Expand Down
30 changes: 23 additions & 7 deletions interactionMenu/lua/client/menuContainer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Container = {
entities = {},
objects = {},
peds = {},
players = {},
vehicles = {}
}
}
Expand Down Expand Up @@ -349,6 +350,7 @@ function Container.createGlobal(t)
Container.indexes.globals['bones'][instance.bone] = Container.indexes.globals['bones'][instance.bone] or {}
table.insert(Container.indexes.globals['bones'][instance.bone], id)
end

return id
end

Expand Down Expand Up @@ -398,21 +400,28 @@ end

local function mergeGlobals(combinedIds, entity, model, closestBoneName)
local entityType = GetEntityType(entity)
local isPlayer = entityType == 1 and IsPedAPlayer(entity)

if not entityType or entityType == 0 then return {} end
local globals = Container.indexes.globals
-- add globals on entities
if next(Container.indexes.globals.entities) then
Util.table_merge(combinedIds, Container.indexes.globals.entities or {})
if next(globals.entities) then
Util.table_merge(combinedIds, globals.entities or {})
end

-- add globals on, peds vehicles and objects
local res = next(Container.indexes.globals[set[entityType]])
local res = next(globals[set[entityType]])
if res then
Util.table_merge(combinedIds, Container.indexes.globals[set[entityType]] or {})
Util.table_merge(combinedIds, globals[set[entityType]] or {})
end

if isPlayer and globals['players'] and next(globals['players']) then
Util.table_merge(combinedIds, globals['players'] or {})
end

-- add globals on bones
if closestBoneName then
Util.table_merge(combinedIds, Container.indexes.globals.bones[closestBoneName] or {})
Util.table_merge(combinedIds, globals.bones[closestBoneName] or {})
end

-- on model and entity
Expand Down Expand Up @@ -508,15 +517,22 @@ function Container.getMenu(model, entity, menuId)
return container, { model, entity, closestBoneId }
end

local function globalsExistsCheck(entityType)
local function globalsExistsCheck(entity, entityType)
if not entityType or entityType == 0 then return false end
local globals = Container.indexes.globals
local specificGlobals = globals[set[entityType]]
local isPlayer = entityType == 1 and IsPedAPlayer(entity)

if isPlayer and globals['players'] and next(globals['players']) then
return true
end

if globals.entities and next(globals.entities) then
return true
end



if specificGlobals and next(specificGlobals) then
return true
end
Expand All @@ -534,7 +550,7 @@ function Container.getMenuType(t)
if t.closestPoint and next(t.closestPoint) then
-- onPosition
return MenuTypes['ON_POSITION']
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or globalsExistsCheck(entityType) then
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or globalsExistsCheck(entity, entityType) then
-- onModel / onEntity / onBone
return MenuTypes['ON_ENTITY']
else
Expand Down
20 changes: 19 additions & 1 deletion interactionMenu/lua/client/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,30 @@ function Util.spawnVehicle(vehicleModel, spawnPoint)
reqmodel(vehicleModel)

local id = #object + 1
object[id] = CreateVehicle(vehicleModel, spawnPoint.x, spawnPoint.y, spawnPoint.z, spawnPoint.w, true, false)
object[id] = CreateVehicle(vehicleModel, spawnPoint.x, spawnPoint.y, spawnPoint.z, spawnPoint.w, false, false)

SetModelAsNoLongerNeeded(vehicleModel)
return object[id]
end

function Util.spawnPed(hash, pos)
reqmodel(hash)

local id = #object + 1
object[id] = CreatePed(5, hash, pos.x, pos.y, pos.z, 0.0, false, true)
while not DoesEntityExist(object[id]) do Wait(10) end

SetEntityHeading(object[id], pos.w)
SetBlockingOfNonTemporaryEvents(object[id], true)
SetPedFleeAttributes(object[id], 0, false)
SetPedCombatAttributes(object[id], 46, true)
SetPedAlertness(object[id], 3)
SetModelAsNoLongerNeeded(object[id])
FreezeEntityPosition(object[id], true)

return object[id]
end

-- #endregion

-- DEVMODE raycast hit marker
Expand Down
38 changes: 38 additions & 0 deletions interactionMenu/lua/examples/globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,41 @@ exports['interactionMenu']:createGlobal {
}
}
}


exports['interactionMenu']:createGlobal {
type = 'players',
offset = vec3(0, 0, 0),
maxDistance = 1.0,
options = {
{
label = '[Debug] On All Players',
icon = 'fa fa-person',
action = {
type = 'sync',
func = function(data)
if not data.player then return end

local player = data.player

Util.print_table(player)
TriggerServerEvent('interaction-menu:server:syncAnimation', player.serverId)
end
}
}
}
}

local function playAnimation(ped, dict, name)
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Wait(50)
end
TaskPlayAnim(ped, dict, name, 8.0, -8.0, -1, 0, 0, false, false, false)
end

RegisterNetEvent('interaction-menu:client:syncAnimation', function()
playAnimation(PlayerPedId(), "random@mugging3", "handsup_standing_base")
Wait(4000)
ClearPedTasks(PlayerPedId())
end)
24 changes: 24 additions & 0 deletions interactionMenu/lua/examples/onEntites.lua
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,28 @@ CreateThread(function()
}
}
}

local ped_position = vector4(-1998.62, 3171.92, 31.81, 271.44)
Util.spawnPed(GetHashKey('cs_brad'), ped_position)

exports['interactionMenu']:Create {
entity = ent,
offset = vec3(0, 0, 0),
static = true,
options = {
{
label = 'Cook',
action = {
type = 'sync',
func = function(data)

end
}
}
}
}


ped_position = vector4(-1998.86, 3173.44, 31.81, 271.76)
Util.spawnPed(GetHashKey('a_m_y_business_02'), ped_position)
end)
5 changes: 5 additions & 0 deletions interactionMenu/lua/server/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RegisterNetEvent('interaction-menu:server:syncAnimation', function(target)
local src = source

TriggerClientEvent('interaction-menu:client:syncAnimation', target)
end)

0 comments on commit 92aae4d

Please sign in to comment.