[LIB]Threads

fxserver-threads

Threads utilities for FXServer

INSTALLATION

Set it as a dependency in you fxmanifest.lua
Set debuglog = false in threads.lua if you dont want any rubbish message

client_script '@threads/threads.lua'

DESCRIPTION

A new Thread.CreateLoop make a CreateThread(function Wait(x) end) loop
Auto add a action into the while loop Threads.CreateLoop(actionname,millisecondID,…
You can delete any action which is in the loop by Threads.KillActionOfLoop(actionname)
Auto delete and break the while loop when all actions have been killed.

You can create a custom Loop which can be set or get the delay of next loop with the cb.setter or cb.getter
Threads.CreateLoopCustom(actionname,0,function(delay) delay.setter(3000) end)

also you can get the actionname and the total loops when debug by Threads.CreateLoop(actionname,millisecondID,function(name,totalofloops) print(name,totalofloops) end)
or Threads.CreateLoopCustom(actionname,0,function(delay,name,total) print(name,total) delay.setter(3000*math.random()) end)

Threads.xxxxCustom is just different with Threads.xxxx by a setter and getter .
You can also pass a Varname into Threads.xxxxCustom params 4 so that you can using Threads.GetLoopCustom and Threads.SetLoopCustom

Threads.CreateLoop
Threads.CreateLoopOnce
Threads.CreateLoopCustom
Threads.CreateLoopOnceCustom
Threads.SetLoopCustom
Threads.GetLoopCustom

EXAMPLE


Threads.CreateLoop("Check",0,function(name)
    print(name)
end)
Threads.CreateLoop("Check2",1000,function(name,total)
    print(name,total)
    Threads.KillActionOfLoop("Check")
end)
Threads.CreateLoop("Check3",3000,function()
    print(9)
end)
Threads.CreateLoopCustom("Check3",3000,function()
    print("hhhh3")
end,"mycar")
Threads.SetLoopCustom("mycar",2000)
Threads.CreateLoopCustom("CheckCustom",3000,function(delay)
    print("hhhh4")
    delay.setter(1500)
end)
Threads.CreateLoopCustom("CheckCustomGetSet",3000,function(delay,name,total)
    print("get:"..name,total,delay.getter())
    delay.setter(100)
    print("get:"..name,total,delay.getter())
end)

Modules : Tween

Threads.TweenCFX.removeTween (object)  --cancel a tween
Threads.TweenCFX.endTween (object, forceComplete)  --force to the end of tween
Threads.TweenCFX.to (object, duration, vars)  --from something to the end such as alpha
Threads.TweenCFX.delayCall (object, duration, vars) --from something to the end such as alpha but not change/tween anything.

Modules : Arrival (dependency with starting Threads script)

Add positions and callback when you arrived that place. recommanded range <=5.0

Threads.AddPositions
Threads.AddPosition 

EXAMPLE

Modules : Scaleforms (dependency with starting Threads script)

Threads.Scaleforms.Call
Threads.Scaleforms.Draw
Threads.Scaleforms.DrawDuration
Threads.Scaleforms.End
Threads.Scaleforms.RequestCallback
Threads.Scaleforms.DrawPosition
Threads.Scaleforms.DrawPosition2
Threads.Scaleforms.DrawPositionDuration
Threads.Scaleforms.DrawPosition2Duration

Github: GitHub - negbook/threads: FXServer Threads

3 Likes

Please can you explaine more about this? What are the advantages? Thank you!

group all functions in one while true loop.
using for i=1,#table do instead for i,v in pairs(table) do
set all functions or tables into a variable like Citizen.Wait (table maybe) to CWait
all of these is for better performances.
if you do

    Threads.loop(expandWorldtasks,0)
    Threads.loop(gametimetasks,500)
    Threads.loop(othertasks,500)

it creates

local CWait = Citizen.Wait
Citizen.CreateThread(function()
 while true do 
 CWait(0)
 functions1() --expandWorldtasks
...
end )
Citizen.CreateThread(function()
 while true do 
 CWait(500)
 functions1() --gametimetasks
 functions2() --othertasks
...
end )

Note: This LIB is useless if you gourp all functions into one while loop one by one just like this LIB’s logic by your hand by yourself in all of your scripts.(because it takes more 0.005~0.008ms CPU msec than by your hand)

Ok thank you! I will test it! But one last question, what is ExpandWorldLimit and the variables -9000 or 10000?

ExpandWorldLimit:

Note: This LIB is useless if you gourp all functions into one while loop one by one just like this LIB’s logic by your hand by yourself in all of your scripts.

Thank you so much, amazing! Will test it and share the result here! I don’t understand this LIB 100%, if I must use it or not. Anyway thanks I like it, hope will optimise some code I have

So all depend about the timer ?
if for e.g two function of loop have the same timer it use the same loop right ?
and for another function who have another timer it will use another new loop but not same as the other?

yes.
all timer 0 will group in to while true do Citizen.Wait(0)
all timer 500 will group in to while true do Citizen.Wait(500)
if you create a timer 501
it will Create a new loop
while true do Citizen.Wait(501)

2 Likes

[This Version] sometimes you may need create a new timer
like:

GetPlayerAvatarTxd = function(transparent,cb)
	local ped = PlayerPedId()
	local mugshot = nil 
	if transparent then
		mugshot = RegisterPedheadshotTransparent(ped)
	else
		mugshot = RegisterPedheadshot(ped)
	end
    while not IsPedheadshotReady(mugshot)  do
		Citizen.Wait(0)
	end
	cb(mugshot,GetPedheadshotTxdString(mugshot))

	UnregisterPedheadshot(mugshot) 
end 
Citizen.CreateThread(function()
	local nextstep = false
	Threads.loop(function ()
		GetPlayerAvatarTxd(true,function(mugshot,mugshotStr)
			print(mugshotStr)
			nextstep = true 
		end)
		repeat 
			Citizen.Wait(0)
		until nextstep == true    --because it may make the entire loop 500 delayed
		nextstep = false 
		end ,501) -- so I would create a new loop 501
	
end)
1 Like

Look interesting good release !

Updated:

Threads.CreateLoop(namestring,millisecond,function) -- group all the same millisecond loop (with a name)  into a while true do 
Threads.CreateLoopOnce(namestring,millisecond,function) --  ignore second call of this. it will group into CreateLoop if a loop is already exist
Threads.CreateLoopCustom(function)  -- just like CreateLoop but without Wait . for custom delay loop

Relative Topic: Arrival utilities for FXServer | Destination Check

Updated.New functions and usage here

Threads.CreateLoop(actionname,millisecondID,function(name,totalofloops)) or (actionname,function(name,totalofloops)) or (function(name,totalofloops)) -- group all the same millisecond loop (with a name)  into a while true do 
Threads.CreateLoopOnce(actionname,millisecondID,function(name,totalofloops)) or (actionname,function(name,totalofloops)) or (function(name,totalofloops)) --  ignore second call of this. it will group into CreateLoop if a loop is already exist
Threads.KillActionOfLoop(actionname)
Threads.CreateLoopCustom(actionname,defaultmillisecondID,function(varname,name,totalofcustomloops),(varname or keeping empty))  -- just like CreateLoop but with delay.setter and delay.getter
Threads.CreateLoopCustomOnce(actionname,defaultmillisecondID,function(varname,name,totalofcustomloops),(varname or keeping empty if you just want to using s/getter))  -- just like CreateLoop but with delay.setter and delay.getter.Will default using functionhash if the varname is empty.
Threads.KillActionOfLoopCustom(actionname) 
Threads.GetLoopCustom(varname)
Threads.SetLoopCustom(varname,millisecond)