Jump to content
Sign in to follow this  
burdy

Working on AI Cache script - what appears wrong here?

Recommended Posts

Working on a little Cache script (that may or may not work with HETMAN) and got this :

if(!isServer) exitWith{};

//HETMAN AI CACHE SCRIPT by Burdy

AICACHESTART = True;
publicvariable "AICACHESTART";
_grp = _this select 0;
_dist = _this select 1; //Define your own Distance here
_allunitsplayer = {isPlayer _x} count playableUnits && switchableUnits;

while (AICACHESTART) do {
sleep 10;
if ((_allunitsplayer distance leader _grp) > _dist) then {{
		hint "cache";
			this enablesimulation false;
			this hideobject true;
	} foreach !leader _grp}
	else
	{{ 
		hint "uncache";
			sleep 10;
				this enablesimulation true;
				this hideobject false;
			!leader _grp setpos (getpos leader _grp);
	} foreach !leader _grp}

}		

with this in the INIT (NN1 being the group, 25 being the distance):

[nn1,25] execVM "AICACHE.sqf";

But nothing happens.. What went wrong?

Share this post


Link to post
Share on other sites

1) _allunitsplayer = {isPlayer _x} count playableUnits && switchableUnits;

The line fails because "&&" seeks boolean returns to compare, not number or array. "Count playableUnits" returns a number and "switchableUnits" returns an array. The compared values have to be boolean.

2) while (AICACHESTART) do {

While-do uses code brackets in place of the condition brackets.

--edit -- But apparently AICACHESTART has to be in condition brackets within the code brackets:

https://community.bistudio.com/wiki/Talk:while

3) } foreach !leader _grp

I think this fails because leader returns an object not a boolean. "!" (not) checks for a boolean return and reverses it.

Edited by OpusFmSPol

Share this post


Link to post
Share on other sites
1) _allunitsplayer = {isPlayer _x} count playableUnits && switchableUnits;

The line fails because "&&" seeks boolean returns to compare, not number or array. "Count playableUnits" returns a number and "switchableUnits" returns an array. The compared values have to be boolean.

2) while (AICACHESTART) do {

While-do uses code brackets in place of the condition brackets.

--edit -- But apparently AICACHESTART has to be in condition brackets within the code brackets:

https://community.bistudio.com/wiki/Talk:while

3) } foreach !leader _grp

I think this fails because leader returns an object not a boolean. "!" (not) checks for a boolean return and reverses it.

Okay. For # 1, adding a count before switchableUnits work? and for 3, would "foreach units && !leader _grp" or "foreach (if(_grp == leader group _grp) then {null} else {[units group _grp]})}" work? Im trying to get the script to run the simulation + hideobject on the squadmembers only.

Here's what I got so far but to no avail. I tried taking out the count playableunits for testing purposes and made !leader to just units, but still nothing.

if(!isServer) exitWith{};

//HETMAN AI CACHE SCRIPT by Burdy

AICACHESTART = True;
publicvariable "AICACHESTART";
_grp = _this select 0;
_dist = _this select 1; //Define your own Distance here
[b]_allunitsplayer = {isPlayer _x} count switchableUnits;[/b]

[b]while { (AICACHESTART) } do [/b]{
sleep 10;
if ((_allunitsplayer distance leader _grp) > _dist) then {{
		hint "cache";
			this enablesimulation false;
			this hideobject true;
	} [b]foreach units _grp[/b]}
	else
	{{ 
		hint "uncache";
			sleep 10;
				this enablesimulation true;
				this hideobject false;
			!leader _grp setpos (getpos leader _grp);
	}[b] foreach units _grp[/b]}

}

Edited by Burdy

Share this post


Link to post
Share on other sites
For # 1, adding a count before switchableUnits work?

No, that won't work because "&&" is a boolean (true/false) test and counting switchable units returns a number.

"&&" is for running code only if the test before and after returns true on both; example:

if (true && true) then {true:code is run}

if (true && false) then {false: code not run}

if (false && true) then {false: code not run}

It only tests for true/false. It does not compile arrays or add numbers. You should drop the "&&" if you're not doing a boolean true/false test. Use plus "+" instead.

Whether you want to obtain a number (count) or compile an array (player and switchable units in a group), you should declare them separately, and then add them together.

example:

_playerUnits = <this>;					// <this> returns 1
_switchableUnits = <that>;				// <that> returns 3
_allUnitsPlayer = _playerUnits + _switchableUnits;	// 4 = 1 + 3

But I believe your seeking an array. As to arrays, they should always be declared beforehand. They can be added to using "+" as well, but you want to avoid nesting or multi-dimensioning them if it's not what is desired:

https://community.bistudio.com/wiki/Array

_allUnitsPlayer = [];  					// declared: _allUnitsPlayer is an array
_allUnitsPlayer = _allUnitsPlayer + _playerUnits; 	//_playerUnits is added into the array
_allUnitsPlayer = _allUnitsPlayer + _switchableUnits;	//_switchableUnits is added into the array behind _playerUnits

However....

A switchable unit might be a player. You'll need a check in place to ensure that a player unit already in the array doesn't get added again as a switchable unit.

"PlayableUnits" will return an array of every playable unit on the map, it won't be limited to the player's group.

I don't know whether "switchableUnits" returns different results for a server versus client; the wiki doesn't indicate locality effect. But I suspect it would return only switchable units in the server's player group, since a dedicated server will return an empty array.

https://community.bistudio.com/wiki/switchableUnits

I know that switchable units is a subset of playable units. Switchable units are the playable units in a SP player's group. If it's playable, it may not be switchable depending on the group, but if it's switchable, it will be playable.

I would say you should therefore check whether a unit in a group is contained in the "playableunits" array. If found, it will be a switchable unit, so then check if it's already in the _allUnitsPlayer array (already containing the player units). If not there, then add it to the _allUnitsPlayer array. That should add the SP switchable (AI playable in MP) units to the array of players.

My god, brain thingy hurts trying to think about doing that.... good luck.

for 3, would "foreach units && !leader _grp" or "foreach (if(_grp == leader group _grp) then {null} else {[units group _grp]})}" work?

"forEach" runs code on every item in an array. Whatever follows the "forEach" command has to be an array. I think you need to run either "forEach _allUnitsPlayer" or "forEach Units _grp", and run the true/false leader check inside the code being run.

--edit--- Or you might run it on _switchableUnits if those are the only ones you want to cache. That array could be built separately, added to the _allUnitsPlayer array, or both.

Edited by OpusFmSPol

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  

×