Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: How and when to use spawn vs execVM.

  1. #1

    How and when to use spawn vs execVM.

    Hi,

    I've read in the http://community.bistudio.com/wiki/C...Commands_ArmA2 the info on the commands spawn and execVM.

    I understand that execVM is used to call and run a script.

    I read somewhere that if a script will be used/called many times it is advisable to compile the script and store/cache it in memory so the engine doesn't have to access the hard drive every time such script is to be run, in turn increasing game efficiency and performance.

    I searched in the forum for an example of the use of the spawn command (in layman terms), but I only found threads about spawning units/objects/etc.

    I would be extremely grateful if anybody could explain to me how to use the spawn command (vs execVM) with a simple example/case.

    Thank you much in advance. Splicer.
    "We must either find a way or make one."
    - Hannibal

  2. #2
    Spawn and execvm are exactly the same thing. Spawn takes the code directly and execvm takes an external file from it read the code.

    If you are going to call a script many times, do this:

    myStuff = compile preprocessfile "script.sqf";

    Then you can call that script with:

    call myStuff

    That way the engine doesnt need to read and compile the script each time, as it's already loaded in the myStuff variable.

  3. #3
    Sergeant Splicer's Avatar
    Join Date
    Dec 13 2009
    Location
    USA
    Posts
    198
    Author of the Thread
    Quote Originally Posted by shk View Post

    call myStuff
    Hi shk,

    Thanks much for explaining this to me!

    One more question if you don't mind.

    If the script needs to be fed with data (e.g., and array such as [value1, value2, etc.], should the data be contained between "call" and "myStuff"?

    Or is there another command (instead of "call") that should be used?

    Again, thank you so much shk!

    Best, Splicer.

  4. #4
    Hi,

    The same way you would call the script:

    [parameters list] execVM "script.sqf";

    or

    [parameters list] call myStuff;

    Cheers

  5. #5
    dataReturned = [datagiven1,given2] call myStuff;

    Spawn can also be used to execute myStuff. Difference between spawn and call is that spawn starts a new thread, which runs besides the main game thread, thus not interrupting game flow and call in run in the one that it's called from. If call is run on the main thread (init.sqf, triggers, eventhandlers etc), it can not contain any waituntil/sleep etc which would stop the game. Call can sleep if it's used within a child thread (execvm, spawn).
    Last edited by Shuko; Jan 8 2010 at 19:20.

  6. #6
    Quote Originally Posted by shk View Post
    Spawn and execvm are exactly the same thing.
    Spawn can be used on internal functions. For example, you can have a dozen functions in a single sqf file running dozens instances of these functions using spawn. With execVM and sqf, you'd have a dozen files instead.

    The difference between call and spawn is easy: call goes through your code line by line (ie it doesnt continue with what is after call until what is within call is done), spawn make a new instance and continues with what is after spawn in paralell with what is within spawn.

  7. #7
    Sergeant Splicer's Avatar
    Join Date
    Dec 13 2009
    Location
    USA
    Posts
    198
    Author of the Thread
    Hi guys,

    Super! Your explanation/examples make this crystal clear!

    THANK YOU SO MUCH!!!

    My best, Splicer.

  8. #8
    Spawn can also be used to execute myStuff. Difference between spawn and call is that spawn starts a new thread, which runs besides the main game thread, thus not interrupting game flow. Call on the other hand is run on the main thread, which means it can not contain any waituntil/sleep etc which would stop the game.
    This isn't quite correct. Call simply executes the code 'in-line' with the current thread. waituntil/sleep etc are all perfectly possible inside called code as long as the calling context is a thread in its own right and interruptable. Some contexts are not interruptable (event-handlers for example).

    Spawn creates a new (and interruptable) thread in its own right.
    Author of PVPmissionWizard ArmA2FPSAnalyser AddonChecker and ... squint

    Tools homepage

    Crosseyed and Painless - a blog about my ArmA2 developments



  9. #9
    Gunnery Sergeant Zonekiller's Avatar
    Join Date
    Nov 19 2005
    Location
    Queensland ***"Australia"
    Posts
    491
    a spawned script will run at the same time as the script that spawned it
    a script will stop and wait till the called script is finished.

    so if you need input into a script use call , if you want something to happen while
    your script is running use spawn

    ie
    script telling unit to goto somewhere , new script telling unit to setdamage to 0 , you would use spawn

    script wanting to tell a unit to go somewhere , new script to find that unit you would use call.

    i dont know if that will make sence to you if not i can go into more detail...

  10. #10
    Sergeant Splicer's Avatar
    Join Date
    Dec 13 2009
    Location
    USA
    Posts
    198
    Author of the Thread
    Quote Originally Posted by Zonekiller View Post
    a spawned script will run at the same time as the script that spawned it
    a script will stop and wait till the called script is finished.

    so if you need input into a script use call , if you want something to happen while
    your script is running use spawn

    ie
    script telling unit to goto somewhere , new script telling unit to setdamage to 0 , you would use spawn

    script wanting to tell a unit to go somewhere , new script to find that unit you would use call.

    i dont know if that will make sence to you if not i can go into more detail...
    Hi ZoneKiller,

    If I understand your examples correctly, one could say that spawn is to be used when the output of one of the 2 scripts does not depend on the other script to be done first (i.e., your example #1).

    On the other hand, when the input of a script depends on the result of another script (i.e., your example #2), in such case the call command is to be used.

    If the above is correct, then it's not clear to me why one couldn't use spawn for example #2. All that would happen is that the script would remain in memory idle until the AI reached its final destination to then be able to obtain its position.

    Is this correct?

    Thanks. Splicer.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •