Jump to content
Sign in to follow this  
alleycat

Question about function basics

Recommended Posts

If I want to pack code into a function how would I do that? I have looked at the keydown functions but I am not sure of the correct syntax and what type of compile to use.

_unassigned_players = []; 
{ 
 if ((_x isKindOf "Man") and (side _x == west)) then 
 { 
	  _unassigned_players set [count _unassigned_players, _x]; 
 }; 
} forEach Allunits; 
diag_log text format["_unassigned_players: %1", count _unassigned_players];

I use this a lot and I would like to pack this into a reusable function.

Share this post


Link to post
Share on other sites

You could simply put that in your init.sqf in two ways:

// Either directly put the code here:
ALY_fnc_countUnassignedPlayers = {
_unassigned_players = []; 
{ 
	 if ((_x isKindOf "Man") and (side _x == west)) then 
	 { 
		  _unassigned_players set [count _unassigned_players, _x]; 
	 }; 
} forEach Allunits; 
diag_log text format["_unassigned_players: %1", count _unassigned_players];
};

// Or you put it in an external .sqf file and then compile it:
ALY_fnc_countUnassignedPlayers = compile(preProcessFileLineNumbers "countUnassignedPlayers.sqf");

// And execute via:
call ALY_fnc_countUnassignedPlayers;

Share this post


Link to post
Share on other sites

Thanks, 2 questions about this:

1. If I call the function, will the game execute that function and wait for it to be finished or simply start it and continue with the next statement? So far I avoided functions because i was not sure how that works.

Example:

statement;

function call;

statement;

2. What is the advantage of compiling and preprocessing? Is it performane gain and does that even matter on something small as my example?

Share this post


Link to post
Share on other sites

1. If you aren't suspending the script, the next statement will run in parallel with the function call. You can use the waituntil command to control that.

2. Preprocessing and compiling to a variable saves hard disk time. Everything runs from the memory. If you are calling the function frequently you should preprocess and compile your code. On a small scale it really doesn't matter.

Share this post


Link to post
Share on other sites

The first one is not true.

As long as you use call Function the script will wait for it to return.

With spawn Function the script will continue.

Share this post


Link to post
Share on other sites

Have to agree with Lappihuan. From my understanding there are three ways to execute .sqf scripts.

1. execVM - Will read in the script file then compile and then execute it line by line. Script's return value is a handle which for example can be used like this: "waitUntil {scriptDone _handle}" or "terminate _handle".

2. call - Needs the script to be preprocessed / compiled already. Executes the code and waits until it's done. Returns a value instead of a handle. When the code contains sleeps or waitUntils it must be executed within a scheduled environment. (Correct me when I'm wrong.)

3. spawn - Also needs the script to be preprocessed / compiled already. Executes the code like with execVM and returns a handle which can be used like described above.

The decision call vs. spawn/execVM is in my opinion dependant on in which environment you want to run it. Run a script containing sleep or waitUntil from a trigger or init line? Use spawn or execVM as it's not scheduled and won't wait until the script is done. Executing a function within a script (that may be executed by spawn or execVM) to calculate some value? Use call because you would want to have the result returned. Furthermore, I read somewhere spawn and execVM open new threads while call functions within the thread from where it is executed. Please, someone with better knowledge may correct me.

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  

×