All your code runs on a
thread, when your script first starts it's on the main thread, and all the code runs on that thread. However... let's say you wanted to make multiple codes run concurrently (at the same time), you could do this by making another thread, as all threads do run concurrently. This can be done in Bully LUA by simply using the function
CreateThread(func). You just put a function name in the () to specify what function should run in the newly created thread. Here is an example:
main = function()
CreateThread("F_Function2")
repeat
TextPrintString("Thread #1 Running",1,1)
Wait(0)
until PedIsDead(gPlayer)
end
F_Function2 = function()
repeat
TextPrintString("Thread #2 Running",1,2)
Wait(0)
until PedIsDead(gPlayer)
end
As you can see, CreateThread is called with "F_Function2" in the parenthesis in the start of the main function, so a new thread is created and the function F_Function2 is running in it. Since it's in it's own thread, it runs at the same time as the function "main" which is in the main thread. Now, both repeat loops are running at the same time, which you can see by testing the script and seeing that both the TextPrintStrings are printing text. (Note, this script won't work how it is now. MissionSetup and MissionCleanup need to be added, if compiling to ArcRace1.lur for example)
Make sure not to put CreateThread inside the repeat loop of main(), or any loop for that matter (unless you have good reason to do so) because if you do it will just keep creating threads, which may eventually result in a crash because of there being too many threads. I'm not sure what the limit is, but it's probably enough to where you don't have to worry about it. I personally only make like 3 or 4 at the most in a single script.
Using threads can really help code that needs multiple repeat loops. Maybe for example, one loop could just have a Wait(0) in it as the code needs to run quickly, but another could have a Wait(1000) and keep track of time accurately, by counting seconds.
Threads in Bully cannot be created with arguments, use global variables if you need to pass extra information to your threads.