Bully-Board
Bully Modding Section => Bully Modding => TUTORIALS => Topic started by: DaBOSS54320 on November 10, 2014, 10:55:32 PM
-
All your code runs on a thread (https://en.wikipedia.org/wiki/Thread_(computing)), 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.
-
I see I see... Of course you are missing one piece of information there.
PedGivePotato(gLemon, 1)
-
can't understand. lel
But is it just for like if you make a new function that has a while/repeat loop?
eg:
function main()
CreateThread("F_TEST")
while true do
Wait(0)
end
end
function F_TEST(l_0_0)
repeat
TextPrintString("hi",1,1)
until not l_0_0
end
Would that work?
-
Almost, but no. You have a parameter in the F_Test function (The l_0_0 inside the ()) which would mean you'd have to pass an argument to the function when calling it for it to work. I actually forgot to include that in the main post, but to pass an argument through a function call you put the arguments in CreateThread right after. For example...
function main()
local Russell = PedCreateXYZ(75,270,-110,6) -- Create Russell in front of the boy's dorm
CreateThread("F_SetPedMoneyReward",Russell,500) -- Create a thread w/ the function "F_SetupPedMoneyReward" and pass arguments "Russell" and "500" to the function
while true do
-- main loop
Wait(0)
end
end
function F_SetPedMoneyReward(ped,money) -- "ped" and "money" are both parameters, and are thus local variables to this function
repeat -- Since this function is meant to be run on it's own thread, this "repeat" loop will run concurently to the "main" loop
Wait(0) -- Repeatedly wait until "ped" is dead
until PedIsDead(ped) -- "Russell" was passed into the function when called, so our parameter "ped" is the same as "Russell"
PlayerAddMoney(money * 100) -- "500" was passed into the function, multiplying by 100 converts it from cents to dollars
end -- The thread will automaticly be deleted once the function ends
Also... if anyone is confused about parameters and arguments: Parameters are the variables that get their values assigned to when the function is called, the names inside () where the function is declared. Arguments are the actually values that get passed into the parameters of the function, the values inside () where the function is called.
-
^ Ah! Now I am starting to understand. The l_0_0 is actually a boolean (http://www.lua.org/pil/2.2.html). So if the function is ever called, you would put like F_Test(true), then it would repeat until F_Test(false) is entered. I believe that is also how they can work. So to call it in an easier manner, can't you just put a new function:
function NTest(ped2, aom)
CreateThread("F_SetPedMoneyReward",ped2,aom)
end
Then, just call the NTest(true) func?
-
No, because you'd need to pass a value into both "ped2" and "aom". So you could do NTest(Edgar,400)
-
^ Yes, I edited the post and forgot to change true to the ped and amount of cash. If we did that, it would work?
-
Right, it should.
-
So... I messed up guys. After a bit of experimentation, I found out threads cannot take additional arguments.
CreateThread("func",arg) this won't work, nor will the money example I gave above.
I'm not sure why I thought they did take arguments, but I apologize for the confusion. If you want to pass information to your threads, you can still easily do so using global variables. (A global variable is a variable without "local" in front of it and can be used through out the entire script)