You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The missing ability to restart scripts on the fly seems to keep some developers away from developing for vRP 2. This got me thinking of what could be done to implement this feature and still maintain compatibility. So, I've done a little research and I would like to share it.
Firstly, the main challenges I identified are:
Terminate the threads.
Unregister all the event handlers.
Let the resource release other things it may have used on its own.
Replace the extension.
Threads
My idea is to keep track of all the thread ids to terminate them using TerminateThread when the resource is stopped. The lua load function allows us to use a custom environment when executing a code and, therefore, lets us override the methods. For example:
localcustom_environment= {
print=print,
MESSAGE="Hello, this is an overridden global variable."
}
-- Loads the code with the custom environment.localexecute=load([[ print(MESSAGE) ]], "code.lua", "bt", custom_environment)
execute()
With that in mind, I've made a small sample of how it could be implemented.
-- Creates table to keep track of the threads created by the script.localthread_ids= {}
-- Wraps the FiveM's CreateThread and inserts the id to 'thread_ids' for every new thread.functionTrackedCreateThread(handler)
CreateThread(function (id)
table.insert(thread_ids, id)
handler()
end)
end-- Declares an environment that overrides the 'CreateThread' to call 'TrackedCreateThread' instead.localcustom_environment=setmetatable(
{ CreateThread=TrackedCreateThread },
{ __index=_ENV }
)
-- Loads the code using the custom environment.localexecute=load(code, file, "bt", custom_environment)
execute()
-- When the resource is stopped, terminate all threads.forindex, thread_idinipairs(thread_ids) doTerminateThread(thread_id)
end
A similar approach could be done to the events.
Sadly, I don't have many ideas to solve the other issues. Though, I'd love to hear some feedback and know what you guys think and solutions to the other challenges.
By the way, I am aware that this is a duplicate of #516, but I've decided to keep it separate.
The text was updated successfully, but these errors were encountered:
The missing ability to restart scripts on the fly seems to keep some developers away from developing for vRP 2. This got me thinking of what could be done to implement this feature and still maintain compatibility. So, I've done a little research and I would like to share it.
Firstly, the main challenges I identified are:
Threads
My idea is to keep track of all the thread ids to terminate them using TerminateThread when the resource is stopped. The
lua
load function allows us to use a custom environment when executing a code and, therefore, lets us override the methods. For example:With that in mind, I've made a small sample of how it could be implemented.
A similar approach could be done to the events.
Sadly, I don't have many ideas to solve the other issues. Though, I'd love to hear some feedback and know what you guys think and solutions to the other challenges.
By the way, I am aware that this is a duplicate of #516, but I've decided to keep it separate.
The text was updated successfully, but these errors were encountered: