Jump to content
scottb613

Spawn - in layman's terms ?

Recommended Posts

Hi Folks,

Getting my feet wet with Arma scripting - would someone better be able to explain the "spawn" function (nothing to do with spawning units) in scripting - it's used in a snippet of code someone helped me out with - I've read the wiki page several times - looked at examples - but - I still don't get what it's doing... Maybe an example of when and why I would use it would help...

Thanks...

Regards,

Scott

Sent from my iPad using Tapatalk

Share this post


Link to post
Share on other sites

There a basically two ways to use SQF in Arma: As scriptfile or as function.

 

A scriptfile is loaded from disc with execVM. This will load a file, compile its contents and run execute them (if valid).

A function is a body of code with a name and is defined either in config or by defining them in a scriptfile. After they are loaded/defined the compiled code exists as the functions name in a variable (typename of this variable is "CODE").

This compiled code can now be executed in two ways: Either by using call or by using spawn.

 

If the function is called, the calling script will be suspended until the called function finished. This allows the function to return a value to the calling script. The called function will not support timing commands like sleep and waituntil.

If the function is spawned the calling script is not suspended and continues with its own procedure. The spawned function is executed in parallel to the invoking script but that also means it can never return any value. Timing and suspending the execution is allowed in spawned functions.

 

Here are some simple examples.

 

Call:

my_fnc_degToRad = {
	params["_deg"];
	private _rad = _deg*3.14/180;
	_rad;
};
SomeDirInRad = [180] call my_fnc_degToRad; //Should be ~pi
my_fnc_killThePlayer = {
	private _success = false;
	if(alive player) then {
		player setdamage 1;
		_success = true;
	}
	_success;
}

if(!(call my_fnc_killThePlayer)) {
	hint "Player already dead!";
};

Spawn:

my_fnc_watchPlayer = {
	while{alive player} do {
		sleep 0.5;
	};
	hint "Player died!";
}

//Do something before function
spawn my_fnc_watchPlayer;
//Code after this will not wait until player died

//Do something before function
call my_fnc_watchPlayer; //Will throw an error as sleep is not allowed in a called script

TL;DR: Small functions that produce a instant sideeffect or need to return a value are called. Larger functions/scripts that do a lot of work, manage the mission flow or watch the states of objects and don't need to have a return value are spawned, so the calling script doesn't have to wait for them to terminate.

  • Like 2

Share this post


Link to post
Share on other sites

Hi Folks,

Thanks for the help - and Neo for going the extra mile - very clear and helpful... So for the UNIX inclined - spawn is like putting an "&" ampersand on the end of he line to get a command/script to run in the background... I think I've got it... I'll look at the link you posted as well - Grumpy...

Much appreciated - gents...

Regards,

Scott

Sent from my iPad using Tapatalk

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

×