Jump to content
Sign in to follow this  
gossamersolid

Spawn/ExecVM - When to Use What?

Recommended Posts

I'm a little confused on the differences between these two. I've checked the BIKI pages on both and it still doesn't provide me with enough information to determine when to use either one.

Could somebody please explain the differences between the two and what the benefits of using one or the other are?

Share this post


Link to post
Share on other sites

execvm "script.sqf"

spawn my_func;

[] spawn {_array = _this;};

Hopefully that gave you a better idea... they are very different, one only accepts file names, other accepts code blocks.

Share this post


Link to post
Share on other sites
they are very different
Not true. In the meaning of what they do they are the same.

execVM compiles and runs the script (in paraller thread)

spawn runs compiled script (preprocessFile command) or code block like you wrote (as well in paraller thread)

The same discussion: http://forums.bistudio.com/showthread.php?t=93261&highlight=execVM

So if you want to use the script more than one time - use spawn (and before save the compiled script in some variable), otherwise use execVM

Edited by Przemek_kondor

Share this post


Link to post
Share on other sites

I see that Spawn and execVM, functionality speaking, are doing the same thing, executing code in parallels tasks. The main difference is the organization of code, Spawn allow agglutinate all codes in a unique source code file,sometimes, facilitating the reading of code. execVM allow to organize the code in peaces of scripts divided in some files. I use Spawn when I don't want/need a lot of script files in my mission.

spawn runs compiled script (preprocessFile command) or code block like you wrote (as well in paraller thread)

Yes, Spawn is great for dynamic scripts too!!

sorry, my English is not enough...

Edited by D3lta

Share this post


Link to post
Share on other sites
How does call differ from spawn and execVM?

spawn or execVM returns a handle to the newly started script:

_handle = [] execVM "myscript.sqf";

Then you can use the _handle to check if the script is done, or to terminate it.

call can return a value. Example:

MultiplyByTen = {
   _original = _this;
   _new = _original * 10;
   _new
};

The last expression is the result. Here the result is the value of _new.

Then if you do:

_largeNumber = 10 call MultiplyByTen;

_largeNumber would be 100.

Using call you can reuse different scripts more easily. The downside is (depending on where you call it) you called scripts cannot spend too much time. A sleep of 5 seconds is not allowed for example.

There are some situations where you can do this. If you spawn a script that then calls another that sleeps then this is allowed because it runs in parallel and does not slow down the game.

Share this post


Link to post
Share on other sites

Couldn't have wished for a better explanation, thanks!

Share this post


Link to post
Share on other sites
How does call differ from spawn and execVM?

In addition to the difference in return value that Muzzleflash explained, call is also different in that the code that it runs is not run in parallel. In other words, call does not start a new instance of code that runs alongside the script where it was used; it takes place within the original instance. Thus, whatever code is contained in the call must be finished before the script where it was used can continue.

Sleeps actually can be used in code that is run via call, but they will be ignored if you are using call in an area outside of the scheduled scripting environment (i.e., in an init field or trigger activation field).

Finally, it's worth noting that any local variable used in code that is run via call will be brought into the same scope as the script where call was used. This is why you see people using "private" in functions all the time.

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  

×