Skip to content

Commit

Permalink
Removing music from cache if player is far away from them
Browse files Browse the repository at this point in the history
  • Loading branch information
Xogy committed Dec 4, 2022
1 parent c25d85a commit 5a9f59c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
14 changes: 12 additions & 2 deletions client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ RegisterNUICallback("data_status", function(data)
TriggerEvent("xSound:songStopPlaying", data.id)
end
if data.type == "maxDuration" then
soundInfo[data.id].timeStamp = 0
if not soundInfo[data.id].SkipTimeStamp then
soundInfo[data.id].timeStamp = 0
end
soundInfo[data.id].maxDuration = data.time

soundInfo[data.id].SkipTimeStamp = nil
end
end
end)
Expand All @@ -25,9 +29,15 @@ RegisterNUICallback("events", function(data)
end
if type == "onPlay" then
if globalOptionsCache[id] then
if globalOptionsCache[id].onPlayStart then
if globalOptionsCache[id].onPlayStartSilent then
globalOptionsCache[id].onPlayStartSilent(getInfo(id))
end

if globalOptionsCache[id].onPlayStart and not soundInfo[id].SkipEvents then
globalOptionsCache[id].onPlayStart(getInfo(id))
end

soundInfo[id].SkipEvents = nil
end
end
if type == "onEnd" then
Expand Down
9 changes: 7 additions & 2 deletions client/exports/events.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function onPlayStart(name, delegate)
function onPlayStart(name, delegate, imporant)

This comment has been minimized.

Copy link
@JonasDev17

JonasDev17 Dec 6, 2022

Typo important?

This comment has been minimized.

Copy link
@Xogy

Xogy Dec 6, 2022

Author Owner

Typo important?

yeah I forgot to remove this thanks for reminding me. + yep its a typo.

globalOptionsCache[name].onPlayStart = delegate
globalOptionsCache[name].imporant = imporant
end

exports('onPlayStart', onPlayStart)
Expand All @@ -26,4 +27,8 @@ function onPlayResume(name, delegate)
globalOptionsCache[name].onPlayResume = delegate
end

exports('onPlayResume', onPlayResume)
exports('onPlayResume', onPlayResume)

function onPlayStartSilent(name, delegate)
globalOptionsCache[name].onPlayStartSilent = delegate
end
8 changes: 8 additions & 0 deletions client/exports/manipulation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ function Destroy(name_)
globalOptionsCache[name_] = nil
end

function DestroySilent(name)
SendNUIMessage({
status = "delete",
name = name
})
end


exports('Destroy', Destroy)

function Resume(name_)
Expand Down
15 changes: 15 additions & 0 deletions client/exports/play.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ end

exports('PlayUrlPos', PlayUrlPos)

function PlayUrlPosSilent(name_, url_, volume_, pos, loop_)
if disableMusic then return end
SendNUIMessage({
status = "url",
name = name_,
url = url_,
x = pos.x,
y = pos.y,
z = pos.z,
dynamic = true,
volume = volume_,
loop = loop_ or false,
})
end

--function TextToSpeech(name_, lang, text, volume_)
-- if disableMusic then return end
-- SendNUIMessage({
Expand Down
49 changes: 47 additions & 2 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getDefaultInfo()
url = "",
id = "",
position = nil,
distance = 0,
distance = 10,
playing = false,
paused = false,
loop = false,
Expand Down Expand Up @@ -93,7 +93,7 @@ CreateThread(function()
while true do
Wait(1000)
for k, v in pairs(soundInfo) do
if v.playing then
if v.playing or v.wasSilented then
if getInfo(v.id).timeStamp ~= nil and getInfo(v.id).maxDuration ~= nil then
if getInfo(v.id).timeStamp < getInfo(v.id).maxDuration then
getInfo(v.id).timeStamp = getInfo(v.id).timeStamp + 1
Expand All @@ -102,4 +102,49 @@ CreateThread(function()
end
end
end
end)

function PlayMusicFromCache(data)
local musicCache = soundInfo[data.id]
if musicCache then
musicCache.SkipEvents = true
musicCache.SkipTimeStamp = true

PlayUrlPosSilent(data.id, data.url, data.volume, data.position, data.loop)
onPlayStartSilent(data.id, function()
if getInfo(data.id).maxDuration then
setTimeStamp(data.id, data.timeStamp or 0)
end
Distance(data.id, data.distance)
end)
end
end

-- If player is far away from music we will just delete it.
CreateThread(function()
local ped = PlayerPedId()
local playerPos = GetEntityCoords(ped)
local destroyedMusicList = {}
while true do
Wait(500)
ped = PlayerPedId()
playerPos = GetEntityCoords(ped)
for k, v in pairs(soundInfo) do
if v.position ~= nil and v.isDynamic then
if #(v.position - playerPos) < (v.distance + config.distanceBeforeUpdatingPos) then
if destroyedMusicList[v.id] then
destroyedMusicList[v.id] = nil
v.wasSilented = true
PlayMusicFromCache(v)
end
else
if not destroyedMusicList[v.id] then
destroyedMusicList[v.id] = true
v.wasSilented = false
DestroySilent(v.id)
end
end
end
end
end
end)

0 comments on commit 5a9f59c

Please sign in to comment.