Jump to content
Sign in to follow this  
fuerst_von_butz

How to make sure functions are initalized before they are needed?

Recommended Posts

Hey,

this probably has been asked before but I can't find the answer to this.

In my init.sqf I have this:

setTerrainGrid 12.5;

[] call compile preprocessFile "scripts\functions.sqf";

[...]

The functions.sqf:

FVB_clear = {
_object = _this select 0;
clearMagazineCargoGlobal _object;
clearWeaponCargoGlobal _object;
clearItemCargoGlobal _object;
clearBackpackCargoGlobal _object;
};

[...]

So, how can I make sure the functions are initialized before they are needed, e.g. an initialization line of a vehicle placed in editor?

Side question: Would it be better/make no difference to use #include "scripts\functions.sqf"; in the init.sqf?

Cheers!

Share this post


Link to post
Share on other sites

snip see above

Edited by cuel

Share this post


Link to post
Share on other sites
Learn all about functions and how to use them here:

http://community.bistudio.com/wiki/Functions_Library_(Arma_3)

Ok, wow thanks kylania. Another wiki page I've never seen. The wiki search engine is really not good, when you search for "function" that page won't show up in the results.

I still have a problem though, this is my setup:

description.ext:

class CfgFunctions {
class FVB {
	class ServerSideFunctions {
		class clear {
			preInit = 1;
			file = "scripts\clear.sqf";
		};
	};
};
};

scripts\clear.sqf:

// Locality: Server only
if (!isServer) exitWith {};

private ["_object"];
_object = _this select 0;
clearMagazineCargoGlobal _object;
clearWeaponCargoGlobal _object;
clearItemCargoGlobal _object;
clearBackpackCargoGlobal _object;

I don't even use the function anywhere yet, but on mission load I get this error:

Error in expression < ["_object"];
_object = _this select 0;
clearMagazineCargoGlobal _object;
clearW>
 Error position: <clearMagazineCargoGlobal _object;
clearW>
 Error clearmagazinecargoglobal: Type String, expected Object
File C:\Users\dlx\Documents\Arma 3\missions\Operation_Ricochet.Stratis\scripts\clear.sqf, line 6

Cheers

Share this post


Link to post
Share on other sites
Another wiki page I've never seen.

That's because it's just a few days old. Moricky spent the weekend making that little bundle of joy for us.

Share this post


Link to post
Share on other sites

Nice to see they are still working on the wiki, that page is one of the better ones :)

I still can't find out why the compiler thinks that _object is a string.

If I remove this line from clear.sqf

_object = _this select 0;

the error is gone, but the function obviously doesn't work. So why does that expression set the type of _object to string?

Any ideas?

Share this post


Link to post
Share on other sites

Use the BIS_fnc_param stuff maybe instead of just _this select 0?

Share this post


Link to post
Share on other sites
I don't even use the function anywhere yet, but on mission load I get this error:

That is because you have set the function as preinit = 1; preinit means run this function when the mission loads up.

From your explanation this is not the behaviour you are after, all you need is for your function to get processed/compiled ready for you to run when you need it.

You are not seeing the error now because you have turned the parameter assignment into a bis_fnc_param statement. If you were to look in your RPT you will find it is still actually running, its just that bis_fnc_param is logging the error to the RPT (due to the param being of the wrong type) instead of the function creating a visible UI error box.

Keep the bis_fnc_param assignment it is good practice to have your function show errors on the wrong type of information being sent to it, just remove the preinit from the function config, this is all you need to preprocess your script ready for use.

Edited by Larrow

Share this post


Link to post
Share on other sites
That is because you have set the function as preinit = 1; preinit means run this function when the mission loads up.

From your explanation this is not the behaviour you are after, all you need is for your function to get processed/compiled ready for you to run when you need it.

Initialization Order

When mission is starting, its components are initialized in the following order:

Functions with recompile param set to 1 are recompiled

Functions with preInit param set to 1 are called

Object Init Event Handlers are called

Object initialization fields are called

My intention was to use the function in the init field of a vehicle, that's why I set preinit. But thanks to you I can see now that it says "Functions with preInit param set to 1 are called" and not compiled. Not sure though why it's called with an empty String.

Thanks Larrow!

Share this post


Link to post
Share on other sites

They're still compiled. Anyone know why they are called, always?

Anyway you should be able to get around it by

_object = [_this,0,objNull] call BIS_fnc_param;
if (isNull _object) exitWith {};

Share this post


Link to post
Share on other sites
I can see now that it says "Functions with preInit param set to 1 are called" and not compiled.

As cuel says they are still compiled.

Anyone know why they are called, always?

Erm they are not. Just done a quick test using

fnc_initCheck.sqf

_passed = _this;
hint str _passed;
diag_log str _passed;

description.ext

class CfgFunctions {
class LAR {
	class myFunctions {
		class initCheck {
			file = "fnc_initCheck.sqf";
		};
	};
};
};

There is no hint and nothing in the RPT.

If you place the preinit = 1; in the config then the function is passed an array of ["preinit"].

Edited by Larrow

Share this post


Link to post
Share on other sites

I wrote that incomprehensibly. I know they are compiled. I just understood the preInit parameter as a setting to make sure they are compiled before object initialization lines. But now I see they are compiled before anyways and preInit is just to call them once before.

Thanks for your help guys.

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  

×