1- --- compat for https://github.com/darktrovx/interact
2-
3- -- Utility function to generate UUIDs (for compatibility with the second script)
41local function generateUUID ()
52 return (' xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' ):gsub (' [xy]' , function (c )
63 local v = (c == ' x' ) and math.random (0 , 0xf ) or math.random (8 , 0xb )
74 return (' %x' ):format (v )
85 end )
96end
107
11- -- Export handler to register functions as exports
128local function exportHandler (exportName , func )
139 AddEventHandler ((' __cfx_export_interact_%s' ):format (exportName ), function (setCB )
1410 setCB (func )
1511 end )
1612end
1713
18- -- Convert options from the second script to sleepless_interact's Option structure
19- local function convertOptions (options , resource , id )
14+ local function convert (data , resource )
2015 local converted = {}
21- for _ , option in ipairs (options ) do
16+ local id = data .id or generateUUID ()
17+ for i = 1 , # data .options do
18+ local option = data .options [i ]
2219 local newOption = {
2320 label = option .label or " Unnamed Option" ,
2421 icon = option .icon ,
2522 iconColor = option .iconColor ,
26- distance = option . distance ,
23+ distance = data . interactDst or 1.0 ,
2724 canInteract = option .canInteract ,
28- name = option .name or id ,
25+ groups = data .groups ,
26+ name = id ,
2927 resource = resource ,
30- offset = option .offset ,
31- bones = option .bone , -- Map 'bone' to 'bones' in sleepless_interact
32- onSelect = option .onSelect ,
33- cooldown = option .cooldown ,
34- export = option .export ,
28+ offset = data .offset ,
29+ bones = (data .bone and { data .bone }) or nil ,
30+ onSelect = option .action ,
31+ cooldown = 1000 ,
3532 event = option .event ,
3633 serverEvent = option .serverEvent ,
37- command = option .command
3834 }
39- -- Handle groups (jobs) if present
40- if option .groups or option .job then
41- newOption .groups = option .groups or option .job
42- end
4335 converted [# converted + 1 ] = newOption
4436 end
45- return converted
37+ return converted , id
4638end
4739
48- -- AddInteraction (coords-based)
4940exportHandler (' AddInteraction' , function (data )
5041 local coords = data .coords
51- local options = convertOptions (data . options , GetInvokingResource ())
42+ local options = convert (data , GetInvokingResource ())
5243 local id = interact .addCoords (coords , options )
53- return id -- Returns the coordinate-based ID from sleepless_interact
44+ return id
5445end )
5546
56- -- AddLocalEntityInteraction
5747exportHandler (' AddLocalEntityInteraction' , function (data )
5848 local entity = data .entity
59- local options = convertOptions (data .options , GetInvokingResource ())
60- interact .addLocalEntity ({ entity }, options )
61- -- Generate a UUID since addLocalEntity doesn't return an ID
62- return generateUUID ()
49+ local options , id = convert (data , GetInvokingResource ())
50+ interact .addLocalEntity (entity , options )
51+ return id
6352end )
6453
65- -- AddEntityInteraction (networked entity)
6654exportHandler (' AddEntityInteraction' , function (data )
6755 local netId = data .netId
68- local options = convertOptions (data .options , GetInvokingResource ())
69- interact .addEntity ({ netId }, options )
70- -- Generate a UUID since addEntity doesn't return an ID
71- return generateUUID ()
56+ local options , id = convert (data , GetInvokingResource ())
57+ interact .addEntity (netId , options )
58+ return id
7259end )
7360
74- -- AddGlobalVehicleInteraction
7561exportHandler (' AddGlobalVehicleInteraction' , function (data )
76- local options = convertOptions (data . options , GetInvokingResource ())
62+ local options , id = convert (data , GetInvokingResource ())
7763 interact .addGlobalVehicle (options )
78- -- Generate a UUID since addGlobalVehicle doesn't return an ID
79- return generateUUID ()
64+ return id
8065end )
8166
82- -- AddGlobalPlayerInteraction
8367exportHandler (' addGlobalPlayerInteraction' , function (data )
84- local options = convertOptions (data . options , GetInvokingResource ())
68+ local options , id = convert (data , GetInvokingResource ())
8569 interact .addGlobalPlayer (options )
86- -- Generate a UUID since addGlobalPlayer doesn't return an ID
87- return generateUUID ()
70+ return id
8871end )
8972
90- -- AddModelInteraction
9173exportHandler (' AddModelInteraction' , function (data )
9274 local model = data .model
93- local options = convertOptions (data . options , GetInvokingResource ())
75+ local options , id = convert (data , GetInvokingResource ())
9476 interact .addModel (model , options )
95- -- Generate a UUID since addModel doesn't return an ID
96- return generateUUID ()
77+ return id
9778end )
9879
99- -- RemoveInteraction (by ID)
10080exportHandler (' RemoveInteraction' , function (id )
101- interact .removeCoords (id , nil , true ) -- Remove coords-based interaction by ID
81+ interact .removeCoords (id , nil , true )
10282end )
10383
104- -- RemoveLocalEntityInteraction
10584exportHandler (' RemoveLocalEntityInteraction' , function (entity , id )
106- interact .removeLocalEntity ({ entity } , id )
85+ interact .removeLocalEntity (entity , id )
10786end )
10887
109- -- RemoveEntityInteraction
11088exportHandler (' RemoveEntityInteraction' , function (netId , id )
111- interact .removeEntity ({ netId } , id )
89+ interact .removeEntity (netId , id )
11290end )
11391
114- -- RemoveModelInteraction
11592exportHandler (' RemoveModelInteraction' , function (model , id )
11693 interact .removeModel (model , id )
11794end )
11895
119- -- RemoveGlobalVehicleInteraction
12096exportHandler (' RemoveGlobalVehicleInteraction' , function (id )
12197 interact .removeGlobalVehicle (id )
12298end )
12399
124- -- RemoveGlobalPlayerInteraction
125100exportHandler (' RemoveGlobalPlayerInteraction' , function (id )
126101 interact .removeGlobalPlayer (id )
127- end )
102+ end )
103+
104+
105+
106+ -- local id = exports.interact:AddInteraction({
107+ -- coords = GetEntityCoords(cache.ped),
108+ -- distance = 8.0, -- optional
109+ -- interactDst = 1.0, -- optional
110+ -- id = 'myCoolUniqueId', -- needed for removing interactions
111+ -- name = 'interactionName', -- optional
112+ -- options = {
113+ -- {
114+ -- label = 'Hello World!',
115+ -- action = function(entity, coords, args)
116+ -- print(entity, coords, json.encode(args))
117+ -- end,
118+ -- },
119+ -- }
120+ -- })
121+
122+ -- SetTimeout(3000, function()
123+ -- exports.interact:RemoveInteraction(id)
124+ -- end)
0 commit comments