Jump to content
Sign in to follow this  
twistking

sometimes loadout-script doesn't work for no apparent reason

Recommended Posts

hello,

like many others, i use a very simple script for editing loadouts of player- and ai-units. most of the time, it works well, but sometimes some or all players start the mission with default loadout. most of the time it works again after a mission restart. this happens on dedicated servers as well as servers hosted from within the game.

i don't know much about scripting, so i guess my scripts are just not "well" written. would be nice, if someone could improve my scripting and could shortly explain why mine wasn't that good.

loadout_example01.sqf

removeAllWeapons _this;
_this removeWeapon "ACE_SpottingScope";
_this removeWeapon "ACE_Kestrel4500";
_this addMagazine "30Rnd_9x19_MP5";
_this addMagazine "30Rnd_9x19_MP5";
_this addMagazine "30Rnd_9x19_MP5";
_this addMagazine "30Rnd_9x19_MP5";
_this addWeapon "ACE_MP5A4";
_this selectWeapon "ACE_MP5A4";

i would call this script from multiple units init-lines with

this exec "loadout_example01.sqf";

in the same mission i would have multiple loadout.sqfs, each called by multiple units via init-line. the scripts are all very basic, much like the example above, but with other weapons etc.

as said before: most of the time it works, but sometimes it does not. help appreciated:)

Share this post


Link to post
Share on other sites

Well without being able to reproduce the state when the loadout doesnt work, we just can assume that some factors while loading the mission is interfering with your desired outcome.

I did not do enough tests to actually confirm my assumption, but my guess would be a sort of race condition, where sometimes the mission is not quickly loaded enough to have the desired effects using the init-lines of the objects you placed in the editor.

Afaik the init-field of the objects are executed before the init.sqf of the mission is being called, this could be false information though so don't consider this as an actual fact.

But you can test this assumption with a little trick :>

Put this line of code into the init-field of your units and replace the code you already had (the one for changing the loadout):

this setVariable ["loadoutScriptFilename", "loadout_example01", true];

Change "loadout_example01" to either string which represents the filename of the sqf-file to be executed for the specific unit.

If you wish to have another loadout for any of your units, you could also use "loadout_example02" or "loadout_example03" and so on.

In the init.sqf, add the following lines:

if (isServer) then 
{
   _loadoutFileName = "";
   {
       // Check if unit has been assigned to any loadout-scripts
       _loadoutFileName = _x getVariable ["loadoutScriptFilename", ""];

       if (_loadoutFileName != "") then
       {	
		// Call the loadout script
		_x execVM format["%1.sqf", _loadoutFileName];
       };

   } forEach allUnits;
};

Please be advised that above code will execute just once, when the server is "logged into the game".

For this to work, every unit in question in your mission has to define the "loadoutScriptFilename" - players who join later on won't be able to be effected by the loadout-scripts (but naturally you can design it for JIP players as well).

Sure, there are always better ways to do this (like just one loadout-script which gets the desired weapons and magazines as parameters), but with this you dont have to rearrange your sqf-files to test it.

Furthermore you could extract the code above and put it into a global function to use it whenever you need, e.g. after spawning more units dynamically.

If your problem still remains, we have to look for another ideas by more experienced developers :)

Share this post


Link to post
Share on other sites

wow! many thanks for the answer!

i think you could be right with the assumption of something like a race condition. in the mission the problem occurred, the only external scripts are the different loadouts, but i experimented a lot with different commands and did all of this in the init-lines of game-logics or objects and because i'm relatively new to all of this, i guess that all this little scripting (creating lights, attaching lights to objects and other relatively "basic" stuff) though working fine might be a bit "messy". did a lot of copy+paste there...

but i will definitely try out your approach! although i now think that cleaning up the rest of the mission may do the trick, i will still try out your code and especially try to fully understand it. i'm still learning ;)

so, thanks again!

Edited by twistking

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  

×