Saints Row IV Some questions about SRIV Lua threads

So let's consider the following function which loops through a complex code every 5 seconds.
Code:
function something_random ()
 while ( true ) do
  code block
  delay(5)
 end
end

We want this function to monitor something so let's make a thread out of it.

Code:
something_random_handle = INVALID_THREAD_HANDLE
something_random_handle = thread_new("something_random")

And my dilemma starts here:If for example I want the thread to simply cease to exist what is the best solution for it ? There are some functions regarding the killing of threads but I cannot find the difference between them,for example:

Code:
thread_kill(something_random_handle)
cleanup_threads(something_random_handle)

Does executing the following commands simply kills of the function without going through the block of code and without waiting for that 5 second delay or just simply breaks the while statement after the block of code + the 5 second delay executes?
 
I don't recall just how it works in Lua, or the embedded engine in the game, but if this were another language i would say make it "while ( some_variable ) do" and then set some_variable to false in your main thread or wherever, when you want the thread to end. In Java, that's really your only option.
 
I don't recall just how it works in Lua, or the embedded engine in the game, but if this were another language i would say make it "while ( some_variable ) do" and then set some_variable to false in your main thread or wherever, when you want the thread to end. In Java, that's really your only option.

That sounds like a great idea and I think I'm gonna stick with it but I still wonder what's the purpose of those functions.

EDIT:Wait but if I set that some_variable to false does the while statement take a break in the middle of the code block or just ends the function?
 
Calling 'thread_kill' will prevent the thread from being executed any more. There's no way it could have any effect on the while loop since the threading code couldn't possibly know the internals of the thread. I don't know the exact details of 'cleanup_threads' but that might be related to the script side of the mission frameworks.

As an aside, Lua threads aren't really "threads" they're what are called co-routines. Each one runs until it completes or hits something that yields (like delay or other blocking functions), then the next one runs. So when 'thread_kill' is called it marks thread and it's just cleaned up instead of running again.
 
Thanks for the prompt answer,but what if I do the following:

something_random_handle = thread_new("something_random")

And after some time when a certain condition is met this following code executes:

something_random_handle = INVALID_THREAD_HANDLE

Is the same thing happening as if I was using thread_kill(something_random_handle) ?
 
No, that will just reassign your local variable to INVALID_THREAD_HANDLE. It won't stop the thread, which continues to exist independently of the variable. something_random_handle is just a pointer/handle to the thread/co-routine object.

Wait but if I set that some_variable to false does the while statement take a break in the middle of the code block or just ends the function?
It will continue executing until the next time it reaches the end of the while loop, at which point it evaluates the condition (which is now false) to determine whether it should execute again. It will then skip past the bottom of the while loop and continue from there. Since you have nothing beyond the end of the while loop, the thread will have run out of code and will end.
 
Back
Top