Jump to content
Archosaurus

findNearestEnemy then check result?

Recommended Posts

Hello. I need your help once again, forums.

 

I want to use findNearestEnemy to check if a squad sees any hostiles.

 

If the result is null, I want nothing to happen and for the script to keep looping and checking if findNearestEnemy is not null.

 

 

If findNearestEnemy is not null, I want a script to run. In this case, let's say a simple hint with "Working!" or something in it. Some kind of indication that it's working.

 

I know how to do a simple

 

"_loop = true;

 
While {_loop} do {"
 
loop to just loop that entire section, so my real issue is with checking the result of findNearestEnemy.
 
I tried isNil and !isNil and nothing really produced anything, so perhaps I'm just misunderstanding.

Share this post


Link to post
Share on other sites

It returns objNull if no enemy found. So you can check against that. Note that it also returns empty enemy vehicles as enemies.

  • Like 1

Share this post


Link to post
Share on other sites

It returns objNull if no enemy found. So you can check against that. Note that it also returns empty enemy vehicles as enemies.

 

_var = leader _group findNearestEnemy getPos leader _group; 

 

if (!objNull "_var") then {

 

 

 

 

Is this correct?

Share this post


Link to post
Share on other sites

Give it  a try. Make sure you have a closing bracket after your if then:

if (_var != objNull) then {hint str _var} else {hint "No enemies near"};
  • Like 1

Share this post


Link to post
Share on other sites

Ah, so that's how it's done in Arma's language. Variable's result is anything but objNull. I've misunderstood ! for quite some time, then.

 

I found a more complex way to do essentially the same thing, but it also produced huge problems further on down the road. This is far nicer and simpler, I'll try it out later. This should also allow me to designate what happens when there's no more enemies in sight.

 

Thanks.

 

I'm new to complex Arma scripting, so I'm also having some problems with making a script that runs constantly during the mission, and runs some functions only once, but with repeatibility.

 

ie: Script checks if enemies in sight, if enemies in sight, it sends a hint. When enemies are not in sight, it sends a different hint. It will do this again the next time you run into enemies, but it won't constantly spam hints.

 

I'll make it's own thread if I can't figure it out in the meanwhile. I think I just have to type in quite a lot of "if, when" and whatnot.

 

EDIT: There's a problem.

 

Putting in

 

 if (_var != objNull) then {hint str _var} else {hint "No enemies near"}; 

 

causes the script to hint back either a null object when enemies are not in view, or the expected result when they are.

 

Nowhere does it hint "No enemies are near". Thus the script basically runs the "then" part constantly. 

 

If this is intended, how do I make some functions run when the script reads a valid hostile unit? Right now, the script just reads back what the units see. 

Share this post


Link to post
Share on other sites

If you want to do something when an enemy has been spotted simply replace the hint str _var part with anything you want the group to do.

 

You can also add a comparison to see if the array has changed and therefore new units are known to the group:

_enemies = [leader _group(1500)] call BIS_fnc_enemyTargets;
waituntil {!(_enemies isEqualTo ([leader _group(1500)] call BIS_fnc_enemyTargets))};
hint "New enemies spotted!";

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

hi,

 

_var = leader _group findNearestEnemy getPos leader _group; 

 

if (!objNull "_var") then {

 

 

 

 

Is this correct?

 

 

no use isNull to see if the tested item is Null.

if !(isNull _var) then {

don't forget to write (before your loop):

_var = objNull;

cya.

 

Nikiller.

  • Like 1

Share this post


Link to post
Share on other sites

Additional o what Nikiller says: using parenthesis will save you tons of "this is not working and I don't know why", for example:

 

_var = leader _group findNearestEnemy getPos leader _group; 

 

May work, but it's better this way:

 

_var = (leader _group) findNearestEnemy (getPos (leader _group)); 

  • Like 1

Share this post


Link to post
Share on other sites

Yep, testing against "null" should probably work. Haven't tested yet but I have faith. I'll report back when I eventually manage to cause an error, and I'll also look into parentheses use. ;)
 
Thanks guys.
 
EDIT:
 
Well, I can get the whole thing to run, but it's in a very clunky format.

 

I have to put a

 

"if !(isNull _var) then {

 
code;
};"
 
and copy that for every function I want. Is there a way to embed multiple functions as a consequence to the _var being not null? I tried to simply put multiple lines between the {}; but it failed to run.
 
Also, how do I access that good and clean looking input box so I can put my scripts there and they show correctly here on the forums?

 

Share this post


Link to post
Share on other sites

 

 Weird cant get this working. So Im using an Editor place trigger set to:

 

 Opfor Detectedby Blufor; Repeatedly

 

Condtion: {!(isNull (_x findNearestEnemy _x))} foreach units group player

Activation Hint "works!"

 

 Problem is that Opfor Detected by is not reliable after the first detection and I need it fired pretty quickly after every enemy soldier detected.

 

So I tried putting in the Missioninit.sqf:

 

_nearEnemy = {!(isNull (_x findNearestEnemy _x))} foreach units group player;

 
while {_nearEnemy}  do {hintc "new enemies spotted!"}

 

To mimic the trigger but it duds and nothing happens

 

Share this post


Link to post
Share on other sites

 

 So this works on enemy sighting only problem is I dont know how to stop the loop after each sighting :D

 

while { true } do {
        waitUntil {
           {!(isNull (_x findNearestEnemy _x))} foreach units group player
        };
        hint"enemies!"; nul = execvm "house.sqf"; false
    };

 

Share this post


Link to post
Share on other sites

 

 Well ive discovered that trying to exit and then re-constitute a Waituntil loop is the 5th Circle of Hell. It either only fires once or spams itself till i literally have to cntrl-alt-delete to exit. Good god

 

Trying new method of adding and subtracting known enemy unit in Array format but hjonestlt shits just getting crazier

 

So Im now starting in the Mission.init:

 

Frog_Spotted = [];
Frog_Enemy = [];
Dead_Frogs = [];
nul = execvm "loop.sqf";

 

 

Loop.sqf

 

 Frog_Enemy = player findnearestenemy player;                                                                     \\\Here Im using Player as the focal point as later Im using _x for fellow AI squaddie which is what I prefer. Obviously this isnt ideal but for the most part we'll have common nearestEnemy\\
 
 if !(alive Frog_Enemy) then {hintc "he's dead";Dead_frogs pushback Frog_Enemy};              \\\ Dead_Frogs is supposed to be the cleanup array to free up more current enemy. So far its always coming up Object Void
 if !(alive Frog_Enemy) then {Frog_spotted = Frog_Spotted - Dead_Frogs};

 if ((Frog_Enemy in Frog_spotted) && (!alive Frog_Enemy)) exitWith {};
  waitUntil {                                                                                                        
           {!(isNull (_x findNearestEnemy _x))} foreach units group player                                   \\\ This sensor works great..I just cant control its power
                };

  if (Frog_Enemy in Frog_spotted) exitWith {};
  hint"enemies!"; nul = execvm "house.sqf"; 

  Frog_Spotted pushback Frog_Enemy;                                                                                  \\\ basically here is where ive gone batshit am am grasping at straws
    
sleep 1;

nul = execvm "loop.sqf";                                                                                                         

 

                                                                           \\\\ refiring script to grab new enemy but so far it just locks in to the first one i encounter either dead or alive. If that enemy dies it will literally return the dead soldiers model # before grabbing a new live one\\

Share this post


Link to post
Share on other sites

 

 Nice post. Its something we really need to be reliable and doesnt seem like it should be that hard for them to tighten up -hopefully they do.

 

Sadly my problem lies in my poor scripting rather than the commands instability or inaccuracy. I actually dont need it for this script to be too accurate -just need it to fire on spotted target and then forget him and free itself up to spot the next one whether it be the absolute nearestTarget or not doesn't really matter in this case.

Share this post


Link to post
Share on other sites
On 1/5/2016 at 2:06 AM, Grumpy Old Man said:

If you want to do something when an enemy has been spotted simply replace the hint str _var part with anything you want the group to do.

 

You can also add a comparison to see if the array has changed and therefore new units are known to the group:


_enemies = [leader _group(1500)] call BIS_fnc_enemyTargets;
waituntil {!(_enemies isEqualTo ([leader _group(1500)] call BIS_fnc_enemyTargets))};
hint "New enemies spotted!";

Cheers

 

Hmm i could use this (my old detection script got borked somehow) yet it gives me an error of a missing Bracket 

 

_group = group  player;

_enemies = [leader _group here(1500)] call BIS_fnc_enemyTargets; 
waituntil {!(_enemies isEqualTo ([leader _group(1500)] call BIS_fnc_enemyTargets))}; 
hint "New enemies spotted!";

Share this post


Link to post
Share on other sites
11 minutes ago, froggyluv said:

 

Hmm i could use this (my old detection script got borked somehow) yet it gives me an error of a missing Bracket 

 

_group = group  player;

_enemies = [leader _group here(1500)] call BIS_fnc_enemyTargets; 
waituntil {!(_enemies isEqualTo ([leader _group(1500)] call BIS_fnc_enemyTargets))}; 
hint "New enemies spotted!";

 

That function only accepts a unit as argument and nothing else, as far as I know. Not sure what the "1500" is supposed to be (and there should be a comma before it).

_group = group player;
_enemies = (leader _group) call BIS_fnc_enemyTargets; 
waituntil {sleep 0.5; _enemies isNotEqualTo ((leader _group) call BIS_fnc_enemyTargets)}; 
hint "New enemies spotted!";

 

Share this post


Link to post
Share on other sites
7 minutes ago, haleks said:

 

That function only accepts a unit as argument and nothing else, as far as I know. Not sure what the "1500" is supposed to be (and there should be a comma before it).


_group = group  player;
_enemies = (leader _group) call BIS_fnc_enemyTargets; 
waituntil {!(_enemies isEqualTo ((leader _group) call BIS_fnc_enemyTargets))}; 
hint "New enemies spotted!";

 

 

Getting an "Error getting generic error in expression" erhm....error  😛

Seemingly right after the Brace after the 'waitUntil'

Share this post


Link to post
Share on other sites

Firing Arma3 - I knew I should've tested it before posting... ^^

  • Haha 1

Share this post


Link to post
Share on other sites

Just popped this in the debug and it worked :

0 spawn {
	_group = group player;
	_enemies = (leader _group) call BIS_fnc_enemyTargets; 
	waituntil {sleep 0.5; _enemies isNotEqualTo ((leader _group) call BIS_fnc_enemyTargets)}; 
	hint "New enemies spotted!";
};

Just make sure you run it in scheduled environment. 😉 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @haleksIll play with that in my script -so it had to do with the needing the Spawn and being in a scheduled environment ...huh 

  • Like 1

Share this post


Link to post
Share on other sites
22 hours ago, froggyluv said:

[...] so it had to do with the needing the Spawn and being in a scheduled environment ...huh 

 

Here's some good readin' : https://community.bistudio.com/wiki/Scheduler  😉 

Simply put, every time you need a delay or a loop, it's best to make sure your code runs in scheduled environment. Technically you can also use a "while" loop as a delay in an unscheduled script, if there's any practical use for the 10.000 iterations limit.

  • Like 1

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

×