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


Author Topic: Threads?  (Read 2905 times)

0 Members and 1 Guest are viewing this topic.

Offline 3gtheepic

  • Jr. Member
  • **
  • Posts: 10
  • Gender: Male
  • I'm just some kid who likes to mod.
    • View Profile
Threads?
« on: September 17, 2016, 05:20:39 PM »
I SUCK AT MODDING
I was just looking through the source code of The Cure by daboss to learn more about bully script making then I cam across a function called CreateThread() and I was wondering what that does. Anyone?

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Threads?
« Reply #1 on: September 17, 2016, 06:56:51 PM »
Threads are basically what all the code runs on. By default, all your code will run on what is called the main thread. You can create a new thread by using CreateThread. Its only argument is a function to start the thread in. For example...

Code: [Select]
function T_MyThread()
  TextPrintString("This is running in a custom thread.",0,1)
end
function main()
  CreateThread("T_MyThread") -- notice the thread is specified by a string, not by just putting the function itself.
end

Now what is useful about this is the fact that each thread gets suspended (waits) separately. All the threads run in order, starting with the main thread, followed by each thread that was created after that. Once one thread is suspended (calls the Wait function), the next thread will run, until all threads are suspended, then the next script that is currently loaded will run, and so on until all the threads/scripts have run and the game processes one frame. So far with this information there really isn't any benefits to using threads... but here is the thing. You can suspend threads (make them wait) for more than just one frame (0 ms), you can suspend them for a certain amount of time before they run again and only the other non-suspended threads will run during that time. All the threads will still be running in the same order, only the suspended threads will be skipped until they are no longer meant to be suspended. If one of your threads does not wait, the game will get stuck running that thread and never move onto the next one, therefor the game will be frozen. This is why all your scripts need to wait, and they need to wait on every thread using the Wait function: either by calling it with 0 (wait for one frame) or with a time in milliseconds.

Here's a better example:
Code: [Select]
function T_FightingStyle()
  while true do
    Wait(0)
    if IsButtonBeingPressed(3,0) then
      PedSetActionNode(gPlayer,"Catch_Throw","BOSS_Darby.act")
  end
end
function main()
  CreateThread("T_FightingStyle")
  TextPrintString("About to start the fight!",3,1)
  Wait(3000)
  for i = 3,1,-1 do
    TextPrintString(i.."!",1,1)
    Wait(1000)
  end
  TextPrintString("GO!",1,1)
  -- some kinda enemy spawning and crap here
end

Notice in this example, the main thread has a lot of waiting, so the fighting style code could not fit in there as it would be screwed up by the thread being suspended and the buttons being unresponsive since the thread isn't running. To fix this, we have the fighting style in its own thread, so that it will always be running and not be paused by the other threads.



I haven't seen ya around here before, new? Taking an interest in modding huh?

Offline 3gtheepic

  • Jr. Member
  • **
  • Posts: 10
  • Gender: Male
  • I'm just some kid who likes to mod.
    • View Profile
Re: Threads?
« Reply #2 on: September 17, 2016, 08:52:15 PM »
Thanks for your help! Yes, I have got into modding. Maybe this will help!  :)