# Executing script

## Recommended Posts

Hello

I'm having trouble executing GKB_fnc_randomAAFire by Gekkibi. I am familiarising myself with scripting through a mission where the only objective is to sit back and watch random firefights, artillery and airstrikes in a city. I've successfully scripted several MLRS and artillery fire missions as well as Scud strikes - however I want to add some ambient AA fire Baghdad style. I've dug around and found Gekkibi's script however I am not sure on how to execute this script. The methods of nul = [] execVM "script.sqf" and execVM "script.sqf"; that I commonly use unfortunately do no work with this script. I have never used a script that has been written in this way. Searching GKB_fnc_randomAAFire with Google or on the Forums yields no results on how to use this script - so all in all, I am stuck.

Here is the script in question

/* ----------------------------------------------------------------------------------------------------

Function:
GKB_fnc_randomAAFire

Author:
Gekkibi

Terms for copying, distribution and modification:
Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)

Description:
Defined AA-batteries shoot coherently to a randomly selected position, simulating anti-air fire
without creating any in-game aircraft. Script will make sure they will not run out of ammunition.
AA-batteries can be stationary or mobile. Batteries will stop firing at the fake target if they
spot a real one, and continues normally afterwards.

Parameters:
1st Objects in Array AA-units
2nd Arrays in array Target is created between these coordinates
[[x1, x2], [y1, y2], [z1, z2]]
3rd Number Delay between targets
4th Number Secondary weapon chance (from 0 to 1). Use 0 if no such weapon
5th Code When true new targets are calculated

Returns:
Nil

Example:
[
[GKB_aa1, GKB_aa2, GKB_aa3],
[[0, 4500], [7000, 8000], [1000, 500]],
20 + random 100,
0.1,
] spawn GKB_fnc_randomAAFire.sqf;
---------------------------------------------------------------------------------------------------- */

if (isServer) then {
private ["_positionArray", "_runCondition", "_secondaryChance", "_sleepTime", "_unitArray"];
_unitArray = _this select 0;
_positionArray = _this select 1;
_sleepTime = _this select 2;
_secondaryChance = _this select 3;
_runCondition = _this select 4;
{
_x lock 3;
} forEach _unitArray;
while _runCondition do {
private ["_direction", "_weapon"];
{
if (! alive gunner _x) then {
_unitArray = _unitArray - [_x];
};
} forEach _unitArray;
if (count _unitArray == 0) exitWith {};
_direction = [
(_positionArray select 0 select 0) - random ((_positionArray select 0 select 0) - (_positionArray select 0 select 1)),
(_positionArray select 1 select 0) - random ((_positionArray select 1 select 0) - (_positionArray select 1 select 1)),
(_positionArray select 2 select 0) - random ((_positionArray select 2 select 0) - (_positionArray select 2 select 1))
];
if (random 1 > _secondaryChance) then {
_weapon = "cannon";
} else {
_weapon = "missile";
};
{
if (alive gunner _x && isNull assignedTarget _x) then {
[_x, _direction, _weapon] spawn {
private ["_direction", "_unit", "_weapon"];
_unit = _this select 0;
_direction = _this select 1;
_weapon = _this select 2;
_unit doWatch _direction;
sleep 2;
switch (_weapon) do {
case "cannon": {
for "_x" from 1 to 1 + ceil random 5 do {
sleep random 2;
for "_x" from 1 to 10 do {
if (! isNull assignedTarget _unit) exitWith {};
_unit fire (_unit weaponsTurret [0] select 0);
sleep 0.05;
};
if (! isNull assignedTarget _unit) exitWith {};
_unit doWatch _direction;
};
};
case "missile": {
sleep (2 + random 5);
_unit fire (_unit weaponsTurret [0] select 1);
};
};
if (isNull assignedTarget _unit) then {
_unit doWatch objNull;
} else {
_unit doWatch assignedTarget _unit;
};
_unit setVehicleAmmo 1;
};
};
} forEach _unitArray;
sleep _sleepTime;
};
};

I have not gotten to arrays like this yet - so I am a bit puzzled on how I can execute this .sqf. I would message Gekkibi but he hasn't been online for 2 years, and I don't want to bump a 3 year old thread.

It is probably something absurdly simple that I have missed - hah.

Thanks.

##### Share on other sites

You may be right... since this is a function it must be compiled before you can say "spawn GKB_fnc_randomAAFire" as in the example. <EDIT: try it with no ".sqf" here>

Assuming that example syntax is correct, try adding this to the file "init.sqf" in your mission directory:

GKB_fnc_randomAAFire = compile preprocessfile "path\to\the\script.sqf";


Where, of course, you insert the actual relative path from your mission directory.  I.e if it isn't in a subfolder, just write "scriptname.sqf" (whatever the filename is).

EDIT: I'm not sure that example syntax IS correct; try dropping the ".sqf" when you actually spawn it.

EDIT 2: backslashes

##### Share on other sites

You may be right... since this is a function it must be compiled before you can say "spawn GKB_fnc_randomAAFire" as in the example. <EDIT: try it with no ".sqf" here>

Assuming that example syntax is correct, try adding this to the file "init.sqf" in your mission directory:

GKB_fnc_randomAAFire = compile preprocessfile "path\to\the\script.sqf";


Where, of course, you insert the actual relative path from your mission directory.  I.e if it isn't in a subfolder, just write "scriptname.sqf" (whatever the filename is).

EDIT: I'm not sure that example syntax IS correct; try dropping the ".sqf" when you actually spawn it.

EDIT 2: backslashes

Howdy.

I still cannot get the editor to accept the script for some reason - whenever I input the syntax into the init of a unit or a trigger, I simply get "Missing ;".

I'm not sure how to get this script to initialise - whether I plop it in the Activation of a Trigger or the Init of a unit.

##### Share on other sites

In that case you can just try:

_nil = [

[GKB_aa1, GKB_aa2, GKB_aa3],
[[0, 4500], [7000, 8000], [1000, 500]],
20 + random 100,
0.1,
{alive GKB_radar}] spawn GKB_fnc_randomAAFire;

If you add "_nil =" at the beginning it won't be trying to return anything to the game where it isn't supposed to.

(of course, you will need to replace the parameters with things that make sense in your case... I presume you don't have any of the units GKB_aa1.. or GKB_radar unless you named them that to work with this specifically)

##### Share on other sites

In that case you can just try:

_nil = [

[GKB_aa1, GKB_aa2, GKB_aa3],
[[0, 4500], [7000, 8000], [1000, 500]],
20 + random 100,
0.1,
{alive GKB_radar}] spawn GKB_fnc_randomAAFire;

If you add "_nil =" at the beginning it won't be trying to return anything to the game where it isn't supposed to.

(of course, you will need to replace the parameters with things that make sense in your case... I presume you don't have any of the units GKB_aa1.. or GKB_radar unless you named them that to work with this specifically)

.Right. I've had success with the _nil promopt - but I am still unsure how to activate this script. I'm trying to get this to work with 3 ZU23s. So at the moment I have a trigger set up with

_nil = [

[GKB_aa1, GKB_aa2, GKB_aa3],
[[0, 4500], [7000, 8000], [1000, 500]],
20 + random 100,
0.1,


in the Trigger's Acvtication. I've renamed the three guns to their respective names and synced them to the trigger. I am still not sure about alive GKB_Radar though.

This seems all rather complex.

##### Share on other sites

If you add "_nil =" at the beginning it won't be trying to return anything to the game where it isn't supposed to.

Not true, when calling a function the handle will return the return value from the function, when used by a spawn it can be used in combination with scriptDone _handle or isNil _handle to determine if the spawned function has finished.

You see all kind of examples in tutorials using _nul =, _nil =, or even 0 =.

No idea where that came from, call and spawn always return their respective values.

Cheers

##### Share on other sites
_nil = [

[GKB_aa1, GKB_aa2, GKB_aa3],
[[0, 4500], [7000, 8000], [1000, 500]],
20 + random 100,
0.1,
{alive GKB_aa1}] execVM "GKB_fnc_randomAAFire.sqf";


That works flawlessly for me. Just put it in a trigger. Sounds awesome with the upgraded sound engine ;)

##### Share on other sites

Not true, when calling a function the handle will return the return value from the function, when used by a spawn it can be used in combination with scriptDone _handle or isNil _handle to determine if the spawned function has finished.

You see all kind of examples in tutorials using _nul =, _nil =, or even 0 =.

No idea where that came from, call and spawn always return their respective values.

Cheers

What? The editor won't even allow you to write "this spawn {}" in a trigger activation or init field.  You HAVE to assign it to something, '_nil' is a convention because the results are not used in this case.

_nil = this spawn {};


is fine, but

this spawn {};


won't even let you close the dialog window.

EDIT: specifically, the error message is:

Init: Type Script, expected Nothing

What I meant was, the game will not allow you to return a variable from whatever you insert into Init.  It expects "Nothing" [which is precisely what an assignment (to _nil or anything else) returns]

EDIT 2:

I can see how in some contexts it may be desirable to wait for the script to finish (or check its value) during init.  In that case, you're right, assignment to _nil is the wrong pattern.  It is perfectly fine in most cases, though, and certainly better than "nil =" or "0 =" which are plainly invalid.

EDIT 3:

Perhaps what we should start doing is "_dontCare =" or "_handle ="... I did that for a while, but honestly, "_nil =" is just a lot shorter, clearer than "_dc =", and works just as well for something that's never going to be seen again.