News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Threads  (Read 7213 times)

0 Members and 1 Guest are viewing this topic.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Threads
« on: November 10, 2014, 10:55:32 PM »
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:

Code: [Select]
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.
« Last Edit: July 06, 2015, 06:52:10 PM by DaBOSS54320 »

Offline WhenLifeGivesYouLemons

  • xfire: 1emonthatsme
  • Sr. Member
  • ***
  • Posts: 971
  • Gender: Male
  • 波動バースト
    • View Profile
Re: Threads
« Reply #1 on: November 23, 2014, 08:17:07 AM »
I see I see... Of course you are missing one piece of information there.

PedGivePotato(gLemon, 1)

Offline Unknownsoldier

  • Hero Member
  • ****
  • Posts: 2,773
    • View Profile
Re: Threads
« Reply #2 on: November 24, 2014, 08:22:10 PM »
can't understand. lel

But is it just for like if you make a new function that has a while/repeat loop?

eg:
Code: [Select]
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?
« Last Edit: November 24, 2014, 08:24:31 PM by Unkn0wnsoldier-|AB| »

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Threads
« Reply #3 on: November 25, 2014, 04:10:09 AM »
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...

Code: [Select]
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.

Offline Unknownsoldier

  • Hero Member
  • ****
  • Posts: 2,773
    • View Profile
Re: Threads
« Reply #4 on: November 25, 2014, 07:11:16 PM »
^ Ah! Now I am starting to understand. The l_0_0 is actually a boolean. 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:

Code: [Select]
function NTest(ped2, aom)
  CreateThread("F_SetPedMoneyReward",ped2,aom)
end

Then, just call the NTest(true) func?

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Threads
« Reply #5 on: November 25, 2014, 08:10:05 PM »
No, because you'd need to pass a value into both "ped2" and "aom". So you could do NTest(Edgar,400)

Offline Unknownsoldier

  • Hero Member
  • ****
  • Posts: 2,773
    • View Profile
Re: Threads
« Reply #6 on: November 25, 2014, 08:26:17 PM »
^ Yes, I edited the post and forgot to change true to the ped and amount of cash. If we did that, it would work?

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Threads
« Reply #7 on: November 25, 2014, 09:59:35 PM »
Right, it should.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Threads
« Reply #8 on: July 06, 2015, 06:54:32 PM »
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)