Jump to content
Sign in to follow this  
Maurdekye

What's the difference between spawn and call?

Recommended Posts

Both commands seem to do exactly the same thing, and I'm not sure why there are two separate ones.

Share this post


Link to post
Share on other sites

spawn creates a new thread/vm (virtual machine)

call executes code within the same thread

call { sleep 5; };
hint "This will be shown after 5 sec.";

[] spawn { sleep 5; };
hint "This will be shown instantly.";

Local variables are also to be considered. That's why local variables in called code should be made private.

_test = 123;
call { hint format[" %1", _test]; }; // Will display 123
hint format[" %1", _test]; // Will display 123

_test = 123;
call {
private ["_test"];
_test = 456;
hint format[" %1", _test]; // Will display 456
};
hint format[" %1", _test]; // Will display 123

_test = 123;
[] spawn {
_test = 456;
hint format[" %1", _test]; // Will display 456
};
hint format[" %1", _test]; // Will display 123

Has been asked recently here.

Share this post


Link to post
Share on other sites

Thanks for the explanation. I kind of had that sort of idea already, but I wasn't absolutely sure.

Share this post


Link to post
Share on other sites

Calls can also be used to return values.

This function will return half of any number input.


My_fnc= {
_N=(_this select 0);
_N=_N * 0.5;
_N
};

_newVal = [5] call my_fnc;
Hint str _newVal;

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×