Jump to content
Sign in to follow this  
Koni

Detecting when player is close to any ambient Wildlife ?

Recommended Posts

As part of one of my tasks, the player has to find some hens, run close to them and they delete as if you've shoved them in your pocket or bag or whatever, and then when needed they respawn back at your base.

For this I have been placing named hens with triggers that detect when player is less than 1m away from them with

player distance hen4 <= 1

is there a way to have this happen without naming the hens, so it happens when the player gets near any un-named hen ?

Share this post


Link to post
Share on other sites

Make an .sqf and execute it whenever, and have this in it:

while {true} do {
{
	if (_x isKindOf "Hen") then {
		_hen = _x;
		if ({(_hen distance _x) < 3 && isPlayer _x}) then {
			deleteVehicle _hen;
			hint "Hen captured!";
		};
	};
} foreach allUnits;
sleep 1;
};

Nevermind, not working. It seems animals don't come up in allUnits :(

Edited by Grimes [3rd ID]

Share this post


Link to post
Share on other sites

while {isLookingForHens} do {
   _cTarget = cursorTarget;
   if (_cTarget isKindOf "Hen" && player distance _cTarget < 2) then {
       deleteVehicle _cTarget; //Or whatever you want to do.
   };
   sleep 1.0;
};

Assuming that the classname is or derives from "Hen".

Share this post


Link to post
Share on other sites

Thanks for the script, but I can't get it working :(

Tried adding it to the init, ran from a script file and trigger... not really sure if it works, or Im doing something wrong.

The script error that comes up is

undefined variable in expression "islookingforhens"

Share this post


Link to post
Share on other sites

I think muzzle intended for that variable to be true when you want the player to be actually chasing hens. You would have to make it true yourself in a trigger or script. Im either case you have to give it an initial value somewhere (probably init.sqf)

Share this post


Link to post
Share on other sites

The code from Grimes:

while {true} do {
{
	if (_x isKindOf "Hen") then {
		_hen = _x;
		if ({(_hen distance _x) < 3 && isPlayer _x}) then {
			deleteVehicle _hen;
			hint "Hen captured!";
		};
	};
} foreach [color="Red"]vehicles[/color];
sleep 1;
};

Not tested, but vehicles should include animals.

Share this post


Link to post
Share on other sites

Thanks, so how do I actually set this up to get this condition to be true so it works ?

Share this post


Link to post
Share on other sites

Why looping over all units constantly instead of just going over nearObjects?

You could just set up a trigger with a condition:

(count ((getPos player) nearObjects ["Hen", 4])) > 4

Or run a script that starts with:

waitUntil {(count ((getPos player) nearObjects ["Hen", 4])) > 4};

Of course both will only work locally for the player unit, and will do nothing on a dedicated server. So if for example you just give a hint or play a sound, only the player that is next to the "Hen" will see/hear it.

Share this post


Link to post
Share on other sites

Muzzleflash has the best approach as it uses the cursortarget to find the object.

All you need to do to get it to work is set isLookingForHens = true before you run the code and when finished with it just set it isLookingForHens = false to end the script.

Share this post


Link to post
Share on other sites

Thank you everyone, got it working with Muzzleflash's little script and F2k's explaination of setting the variable to true.

Sorry, my scripting know-how isn't very good, but I feel I've learnt a good deal and will probably be able to impliment it for other occasions.

Thanks everyone :)

Share this post


Link to post
Share on other sites

heh, here we go again with Animals.

This time Goats.

I've changed it so it works on Goats now, but instead of snatching any coloured Goat and just one type spawning back at the farm, I wanted to try and make the goats that spawn back at the farm different colours.

So I have been playing around with this to spawn random goats back at the farm.

islookingforgoats = true;

vehicles = ["Goat01_EP1","Goat02_EP1", "Goat"];
_goats = vehicles select (floor random 3);

while {islookingforgoats} do {  
   _cTarget = cursorTarget;
   if (_cTarget isKindOf "Goat" && player distance _cTarget < 2) then { 
       deleteVehicle _cTarget; player say2d "goat"; _goats createUnit [getMarkerPos "hens", superhen,"shen4 = this;", 0.6, "corporal"];
};
   sleep 0.5;
};

This works for deleting a black goat or whatever coloured goat you capture, and will spawn a random coloured goat back at the farm, but for every goat after that you capture, it will be the same coloured goat that spawns everytime again, so it's only choosing a random goat class for the first one spawned, and sticking with that goat class for everyone afterwards.

Where am I going wrong please.

edit: Figured it out

islookingforgoats = true;

while {islookingforgoats} do {

_cTarget = cursorTarget;

if (_cTarget isKindOf "Goat" && player distance _cTarget < 2) then {

deleteVehicle _cTarget; player say2d "goat";

vehicles = ["Goat01_EP1","Goat02_EP1", "Goat"];

_goats = vehicles select (floor random 3);

_goats createUnit [getMarkerPos "hens", superhen,"shen4 = this;", 0.6, "corporal"];

};

sleep 0.5;

};

Just needed to move the random info in the script for it to run everytime.

Thanks

p.s

Or even better, can you get the class type of the unit you capture and use that same class as the spawned unit ?

That's going a bit to far now, lol

Edited by Koni

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  

×