Jump to content
Sign in to follow this  
KanePayne

Spawned unit Attacks!

Recommended Posts

I try and try, but I cant figure out, how to make spawned unit go to nearest unit from list [s1,s2...Techpriest] and attack it.

Share this post


Link to post
Share on other sites
I try and try, but I cant figure out, how to make spawned unit go to nearest unit from list [s1,s2...Techpriest] and attack it.

If it's some kind of WH40k units, you'd better post this in their topic.

Share this post


Link to post
Share on other sites

:/

Im asking for solution of my problem, I just want to know is there anything like

_this comandtarget/attack/whatever [s1,s2,s3]

I look everywhere and cant find solution in google or this forum.

I figured out first part, but still cant make nearest target detector.

Edited by KanePayne

Share this post


Link to post
Share on other sites

Ok, so you'll need the findunit.sqf function, which can be found on OFPEC or just copy and paste this in a "findunit.sqf" script file :

/*findUnits.sqf

By General Barron

aw_barron@hotmail.com

11/15/03

This script returns an array of all units within a specified distance

of another unit that are in the passed array. The result is sorted

in acsending order of distance to the calling unit. Will never return the

unit originally passed to the function. To call:

[unit, range, [unit array]] call findUnits

Usage example: make a trigger, covering entire map, activation = once, "east present". On activation = "east_units = thisList"

Make another trigger. Condition = "count ([player, 50, east_units] call findUnits) > 0". On activation = "hint "the enemy is near" "

The hint will appear once the player comes within 50 meters of an east unit.

*/

private ["_unit", "_range", "_list", "_found", "_numArray","_j","_k","_tempVal","_tempNum","_element"];

_unit = _this select 0;

_range = _this select 1;

_list = _this select 2;

_found = [];

{if (_x distance _unit <= _range && _x != _unit) then {_found = _found + [_x]}} foreach _list;

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

Start bubble sort (sort units from closest to farthest)

Code taken from sortBubble.sqf

By bn880 2/24/2003

(slightly modified to fit in this function)

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

_count = count _found;

_numArray = [];

_numArray resize _count;

// ACQUIRE NUMERIC VALUES FROM EVALUATION

_j = 0;

while {_j < _count} do

{

_element = _found select _j;

_numArray set [_j,_element distance _unit];

_j = _j + 1;

};

_j = 0;

// SORT

while {_j < _count -1} do

{

_k = _j + 1;

while {_k < _count} do

{

if (_numArray select _j > _numArray select _k) then

{

_tempNum = _numArray select _j;

_numArray set [_j,(_numArray select _k)];

_numArray set [_k,_tempNum];

_tempVal = _found select _j;

_found set [_j,(_found select _k)];

_found set [_k,_tempVal];

};

_k = _k + 1;

};

_j = _j + 1;

};

Then the command to make your unit move to the nearest unit of the given array are the following script lines :

myarray = [s1,s2...Techpriest]
_closest = ([_unit, 9999, my_array] call findUnits) select 0
_unit domove getpos _closest

Share this post


Link to post
Share on other sites

Could you guide me a bit more? I have everything set, no errors, but spawned unit still doesnt move.

Share this post


Link to post
Share on other sites

There was a typo here :

my[color="Red"]_[/color]array = [s1,s2...Techpriest]
_closest = ([_unit, 9999, my_array] call findUnits) select 0
_unit domove getpos _closest

Anyway, could you post your spawning script here ? The unit must be out of a group i guess, so you may add some :

[_unit] join grpnull

after the spawning.

Share this post


Link to post
Share on other sites

"SoldierE" createUnit [spawn1,WAVE1,"M1=this",1,"PRIVATE"]; [this] exec "Hunt.sqs"

Thats all.

Share this post


Link to post
Share on other sites

"SoldierE" createUnit [spawn1,WAVE1,"M1=this",1,"PRIVATE"]; [[color="Red"]M1[/color]] exec "Hunt.sqs" 

By the way the hunt.sqs should look like :

_unit = _this select 0
my_array = [s1,s2...Techpriest]

[_unit] join grpnull
_closest = ([_unit, 9999, my_array] call findUnits) select 0
~1
_unit domove getpos _closest

exit

Edited by ProfTournesol

Share this post


Link to post
Share on other sites

Thnx, works fine now.

Maybe I'll prepare template & put somewhere.

EDIT:

I tested It and AI goes to first unit from

my_array = [s1,s2...Techpriest]

No matter the distance

Edited by KanePayne

Share this post


Link to post
Share on other sites
Thnx, works fine now.

Maybe I'll prepare template & put somewhere.

EDIT:

I tested It and AI goes to first unit from

No matter the distance

Mmm... could you post the exact content of my_array ?

Share this post


Link to post
Share on other sites
my_array = [s1,s2,Techpriest]

If I swicht s1 with s2 AI will go to s2

Share this post


Link to post
Share on other sites

Sorry i'm stupid. Create an init.sqs file in your mission, and add this into it :

findUnits = preprocessFile "findUnits.sqf"

Share this post


Link to post
Share on other sites

Nope, still nothing, they still go to the first guy in my_array, no matter the distance.

Share this post


Link to post
Share on other sites

First off; I'm not sure I'm seeing/getting all of KanePayne's info...

But from what I see...

Why is the below setup like this?:

_closest = ([_unit, 9999, my_array] call findUnits) select 0

Doesn't this select 0 - mean select the first unit?

This script returns an array of all units within a specified distance

of another unit that are in the passed array. The result is sorted

in acsending order of distance to the calling unit. Will never return the

unit originally passed to the function. To call:

[unit, range, [unit array]] call findUnits

Edited by WW2Weasel

Share this post


Link to post
Share on other sites
First off; I'm not sure I'm seeing/getting all of KanePayne's info...

But from what I see...

Why is the below setup like this?:

_closest = ([_unit, 9999, my_array] call findUnits) select 0

Doesn't this select 0 - mean select the first unit?

This script returns an array of all units within a specified distance

of another unit that are in the passed array. The result is sorted

in acsending order of distance to the calling unit. Will never return the

unit originally passed to the function. To call:

[unit, range, [unit array]] call findUnits

Nope, the function works very well, and as you can see in the enlighted sentence in red, it's because the units are sorted according to the distance to the calling unit. Moreover, it works flawlessly for me, i just tested it.

@KanePayne : Do you want me to upload my test somewhere so you can check what's the trouble ?

Share this post


Link to post
Share on other sites

Works perfect, just combine it with body removal & its good to go.

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  

×