Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
if not LoadResourceFile(GetCurrentResourceName(), 'web/build/index.html') then
print('^1[SD-DIALOG] ERROR: web/build folder not found!^0')
print('^1[SD-DIALOG] You likely downloaded the source code instead of the release.^0')
print('^1[SD-DIALOG] Please download the latest release from: https://github.com/Samuels-Development/sd-dialog/releases/latest^0')
print(
'^1[SD-DIALOG] Please download the latest release from: https://github.com/Samuels-Development/sd-dialog/releases/latest^0')
return
end

local Config = require 'config'

local isDialogOpen = false --- Whether dialog is currently open
local currentDialog = nil --- Current dialog data
local originalOptions = nil --- Original options with functions intact
local dialogCamera = nil --- Camera handle
local isDialogOpen = false --- Whether dialog is currently open
local currentDialog = nil --- Current dialog data
local originalOptions = nil --- Original options with functions intact
local dialogCamera = nil --- Camera handle
local originalCamCoords = nil --- Original camera position for restoration
local originalCamRot = nil --- Original camera position for restoration
local currentCallback = nil --- Current callback for option selection
local focusedEntity = nil --- Entity being focused on
local entityDialogs = {} --- Store dialog data for entities and models
local modelDialogs = {} --- Store dialog data for entities and models
local originalCamRot = nil --- Original camera position for restoration
local currentCallback = nil --- Current callback for option selection
local focusedEntity = nil --- Entity being focused on
local entityDialogs = {} --- Store dialog data for entities and models
local modelDialogs = {} --- Store dialog data for entities and models

--- Creates and activates the dialog camera focused on the NPC
--- @param entity number Entity to focus on
Expand All @@ -35,7 +36,8 @@ local function CreateDialogCamera(entity, transitionTime)
local rx = GetEntityRotation(entity, 2)
local camRotation = rx + vector3(0.0, 0.0, 181.0)

dialogCamera = CreateCamWithParams("DEFAULT_SCRIPTED_CAMERA", x, y, z, camRotation.x, camRotation.y, camRotation.z, 65.0)
dialogCamera = CreateCamWithParams("DEFAULT_SCRIPTED_CAMERA", x, y, z, camRotation.x, camRotation.y, camRotation.z,
65.0)
SetCamActive(dialogCamera, true)
RenderScriptCams(true, true, transitionTime, true, true)

Expand Down Expand Up @@ -82,6 +84,15 @@ local function OpenDialog(data, callback, rawOptions)

if data.entity and DoesEntityExist(data.entity) then
CreateDialogCamera(data.entity, data.transitionTime)

CreateThread(function()
local SetPlayerInvisibleLocally = SetPlayerInvisibleLocally;

while isDialogOpen do
SetPlayerInvisibleLocally(cache.playerId, true);
Wait(0)
end
end)
end

SetNuiFocus(true, true)
Expand Down Expand Up @@ -162,28 +173,29 @@ RegisterNUICallback('selectOption', function(data, cb)
if option then
if option.action and type(option.action) == 'function' then
option.action(option.params, option, currentDialog)

elseif option.clientEvent then
if type(option.clientEvent) == 'table' then
local eventName = option.clientEvent[1]
local args = {table.unpack(option.clientEvent, 2)}
local args = { table.unpack(option.clientEvent, 2) }
TriggerEvent(eventName, table.unpack(args))
else
TriggerEvent(option.clientEvent)
end

elseif option.serverEvent then
if type(option.serverEvent) == 'table' then
local eventName = option.serverEvent[1]
local args = {table.unpack(option.serverEvent, 2)}
local args = { table.unpack(option.serverEvent, 2) }
TriggerServerEvent(eventName, table.unpack(args))
else
TriggerServerEvent(option.serverEvent)
end

elseif option.onSelect then
option.onSelect(option, currentDialog)
end

if option.close then
CloseDialog()
end
end
end

Expand Down Expand Up @@ -323,7 +335,7 @@ end
---@param data table Dialog data configuration
---@return boolean Success
local function AddModelDialog(models, data)
local modelList = type(models) == 'table' and models or {models}
local modelList = type(models) == 'table' and models or { models }
local targetName = 'sd_dialog_model_' .. tostring(modelList[1])

for _, model in ipairs(modelList) do
Expand Down Expand Up @@ -368,7 +380,7 @@ end
---@param models table|string Model or array of models
---@return boolean Success
local function RemoveModelDialog(models)
local modelList = type(models) == 'table' and models or {models}
local modelList = type(models) == 'table' and models or { models }
local targetName = 'sd_dialog_model_' .. tostring(modelList[1])

for _, model in ipairs(modelList) do
Expand Down Expand Up @@ -397,8 +409,8 @@ local function SpawnConfigEntity(entityConfig, index)
entityConfig.coords.z, entityConfig.coords.w, false, true)

if not lib.waitFor(function()
if DoesEntityExist(ped) then return true end
end, 'Failed to create config ped', 5000) then
if DoesEntityExist(ped) then return true end
end, 'Failed to create config ped', 5000) then
SetModelAsNoLongerNeeded(entityConfig.model)
return
end
Expand Down