Jump to content

Recommended Posts

Hi

 

I sure help someone can shed some light - I'm making a mission where I want several random tasks to be created at random marker positions. With this I have found a way which thus far proved very effective.
I created 15 markers which I placed in appropriate positions for each task. Then an officer-unit, named S1, which I placed out of the way (simulation and damage disabled)  until the script is called.

The working script looks like this:

 

//     SCRIPT CODE    //

//Place officer (s1) at random marker ("red1"-"5") and start him patrol the area. Only markers and unit placed out of the way. Once the unit (s1) has been placed, another script (USPS.sqf) then gives the unit random waypoints to patrol. After that, another script (PWNR) spawns in a support group, also patrolling the area. Task complete when s1 is killed.

 

_taskArray = ["red_1","red_2","red_3","red_4","red_5","red_6","red_7","red_8","red_9","red_10","red_11","red_12","red_13","red_14","red_15"];  //array with marker names
_taskPos = _taskArray call BIS_fnc_SelectRandom; //select a random spawn position
_officer1 = s1;
s1 setPosATL getMarkerPos _taskPos, 100;

[player,"Task_1",["A CSAT officer is around this area","Assassinate the officer","Kill"],_officer1,True] call BIS_fnc_taskCreate;
sleep 0.5;

s1 hideObject false;
s1 allowDamage true;
s1 setCaptive false;

null = [s1, 100, 200, _taskPos, _taskPos, 200, true, "SAFE", "RED", "LIMITED", "FILE", 30, 30, 0, [true,35,25,3,1]] execVM "scripts\USPS.sqf";
sleep 0.5;
nul = [_taskPos,200,"EAST GUERILLA INFANTRY UNITS",8] execVM "PWNR\Scripts\PUGS.sqf";

while {alive s1} do
    {
        waitUntil {not alive s1};
        ["Task_1","Succeeded"] call BIS_fnc_taskSetState;
    };

As I said, this works fine as long as the unit (S1) and markers is placed in the editor.
My objective: I want to create the same task WITHOUT placing the unit in the editor, but rather spawning him in with <createUnit>. In this regard I have made the following script:

 

// Task: Kill HVT

_hvtArray = ["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"];//Set location array

_hvtPos = _hvtArray call BIS_fnc_SelectRandom; //Select random position from array

_hvtGroup = createGroup EAST; // Create HVT group

_hvt = "O_Officer_F" createUnit [position _hvtPos, _hvtGroup]; // Spawn HVT


[player,"Eliminate_HVT",["We have pinpointed the position of a High Value Target in the indicated area. You are to relocate and eliminate the HVT.","High Value Target","Kill"],_hvt,True] call BIS_fnc_taskCreate;

while {not isNull _hvt} do
    {
        waitUntil {not alive _hvt};
        ["Eliminate_HVT","Succeeded"] call BIS_fnc_taskSetState;
    };

//The script exits with the following error refering, I think, to line 4 : <_hvt = "O_Officer_F" createUnit [position _hvtPos, _hvtGroup]; > and neither the unit is spawned, or the task is created.

"Error position, Type String, Expected object, position"

 I have very little scripting experience but I'm learning fast.  What am I doing wrong here?

Share this post


Link to post
Share on other sites

_hvt = "O_Officer_F" createUnit [getMarkerPos _hvtPos, _hvtGroup];  ... if "hvt_1","hvt_2","hvt_3",... are markers (should be).

  • Like 3

Share this post


Link to post
Share on other sites

@Theo Thalwitzer,
This looks good,

Spoiler

_hvtArray = ["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"];

_hvtPos = selectRandom _hvtArray;

_hvtGroup = createGroup EAST;

"O_Officer_F" createUnit [getMarkerPos _hvtPos, _hvtGroup]; 

 

Check this out,

Spoiler

MMF_fnc_tasker=
{	params [["_name", "task_1"], ["_type", "Objective"], ["_pos", objNull], ["_state", "Assigned"], ["_grp", grpNull]];

if (_state isEqualTo "SUCCEEDED") exitWith {[_name,_state] call BIS_fnc_taskSetState;  [ _name ] call BIS_fnc_deleteTask;};
	[true,[_name],[_type,_type,_type],objNULL,1,3,false] call BIS_fnc_taskCreate;
	[_name,_state] call BIS_fnc_taskSetState;

if !(_grp isEqualTo grpNull) exitWith {[_name,[_pos, true]] call BIS_fnc_taskSetDestination};

};

["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker;
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker; 

all together,


you_fnc_HVT= 
{ params [["_locARR", []], ["_side", playerSide], ["_type", "O_Officer_F"]]; 
 
private _pos = selectRandom _locARR; 
 
private _grp = createGroup _side; 
 
_type createUnit [getMarkerPos _pos, _grp];

private _unit = leader _grp; 
 
["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker; 
 
waitUntil {sleep 1; !alive _unit}; 
 
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker;  
}; 
 
hvt_array=["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"]; 
[hvt_Array, EAST, "o_officer_f"] spawn you_fnc_hvt;

*edited
**tested


Have fun!

  • Like 1

Share this post


Link to post
Share on other sites
On 9/22/2020 at 7:28 AM, pierremgi said:

_hvt = "O_Officer_F" createUnit [getMarkerPos _hvtPos, _hvtGroup];  ... if "hvt_1","hvt_2","hvt_3",... are markers (should be).

That is correct, they are markers. But I still get an error saying: Error Undefined variable in expression: _hvt
This suggests that the variable name "_hvt" has not been assigned to the unit?
Like I said I'm only slightly literate in .sqf coding and have much to learn

Share this post


Link to post
Share on other sites
On 9/22/2020 at 8:45 AM, wogz187 said:

@Theo Thalwitzer,
This looks good,

  Reveal hidden contents


_hvtArray = ["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"];

_hvtPos = selectRandom _hvtArray;

_hvtGroup = createGroup EAST;

"O_Officer_F" createUnit [getMarkerPos _hvtPos, _hvtGroup]; 

 

Check this out,

  Hide contents


MMF_fnc_tasker=
{	params [["_name", "task_1"], ["_type", "Objective"], ["_pos", objNull], ["_state", "Assigned"], ["_grp", grpNull]];

if (_state isEqualTo "SUCCEEDED") exitWith {[_name,_state] call BIS_fnc_taskSetState;  [ _name ] call BIS_fnc_deleteTask;};
	[true,[_name],[_type,_type,_type],objNULL,1,3,false] call BIS_fnc_taskCreate;
	[_name,_state] call BIS_fnc_taskSetState;

if !(_grp isEqualTo grpNull) exitWith {[_name,[_pos, true]] call BIS_fnc_taskSetDestination};

};

["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker;
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker; 

all together,



you_fnc_HVT= 
{ params [["_locARR", []], ["_side", playerSide], ["_type", "O_Officer_F"]]; 
 
private _pos = selectRandom _locARR; 
 
private _grp = createGroup _side; 
 
_type createUnit [getMarkerPos _pos, _grp];

private _unit = leader _grp; 
 
["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker; 
 
waitUntil {sleep 1; !alive _unit}; 
 
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker;  
}; 
 
hvt_array=["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"]; 
[hvt_Array, EAST, "o_officer_f"] spawn you_fnc_hvt;

*edited
**tested


Have fun!

Wow! This is a lot to understand but I'll work though it. Thanks
I tried this and it works nicely with one error: Undefined variable in expression _unit. Also the task doesn't end when the officer gets killed...

Sorry I'm a little lost here.

Share this post


Link to post
Share on other sites

I'm taking a guess here but I suspect the "undefined variable"-error may be eliminate by giving the spawned unit a new name perhaps?
As I mentioned, as long as the officer is named the script works fine via the unit's name. I know its possible to name a spawned unit with setVehicleVarName.
I have made another script: (Where 'tareaGrn' is a placed helipad.
The full script looks like this:

 

//Start of script
if (!alive tareaGrn) then
{
    hint "The distribution point has been destroyed";
}
else

    {
_defenceGrpG = createGroup Independent;

testGroupB = _defenceGrpG;


//hint "Spawning New Assault Team...";
                _newSoldier1 = "PMC_SecurityCon_MXGL" createUnit [position tareaGrn,testGroupB];
                sleep 1;
                
                _newSoldier2 = "PMC_FieldSpecialist_M249" createUnit [position tareaGrn,testGroupB];
                sleep 1;

                _newSoldier3 = "PMC_Marksman_M14" createUnit [position tareaGrn,testGroupB];
                sleep 1;

                _newSoldier4 = "PMC_SecurityCon_MX" createUnit [position tareaGrn,testGroupB];
                sleep 1;

                _newSoldier5 = "PMC_Medic" createUnit [position tareaGrn,testGroupB];
                sleep 1;

                _newSoldier6 = "PMC_TL_M4" createUnit [position tareaGrn,testGroupB];
                sleep 1;

                _newSoldier7 = "PMC_Engineer" createUnit [position tareaGrn,testGroupB];
                sleep 2;

                _newSoldier8 = "PMC_SecurityCon_MX" createUnit [position tareaGrn,testGroupB];
                sleep 1;
//        hint "TeamSpawn Complete!";
testGroupB = group pmcl;


    };
    sleep 2;
    
    _newSoldier1 setVehicleVarName "pmc1";
    _newSoldier2 setVehicleVarName "pmc2";
    _newSoldier3 setVehicleVarName "pmc3";
    _newSoldier4 setVehicleVarName "pmc4";
    _newSoldier5 setVehicleVarName "pmc5";
    _newSoldier6 setVehicleVarName "pmc6";
    _newSoldier7 setVehicleVarName "pmc7";
    _newSoldier8 setVehicleVarName "pmc8";
    
    hint "Renamed them";
    testGroupB setBehaviour "SAFE";


nul=[] execVM "randomMoveS3.sqf"; //Activates waypoint task on units

//End of script

As you can see I kept this as simple as possible and yes it works fine - so I'm guessing adding:
_hvt  setVehicleVarName "hvt_1";
This should rename the variable <_hvt> to <hvt_1> , which seems easier to define when the unit gets killed and completes the task. Not sure if it will work in MP though

Share this post


Link to post
Share on other sites

I also have another problem which has been stumping me for a while. Its a simple syntax issue I think:

While this line works fine:

_taskArray = ["red_1","red_2","red_3","red_4","red_5","red_6","red_7","red_8","red_9","red_10","red_11","red_12","red_13","red_14","red_15"]; 

I know its also possible to shorten that 15-name list by using; "red_%1" , which would then select all markers starting with "red_".
I tried: _taskArray = ["red_%1"]; but I know its not correct and I also know it should be pretty simple. I'm still learning ...

Share this post


Link to post
Share on other sites

@Theo Thalwitzer,

Quote

I tried this and it works nicely with one error: Undefined variable in expression _unit. Also the task doesn't end when the officer gets killed...

Let's take another look,

Spoiler

These functions can be loaded in init.sqf or the Attributes Init Dialog,


MMF_fnc_tasker=
{	params [["_name", "task_1"], ["_type", "Objective"], ["_pos", objNull], ["_state", "Assigned"], ["_grp", grpNull]];

if (_state isEqualTo "SUCCEEDED") exitWith {[_name,_state] call BIS_fnc_taskSetState;  [ _name ] call BIS_fnc_deleteTask;};
	[true,[_name],[_type,_type,_type],objNULL,1,3,false] call BIS_fnc_taskCreate;
	[_name,_state] call BIS_fnc_taskSetState;

if !(_grp isEqualTo grpNull) exitWith {[_name,[_pos, true]] call BIS_fnc_taskSetDestination};

};


you_fnc_HVT= 
{ params [["_locARR", []], ["_side", playerSide], ["_type", "O_Officer_F"]]; 
 
private _pos = selectRandom _locARR; 
 
private _grp = createGroup _side; 
 
_type createUnit [getMarkerPos _pos, _grp];

private _unit = leader _grp; 
 
["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker; 
 
waitUntil {sleep 1; !alive _unit}; 
 
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker;  
}; 

As functions they can be "called" or "spawned" repeatedly with different parameters.
To call the HVT function,


hvt_array=["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"]; 
[hvt_Array, EAST, "o_officer_f"] spawn you_fnc_hvt;

In this case we will "spawn" the function because it includes a timed command (waituntil).


Have fun!
 

  • Like 1

Share this post


Link to post
Share on other sites
8 hours ago, Theo Thalwitzer said:

But I still get an error saying: Error Undefined variable in expression: _hvt

As...

On 9/22/2020 at 3:13 PM, Theo Thalwitzer said:

_hvt = "O_Officer_F" createUnit [position _hvtPos, _hvtGroup];

...the version of createUnit you have used does not return a unit, so _hvt is nil. (see warning on linked command page)

Instead use...

_hvt = _hvtGroup createUnit[ "O_Officer_F", getMarkerPos _hvtPos, [], 0, "FORM" ];

 

  • Like 3

Share this post


Link to post
Share on other sites
23 hours ago, Larrow said:

As...

...the version of createUnit you have used does not return a unit, so _hvt is nil. (see warning on linked command page)

Instead use...


_hvt = _hvtGroup createUnit[ "O_Officer_F", getMarkerPos _hvtPos, [], 0, "FORM" ];

 

BINGO! Thanks a bunch! And it's right there on the wiki... What a tool I am... This is working flawlessly now. And suddenly I see a load of possibilities! Just one question though: What does the "FORM" represent?

Share this post


Link to post
Share on other sites
On 9/23/2020 at 7:11 AM, wogz187 said:

@Theo Thalwitzer,

Let's take another look,

  Hide contents

These functions can be loaded in init.sqf or the Attributes Init Dialog,



MMF_fnc_tasker=
{	params [["_name", "task_1"], ["_type", "Objective"], ["_pos", objNull], ["_state", "Assigned"], ["_grp", grpNull]];

if (_state isEqualTo "SUCCEEDED") exitWith {[_name,_state] call BIS_fnc_taskSetState;  [ _name ] call BIS_fnc_deleteTask;};
	[true,[_name],[_type,_type,_type],objNULL,1,3,false] call BIS_fnc_taskCreate;
	[_name,_state] call BIS_fnc_taskSetState;

if !(_grp isEqualTo grpNull) exitWith {[_name,[_pos, true]] call BIS_fnc_taskSetDestination};

};


you_fnc_HVT= 
{ params [["_locARR", []], ["_side", playerSide], ["_type", "O_Officer_F"]]; 
 
private _pos = selectRandom _locARR; 
 
private _grp = createGroup _side; 
 
_type createUnit [getMarkerPos _pos, _grp];

private _unit = leader _grp; 
 
["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker; 
 
waitUntil {sleep 1; !alive _unit}; 
 
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker;  
}; 

As functions they can be "called" or "spawned" repeatedly with different parameters.
To call the HVT function,



hvt_array=["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"]; 
[hvt_Array, EAST, "o_officer_f"] spawn you_fnc_hvt;

In this case we will "spawn" the function because it includes a timed command (waituntil).


Have fun!
 

 

9 hours ago, Theo Thalwitzer said:

BINGO! Thanks a bunch! And it's right there on the wiki... What a tool I am... This is working flawlessly now. And suddenly I see a load of possibilities! Just one question though: What does the "FORM" represent?

Thanks to everyone who helped with this! It is now thoroughly solved. Special thanks to wogz187 - your solution is exactly what I needed without adapting more than one line of code as simplicity and less lines of code is what I'm after.

It's great to have a community as selflessly helpful as this!

  • Like 1

Share this post


Link to post
Share on other sites
On 9/23/2020 at 10:11 PM, wogz187 said:

@Theo Thalwitzer,

Let's take another look,

  Hide contents

These functions can be loaded in init.sqf or the Attributes Init Dialog,



MMF_fnc_tasker=
{	params [["_name", "task_1"], ["_type", "Objective"], ["_pos", objNull], ["_state", "Assigned"], ["_grp", grpNull]];

if (_state isEqualTo "SUCCEEDED") exitWith {[_name,_state] call BIS_fnc_taskSetState;  [ _name ] call BIS_fnc_deleteTask;};
	[true,[_name],[_type,_type,_type],objNULL,1,3,false] call BIS_fnc_taskCreate;
	[_name,_state] call BIS_fnc_taskSetState;

if !(_grp isEqualTo grpNull) exitWith {[_name,[_pos, true]] call BIS_fnc_taskSetDestination};

};


you_fnc_HVT= 
{ params [["_locARR", []], ["_side", playerSide], ["_type", "O_Officer_F"]]; 
 
private _pos = selectRandom _locARR; 
 
private _grp = createGroup _side; 
 
_type createUnit [getMarkerPos _pos, _grp];

private _unit = leader _grp; 
 
["HVT_task", "DESTROY", _unit, "Assigned", group player] call MMF_fnc_tasker; 
 
waitUntil {sleep 1; !alive _unit}; 
 
["HVT_task", "DESTROY", objNull, "SUCCEEDED", group player] call MMF_fnc_tasker;  
}; 

As functions they can be "called" or "spawned" repeatedly with different parameters.
To call the HVT function,



hvt_array=["hvt_1","hvt_2","hvt_3","hvt_4","hvt_5"]; 
[hvt_Array, EAST, "o_officer_f"] spawn you_fnc_hvt;

In this case we will "spawn" the function because it includes a timed command (waituntil).


Have fun!
 

Hi!
I'm very bad at writing a script, since I'm far from programming at all, but yours looks amazing. Please tell me how to add random spawn generation of various units from a pre-created array to your script?
All the best to you.

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

×