UGX-Mods

Call of Duty: Black Ops 3 => Help Desk => Scripting => Topic started by: Doodles_Inc on July 10, 2018, 06:13:42 pm

Title: Calling functions when to use thread
Post by: Doodles_Inc on July 10, 2018, 06:13:42 pm
Another nooby question, I noticed people use thread to call functions, is it needed? What does it do really?
What's the difference between
Code Snippet
Plaintext
function(argument)
and
Code Snippet
Plaintext
thread function(argument)
?
Title: Re: Calling functions when to use thread
Post by: BluntStuffy on July 10, 2018, 07:54:54 pm
When you just call a function, the execution is not going to continue till that function ends. Only when that function is completed, the Original execution-chain will continue.

When you thread a function the execution will continue as well and all code below it will immediately be executed.


So for example, if you call the function AddStuffToVariable() it will print "I have increased":
Code Snippet
Plaintext
SomeFunction()
{
level.SomeVariable = 1;

Add_Stuff_To_Variable();

if( level.SomeVariable == 2 )
{
Iprintln( "I have increased" );
}
else
{
Iprintln( "I'm still 1" );
}
}
Add_Stuff_To_Variable()
{
wait 1;
level.SomeVariable++;
}




But if you thread it it will print "I'm still 1" because it immediately executes that code, and the variable will only be increased a second later..:
Code Snippet
Plaintext
SomeFunction()
{
level.SomeVariable = 1;

thread Add_Stuff_To_Variable();

if( level.SomeVariable == 2 )
{
Iprintln( "I have increased" );
}
else
{
Iprintln( "I'm still 1" );
}
}
Add_Stuff_To_Variable()
{
wait 1;
level.SomeVariable++;
}
Title: Re: Calling functions when to use thread
Post by: Doodles_Inc on July 10, 2018, 11:48:30 pm
Thanks for the clear explanation  :D