Jump to content
Sign in to follow this  
kocrachon

Check all M2s.

Recommended Posts

So right now I am trying to write a script that checks every M2 static defense on the map, and with that I want to make a script that also checks its ammo, and if its low on ammo, automatically reloads it with ammo. Giving me a base defense with unlimited ammo (these are manned by AI of course)

Anyone able to help me out as to how to make this work? I never made a script that checked all of a single type of object before... I know how to do the ammo check.. and how to refil the ammo. I guess the real question is, how do I set it up to check every M2 for said ammo?

Edited by HavocDemon

Share this post


Link to post
Share on other sites

Two options:

1) For every M2 you place in the editor (or spawn per script), you run the ammo-check logic by either starting the script from the init-line of your M2 in the editor or per execVM for spawned ones. The script should check the ammo at a generous interval and exit if it's dead. If on the other hand, the M2 is out of ammo and still alive, you have again two options: either you handle the reammo right here in this script and you get magic reammo. The other solution would be to register your M2 being out of ammo to a global array. Then you have another extra script running, which checks if this array is empty, if not, it pops the first M2 from the array and then you send an ammo-truck from another base/storage to go and supply the M2 - then going to the next empty M2, or back to the base/storage, whatever.. You could even setup multiple ammo-trucks on duty. If you go for the latter, make sure you script it in such a manner, that also other vehicles can be resupplied! And there you have a nice, reuseable script. ;)

2) The second solution would be to iterate over allUnits and check with the command `isKindOf` if the unit/vehicle is of type M2 (or whatever the class name is exactly). Then you have the choice, to either attach a listener-script like in 1), or you can manage all from this script, reiterating over allUnits again and again... though I wouldn't suggest that, for it's quite unneccessary (ok you wouldn't need to check if a unit is still alive..).

I really recommend the second option:

1) iterate over allUnits once at init-time (in FN_autoSupplyAllUnits.sqf). This is not only alot easier, but also alot better, than writing the same init-line for each unit you wanna register. In case you wanna change something later, you can do it at one place, instead of all those init-lines. Programmers are lazy. ;)

2) run your FN_autoSupplyVehcile.sqf (which you need to write) for each allUnits

3) in FN_autoSupplyVehicle, you first check for the type of the given unit/vehicle and attach a listener-script to it or exit.. I'd do this check here, (and make an extra function for this), so you can run this function over single units, later, maybe for spawned units. (thus this logic isn't burried in the FN_autoSupplyAllUnits.sqf)

4) write your listener-scripts like

while (alive _veh) do {
  // 1) check if out of ammo, if do something (direct/magic reammo OR put on supplyList)
  // 2) maybe another check to call a repair- or fuel-truck
  //    ^^ maybe make an extra parameter to activate such extra checks..
  // 3) sleep for a reasonable time (simulate either the time needed to reammo the magic way, or the time needed to remark and reporting the failure.. also I suggest to randomise the sleeptime for some seconds, so all these listening scripts will distribute better over time and not run/check all at the same time/interval)
};

If you wanna do that in a reuseable form, then why not something totally generic like this, which you could use for any sort of listeners:

## SCRIPT_objectListener.sqf


  _obj = _this select 0;
  // array of array[check-function, action-function]
  _checks = _this select 1;

  while (alive _obj) do
  {
     // run all passed/registered checks
     {
        if (_obj call (_x select 0)) then
        {
           // run action
           _obj call (_x select 1);
        };
        // little sleep between each checks
        sleep 1.3;
     } forEach _checks;

     // sleep
     sleep (8 + (random 4));
  };

  // dead/exit (you may wanna implement a cleanup-function, why not passed as an optional parameter to this script)

  // btw. also the (alive _obj) part could be made variable/passed in by another parameter.. makeing this totally generic (you could run a listener only for some time then, or make it while(true) ... and then pass an exit-check-function along.., you may even make this optional and have a default exit-check... )

### Thus in another file:

// array of supply listeners (array of check and action)
// both functions gets the vehicle passed as _this
_supplyListeners = [
  // ammo check
  [
     { !(_this someAmmo) },
     { /* reammo logic*/ }
  ],
  // damage check
  [
     { (damage _this > 0.1) },
     { /* repair logic*/ }
  ]
];

// attach listener to object(s) (either for single units, or in while iterating over allUnits (+ some type filter...))
[_veh, _supplyListeners] execVM SCRIPT_objectListener.sqf

^^ code has never been run and is not guaranteed to work. But something along those lines should get you there..

Share this post


Link to post
Share on other sites

Ok I am slightly confused, I have a script writen for the most part but the part I am failing at is the actual "check ammo" count. How do I script "If ammo is at .5 then". Because right now I cant seem to get an ammo check to work.

Share this post


Link to post
Share on other sites

does anyone know how to write

if ammo is < .5

or something a long those lines ot initiate a script?

such as

M2 ammo < .5?

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  

×