Jump to content
Sign in to follow this  
phronk

If Then Else in Functions

Recommended Posts

I'm writing a script that executes on every spawned unit which calls several functions.  Each of the functions randomly choose a piece of gear for the spawned unit to wear/be equipped with: uniform, vest, headgear, goggles, face, weapon, and magazines.

 

I have If Then Else commands in the functions which check if the TalibanFighters addon is running.  If it is, the function chooses a random uniform from a list of TalibanFighters uniform classnames and applies them to the unit.  If the addon is NOT running, it adds a vanilla uniform to the unit from a list of vanilla uniform classnames.

 

My question is, what is better for performance in this situation?  Have the If Then Else commands in the script like this:

1. If ______ Mod Is Running

2. Then { RemoveUniform _unit;

3. _unit forceAddUniform _listOfModUniforms;

4. } Else {

5. RemoveUniform _unit;

6. _unit forceAddUniform _listOfVanillaUniforms;};

 

Or have the If Then Else like that in a function and call it from the script like this:

1. RemoveUniform _unit;

2. [_unit] call myUniformVariable;

 

What performs better?

Share this post


Link to post
Share on other sites

You could check with the codePerformance function, but I would say for something simple like this with only two possible conditions the if-then-else would be the faster option.

Share this post


Link to post
Share on other sites

To clarify, I'm using the If Then Else regardless if it's all done through a script without functions or if it's in the function itself.  I'm just curious if it would be optimal to have the randomization script call functions which basically do what I was going to put in the script.  If my functions have If Then Elses and the script which calls them does not, would that be faster?  Not sure if it's redundant to call the functions when I could just include it all in a big blob of code.  Call it all through functions would be cleaner and therefore faster, I'd assume. :\

Share this post


Link to post
Share on other sites

Functions would be the best choice, IF you are having that code executed more than twice. However, I would recommend you take a look Here.

 

 

In order for the game to remember the function, you will need to have the following syntax:

call compileFinal preprocessFileLineNumbers "path\to\your\file.sqf";

The reason why I mentioned compileFinal instead of compile, is that compileFinal will secure the specified file from hackers. (Reference is at the bottom of the page I linked above)

 

 

 

Article from the link above:

 

If Else If Else If Else ...

If you can't escape this using a switch control structure, then try and rethink the functionality. Especially if only one option is needed to match.

On the other hand switch is slower than if then else. To keep tidiness of the switch and speed of if, use if exitWith combined with call:

call {
	if (cond1) exitWith
	{
		//code 1
	};
	if (cond2) exitWith
	{
		//code 2
	};
	if (cond3) exitWith
	{
		//code 3
	};
	//default code
};

if () then {} 
is faster than 
if () exitWith {} 
is faster than 
if () then {} else {} 
or 
if () then [{},{}]

However there is no noticeable difference in speed in the following:

_a = 0;
if (true) then {_a = 1};
_a = if (true) then [{1},{0}];
_a = if (true) then {1} else {0};
  • Thanks 1

Share this post


Link to post
Share on other sites

The optimization between if () then {}; and if () then {} else {}; won't matter that much, as a general optimization of your code would do.

Try this instead:

_uniforms = ["vanillaUniform1","vanillaUniform2"]
if (isClass(configFile >> "CfgPatches" >> "talibanMod")) then {
_uniforms = ["talibanUniform1","talibanUniform2"]
};
removeUniform _unit;
_unit forceAddUniform (_uniforms call BIS_fnc_selectRandom);

That way the _uniforms-array is redefined if the talibanMod is present and the first _uniforms is being used if the mod isn't present. The removeUniform and the _unit forceAddUniform will be executed either way, so you don't have to put it in twice.

Share this post


Link to post
Share on other sites

You can also use:

if (!isServer) exitWith {};

To avoid global problems within multiplayer. More info can be found Here.

Share this post


Link to post
Share on other sites

Gonna highjack this thread for a quick question:

 

Is there a difference performance between:

if (!isServer) exitWith {};


if(isServer) then
{
  // some splendid code
};

Share this post


Link to post
Share on other sites

 

Gonna highjack this thread for a quick question:

 

Is there a difference performance between:

if (!isServer) exitWith {};


if(isServer) then
{
  // some splendid code
};

 

See my last Post.

 

 

 

Alternatively, you can use

if (!local player) exitWith {};

instead of

if (!isServer) exitWith {};

Which KK has heard that it works better than the other. But both work in the same matter, so try out which one suits your code. :)

Share this post


Link to post
Share on other sites

If the code is run only once (on a client) then if then else will suffice. If you run it more than once, use a function. In your example, the server could apply the gear and then it would be more efficient to use a function.

Share this post


Link to post
Share on other sites

Oh I see, that's useful information guys.

I've been doing that same method you gave, belbo, but with an if{then} for the uniforms:

 

if (isClass(configFile >> "CfgPatches" >> "talibanMod")) then {
_uniforms = ["talibanUniform1","talibanUniform2"];

} else {
_uniforms = ["vanillaUniform1","vanillaUniform2","vanillaUniform3"];

};

 

Didn't think I could do it in a way to not need an } else {

Share this post


Link to post
Share on other sites

See my last Post.

 

 

 

Alternatively, you can use

if (!local player) exitWith {};

instead of

if (!isServer) exitWith {};

Which KK has heard that it works better than the other. But both work in the same matter, so try out which one suits your code. :)

 

Thanks for that link, enjoyed reading that, some great infos there.

  • Like 2

Share this post


Link to post
Share on other sites
_uniforms = ["vanillaUniform1","vanillaUniform2"]
if (isClass(configFile >> "CfgPatches" >> "talibanMod")) then {
_uniforms = ["talibanUniform1","talibanUniform2"]
};

should be faster than

if (isClass(configFile >> "CfgPatches" >> "talibanMod")) then {
_uniforms = ["talibanUniform1","talibanUniform2"];
} else {
_uniforms = ["vanillaUniform1","vanillaUniform2","vanillaUniform3"];
};
  • Like 1

Share this post


Link to post
Share on other sites

Maybe I'm on drugs, but it feels faster when I test the mission using the functions as you guys have suggested.  I get a lot less stutter at the start of the mission.  Still get stutter when spawning units, even though I disable every AI feature and simulation for the unit upon being spawned, but aside from that it seems like an improvement.

 

[EDIT]

Tested the performance chance by going into an area that usually spawns so many guys it crashes my cheap dedicated server, but it's actually running pretty smoothly and the desync was reduced considerably. High fives all around!

I also did some other stuff with disabling all AI and simulation of the spawned units as soon as they are created.  That seemed to help as well.

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  

×