Jump to content
Sign in to follow this  
Tom_Pynchon

Help with "FindNearest" code

Recommended Posts

Hi.

I’ve got members of an OPFOR patrol – let’s call the group “eg1†-- scattered in separate ambush positions throughout a small village. I’ve got the following code on the group leader’s init line:

{_x  disableAI “ANIMâ€} foreach units eg1;

I’d like to enable AI “ANIM†each time a BLUFOR unit is within 10 meters of any member of the OPFOR patrol. I presently have a BLUFOR activated trigger with the following code on the condition field:

(player findNearestEnemy position player) distance player < 10;

On the “On Act†field I have:

{_x enableAI “ANIMâ€} foreach units eg1;

The effect is that each time my BLUFOR AI is within 10 meters of an enemy unit, then animations for the entire OPFOR patrol are enabled.

This is well and good for other purposes, I suppose. What I’d like, however, is a trigger configuration that enables “ANIM†only for those OPFOR units that are within 10 meters of any member of my BLUFOR team.

The effect I’d like is for the members of the OPFOR patrol to “come alive†one-by-one as the BLUFOR patrol walks through the village. (Again, I'd like the trigger to work for all members of my BLUFOR team.)

I've tried searching the site. It helped to narrow the focus a little on the sort of trigger I would need -- but I haven't found the trigger configuration to produce the effect I had described above.

Any help would be greatly appreciated.

Thanks in advance.

Share this post


Link to post
Share on other sites

A few weeks back there was a thread about a script where the enemy would ignore blufor till they were 30 some meters away or something similar. 10 meters is realllly close.

Share this post


Link to post
Share on other sites

I have this, it seems to work.

I'd like to remove the second findnearest as it's doing the same thing twice.

Done it and updated it.

// null=[] execvm "find.sqf";

while {alive player} do {
{
_who = _x findNearestEnemy position _x;

if (_who distance _x < 10) then {

_who enableAI "ANIM";

};

} foreach units player;
sleep 1;
};

Edited by F2k Sel

Share this post


Link to post
Share on other sites

Keep an array of all "sleeping" OPFOR and all BLUFOR team members:

_bluforUnits = units group player;
_opforUnits = list OpforPresentTrigger; // a trigger with OPFOR Present activation

while{true}do{
{
  {
     _opfor = _X; // current opfor unit
     _closeBluforCount = {(alive _X) && (_opfor distance _X < 10)} count _bluforUnits;
     if(_closeBluforCount > 0)then{
        _opfor enableAI "ANIM"; // wake up
     };
  } forEach _opforUnits;
};

Share this post


Link to post
Share on other sites

Thanks all for your replies.

Both the above suggestions look good and solid. I'll try them both -- or at least attempt to extract what I need from them so that I can have en editor-based trigger do the job, if at all possible. My PC struggles when I have .sqf scripts running in the background.

@ kylania:

Yeah, ten meters is pretty close. I hadn't realized that. Heh-heh.

Again, thanks all for taking time to reply.

Edited by Tom_Pynchon

Share this post


Link to post
Share on other sites

Hi guys. Just wanted to get back to you regarding the subject code.

The scripts worked fine. Thanks for your help. As I had mentioned, however, my PC struggles when I have .sqf files running in the background. (I'm guessing it's a CPU bottleneck.) I played around with the editor a bit and found that the only way I could -- with the little that I know about codes and scripts and editing in general -- pull off the effect I described above without an .sqf file was to make separate triggers for each OPFOR unit on the editor.

I put the following code on the OPFOR group leader's init field:

{_x setcaptive true} foreach units [i]OPFORgroupname[/i];{_x disableAI "ANIM"} foreach units [i]OPFORgroupname[/i];

(I had to set them captive as my BLUFOR patrol kept spotting -- and shooting -- them from a long ways off, in spite the fact that they were hidden inside buildings or behind walls and such.)

I then made one trigger for each member of the OPFOR group using the following condition for each:

([i]soldiername[/i] findNearestEnemy position [i]soldiername[/i]) distance [i]soldiername [/i]< 30;

In the On Act fields of each I put:

[i]soldiername[/i] setcaptive false; [i]soldiername[/i] enableAI "ANIM";

This worked, too. The OPFOR units "woke" one-by-one as my BLUFOR patrol went through the village in the dark -- the bonus was that the action was fun and unpredictable. But then I wouldn't recommend the editor-based method for big groups as the work involved is intolerably tedious.

I'm still working on the mission. I'm guessing I'll end up using an .sqf file, in any case, as I want to prolong the gunfight a bit and that means I'm going to need a bigger OPFOR group.

Anyways, just posting as a way to thank you guys again for your help. Also, there just might be some fellow noob out there for whom the above codes may be of some help.

Edited by Tom_Pynchon

Share this post


Link to post
Share on other sites

Not sure why you use two forEach?

{_x setcaptive true; _x disableAI "ANIM"} foreach units OPFORgroupname;

Share this post


Link to post
Share on other sites

Btw that looked faulty too:

“ANIM†=> "ANIM" (note the different quotes)

Share this post


Link to post
Share on other sites

I'm surprised that scripting would be slower than using triggers.

You usually have more control over a script.

Share this post


Link to post
Share on other sites
Not sure why you use two forEach?

{_x setcaptive true; _x disableAI "ANIM"} foreach units OPFORgroupname;

I'm trying to get a specific flow of action in the mission. I want a CQC kind of thing going where the enemies come out firing from inside buildings and the like one-by-one -- at very close range, as though both sides had caught each other by surprise.

I kept running into a strange problem, though. My AI teammates kept spotting and calling out enemy positions even though the OPFOR units were completely hidden. It's like my AI teammates could see through walls -- in the dark -- from a long ways off, like no human being can possibly do in real life. The OPFOR units were being killed from around 150 meters away or so. So I couldn't get the close-quarter, down and dirty firefight I wanted.

I'm new to the editor and relatively new to ARMA. I couldn't find a way around the problem. And, since I couldn't eliminate my teammates' x-ray vision, I went for the sneaky, inelegant and crude alternative: I used setcaptive true so that my team would only recognize them as enemies at > 30.

Yeah, I know, it looks real ugly and -- well, moronic. Any suggestions? Again, I'm a total noob -- and a slow one at that -- so I'd really appreciate the help.

Edited by Tom_Pynchon

Share this post


Link to post
Share on other sites

sorry I see you tried what I was going to suggest.

Edited by F2k Sel

Share this post


Link to post
Share on other sites

It will be hard to get a really genuine cqb feel with the AI, they just seem a little too hyper-aware as if calibrated for the 100m+ battle.

Share this post


Link to post
Share on other sites

@ F2K Sel regarding scripting vs triggers: I know what you mean. But for some reason my PC struggles when I have .sqf files running within a game. My framerates take plunges of about 10 FPS on average -- worse in urban portions of the maps. I didn't have that problem when I had my CPU overclocked. But I've had to do away with my overclock profile since moving to the tropics because of very bad heat-related overall performance issues. So I'm guessing it's a CPU bottleneck.

@ PvPscene: Whoa! There's a difference? Man, I'm yet unused to all the little nuances and details. Those are, for me, the toughest to learn. And, as I'm sure you can tell, I've got a lot of learning to do.

Share this post


Link to post
Share on other sites

I just tested what CG had posted {_x setcaptive true; _x disableAI "ANIM"} foreach units OPFORgroupname; and this seems to work perfectly. My men walk right upto OPFOR without saying a word.

Share this post


Link to post
Share on other sites
I just tested what CG had posted {_x setcaptive true; _x disableAI "ANIM"} foreach units OPFORgroupname; and this seems to work perfectly. My men walk right upto OPFOR without saying a word.

Yeah, it worked great. Shorter, too. I didn't know you could put two separate lines of code inside a single pair of parentheses -- one fell swoop -- like that. Will make a lot of other things easier.

Hey, guys, thanks and thanks again for all the help.

While you may have just commented and lent your know-how to one particular item, the lines of code, configurations, syntaxes and whatnot offer a wealth of learning possibilities -- much gratitude here.

Edited by Tom_Pynchon

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  

×