Jump to content
  • 0

player_build.sqf - one works. one does not? why?


js2k6

Question

Hi everyone,

this isn't really an urgent request as I can make this script work

I'm just wondering what I've done wrong with my forEach loop

 

Basically, I use P4L, snap pro and build vectors

And I disallow building in certain areas.

 

this code here, works perfectly. 

	if ((player distance [6710.0474, 2571.0735, 0]) < 500) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build in Cherno!","PLAIN DOWN"]; };
	if ((player distance [10340.616, 2068.4207, 0]) < 500) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build in Elektro!","PLAIN DOWN"]; };
	if ((player distance [12100.296, 9311.5938, 0]) < 500) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build in Berezino!","PLAIN DOWN"]; };
	if ((player distance [4422.5376, 10643.881, 0]) < 500) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build near the NWAF Compound!","PLAIN DOWN"]; };
	if ((player distance [4529.9561, 8276.0459, 0]) < 500) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build near the Dichina Compound!","PLAIN DOWN"]; };
	if ((player distance [11951.005, 12712.544, 0]) < 1000) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build near the Aircraft Trader!","PLAIN DOWN"]; };
	if ((player distance [5025.2598, 9780.6973, 0]) < 1000) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build near the Aircraft Trader!","PLAIN DOWN"]; };

But I tried to get a little bit fancy and use a foreach loop, 

When I use my foreach loop, I get the error message telling me that I cannot build in the area, but I can still place the item. 

 

this is my foreach loop

uCantBuildHere = true;

_cantBuild = [
	[[6710.0474, 2571.0735, 0],500],
	[[10340.616, 2068.4207, 0],500],
	[[12100.296, 9311.5938, 0],500],
	[[4422.5376, 10643.881, 0],500],
	[[4529.9561, 8276.0459, 0],500],
	[[11951.005, 12712.544, 0],1000],
	[[5025.2598, 9780.6973, 0],1000]
];

if (uCantBuildHere) then {
	{
		_cbPos = _x select 0;
		_cbRad = _x select 1; 
		
		if ((player distance _cbPos) < _cbRad) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build in this area!","PLAIN DOWN"]; };
	
	} forEach _cantBuild;
};

As I said, the top code works, so this is not by any means urgent. I'm just curious to know why one piece of code stops me from building, but the other does not. 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I would suggest reversing the logic.

_canBuild = true;

{
    //Check location code.  
    //if at bad build location then _canbuild = false;
}count _cantBuild;

if (!(_canBuild)) exitWith { DZE_ActionInProgress = false; cutText ["You cannot build in this area!","PLAIN DOWN"]; };

// rest of the build code.

You could also add another element to each sub-array for the name of the location so the cannot build message can also contain the location.

 

Update: setting _canBuild = false if the player is trying to build in a restricted location can also be an exit with to jump out of the {}count loop whilst setting _canBuild = false.   This would be more efficient as the whole cantBuild array does not need to be checked if a positive is found. 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...