Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restarting scripts #656

Open
jaimeadf opened this issue Mar 2, 2022 · 0 comments
Open

Restarting scripts #656

jaimeadf opened this issue Mar 2, 2022 · 0 comments

Comments

@jaimeadf
Copy link
Contributor

jaimeadf commented Mar 2, 2022

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:

local custom_environment = {
    print = print,
    MESSAGE = "Hello, this is an overridden global variable."
}

-- Loads the code with the custom environment.
local execute = 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.
local thread_ids = {}

-- Wraps the FiveM's CreateThread and inserts the id to 'thread_ids' for every new thread.
function TrackedCreateThread(handler)
    CreateThread(function (id)
        table.insert(thread_ids, id)
        handler()
    end)
end

-- Declares an environment that overrides the 'CreateThread' to call 'TrackedCreateThread' instead.
local custom_environment = setmetatable(
    { CreateThread = TrackedCreateThread },
    { __index = _ENV }
)

-- Loads the code using the custom environment.
local execute = load(code, file, "bt", custom_environment)

execute()

-- When the resource is stopped, terminate all threads.
for index, thread_id in ipairs(thread_ids) do
    TerminateThread(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.

@vRP-framework vRP-framework deleted a comment from SenticAC Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant