Jump to content
draoth

How can i make scripts that uses expressions that may or may not be present.

Recommended Posts

Hello everyone,

 

I've got multiple playable units and have some scripts that include those units. But i want the script to ignore that part when no one is playing with them.

I've tried this: 

if (unit1 != nil) then { 

//Very cool script that includes unit1

};

Any efficient way to do this?

Share this post


Link to post
Share on other sites

if (!isNil "unit1") then {};

 

and if you want to check if the player is unit1:

if (str player == "unit1") then {};

  • Like 1

Share this post


Link to post
Share on other sites
16 minutes ago, Belbo said:

if (!isNil "unit1") then {};

 

and if you want to check if the player is unit1:

if (str player == "unit1") then {};

Thanks, and what if i want to use a "unit1" and "unit2" in for example, a waituntil?

waituntil {unit1 distance _veh <10 || unit2 distance _veh <10};

 

Share this post


Link to post
Share on other sites
//creating the base variable
private _themIn = false;

//we create a little function to call for the waitUntil:
private _checkForUnits = {
	//creating an array of all objects from the type "MAN" closer than 10 meters to _veh
	private _nearestObjects  = nearestObjects [_veh, ["MAN"], 10];

	//counting all objects in that array with the name "unit1" or "unit2" - IN is case sensitive, so you have toUpper the str
	_themIn = { toUpper (str _x) in ["UNIT1","UNIT2"] } count _nearestObjects > 0;
	
	_themIn
};

//with the sleep you ease the strain on the mission performance.
waitUntil { sleep 1; call _checkForUnits };

That should do it.

  • Like 2

Share this post


Link to post
Share on other sites
34 minutes ago, Grumpy Old Man said:

If you give out a bit more detail I'm sure there's a simple approach to what you want to do.

 

Cheers

I hope so, i'm all in for a simple approach ;)

This script i'm trying to make checks if one of the two guys is near a car and makes the car stop and take take them for a ride.  I just want to make the script work if a player joins man2 instead of man1. It should also work on man1 if man2 is also playing and vice versa. 

This here is the whole "hitchhiking" script for anyone who needs more context:
 

Spoiler

_veh = _this select 0;
_AI = _this select 1;
_AI2 = _this select 2;
_AI3 = _this select 3;
_wp = _this select 4;
_pass = _this select 5;

_chance = selectrandom [1,2,3];
waituntil {man1 distance _veh <10 ||};
if (_chance == 3 && vehicle man1 == man1) then {
_AI disableAI "all";

if (!isNil "man1") then {
if (_Pass > 1) then {_AI disableAI "all"; _AI2 disableAI "all";
};};

if (!isNil "man") then {
if (_Pass > 2) then {_AI disableAI "all"; _AI2 disableAI "all"; _AI3 disableAI "all"; 
};};

_veh engineOn false;
if (!isNil "man1") then {
if (man2 == player) then {
hint "Get in you pieces of shit, this will be a hell of a ride";
};
if (!isNil "man2") then {
if (man2 == player) then {
hint "Get in you pieces of shit, this will be a hell of a ride";
};};

sleep 2;
if (!isNil "man1") then {
man1 moveInAny _veh;
};
if (!isNil "man2") then {
man2 moveInAny _veh;
};
sleep 2;
_AI enableAI "all";
_AI disableAI "AUTOCOMBAT";
if (_Pass > 1) then {_AI enableAI "all"; _AI2 enableAI "all"; _AI2 disableAI "AUTOCOMBAT";
};
if (_Pass > 2) then {_AI enableAI "all"; _AI2 enableAI "all"; _AI3 enableAI "all";  _AI3 disableAI "AUTOCOMBAT";
};
sleep 120;
_AI disableAI "all";
if (_Pass > 1) then {_AI disableAI "all"; _AI2 disableAI "all";
};
if (_Pass > 2) then {_AI disableAI "all"; _AI2 disableAI "all"; _AI3 disableAI "all"; 
};
_veh engineOn false;
if (!isNil "man1") then {
if (man1 == player) then {
hint "I'm not helping you cunts any further. I hope you'll die.";
};};
if (!isNil "man2") then {
if (man2 == player) then {
hint "I'm not helping you cunts any further. I hope you'll die.";
};};

sleep 2;
if (!isNil "man1") then {
doGetOut man2;
if (!isNil "man2") then {
doGetOut man2;
};

_veh engineOn true;
_AI enableAI "all";
_AI disableAI "AUTOCOMBAT";
if (_Pass > 1) then {_AI enableAI "all"; _AI2 enableAI "all"; _AI2 disableAI "AUTOCOMBAT";
};
if (_Pass > 2) then {_AI enableAI "all"; _AI2 enableAI "all"; _AI3 enableAI "all";  _AI3 disableAI "AUTOCOMBAT";
};
};
sleep 2;

 

 

Share this post


Link to post
Share on other sites
49 minutes ago, Belbo said:

//creating the base variable
private _themIn = false;

//we create a little function to call for the waitUntil:
private _checkForUnits = {
	//creating an array of all objects from the type "MAN" closer than 10 meters to _veh
	private _nearestObjects  = nearestObjects [_veh, ["MAN"], 10];

	//counting all objects in that array with the name "unit1" or "unit2" - IN is case sensitive, so you have toUpper the str
	_themIn = { toUpper (str _x) in ["UNIT1","UNIT2"] } count _nearestObjects > 0;
	
	_themIn
};

//with the sleep you ease the strain on the mission performance.
waitUntil { sleep 1; call _checkForUnits };

That should do it.

Thanks man, will try it out if @Grumpy Old Man doesn't have a simpler solution :).

Share this post


Link to post
Share on other sites

You dn't need to stringify "unit1" and "unit2" ??

waituntil { (!isNil "unit1" && {unit1 distance _veh <10})  or ( !isNil "unit2" && {unit2 distance _veh <10}) };

  • Like 1

Share this post


Link to post
Share on other sites
On 2/4/2018 at 8:25 PM, pierremgi said:

You dn't need to stringify "unit1" and "unit2" ??

waituntil { (!isNil "unit1" && {unit1 distance _veh <10})  or ( !isNil "unit2" && {unit2 distance _veh <10}) };

Thanks, but it doesn't seem to work. When i use this there are no error messages and it just doesn't do anything sadly.

Share this post


Link to post
Share on other sites

That works fine for me. Check your players (or your spawned units) are named unit1, unit2. check also _veh is defined and 10 m is compatible with the dimensions of the vehicle (distance from center of unit to center of vehicle must be < 10).

The code I wrote is tested, running in init.sqf or everywhere in common scope for every clients like standard trigger. So, your problem is somewhere else. Perhaps some locality issue?

  • Like 1

Share this post


Link to post
Share on other sites
On 2/6/2018 at 12:55 AM, pierremgi said:

That works fine for me. Check your players (or your spawned units) are named unit1, unit2. check also _veh is defined and 10 m is compatible with the dimensions of the vehicle (distance from center of unit to center of vehicle must be < 10).

The code I wrote is tested, running in init.sqf or everywhere in common scope for every clients like standard trigger. So, your problem is somewhere else. Perhaps some locality issue?

Thanks it indeed was a locality issue. Thanks for helping!

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

×