Jump to content
JR Nova

[SOLVED] addAction for players near hidden object

Recommended Posts

I'm trying to make an action to purchase and unhide a hidden tower (named Tower) when a player is near where it will go and has enough money. 

 

I had it working fine for 1 person using a trigger around the tower, but when I tested it on MP it added an action for each player that was connected, for example 2 players would give 2 actions and give the actions to players who weren't in the trigger but had enough money, also these duplicate actions would not delete upon trigger deact or if another player used the action.

 

the code from the trigger (named towerTrig) that sort of worked 

 

Activation: Any Player

Type: Present

Condition

player in thisList

On Act

towerAction = player addAction [format ["Purchase Tower <t color='#00ff00'>$%1</t>", towerPrice], "scripts\towers.sqf", [Tower, towerTrig, towerPrice], 5, true, true, "", "player getVariable ['lmo_cash', 0] >= towerPrice"];

On Deact

player removeAction towerAction;

 

scripts\towers.sqf

Spoiler

_player = _this select 1;
_action = _this select 2;
_tower = _this select 3 select 0;
_trigger = _this select 3 select 1;
_price = _this select 3 select 2;
_cash = player getVariable "lmo_cash";

deleteVehicle _trigger;
player removeAction _action;

_tower hideObjectGlobal false;
_tower enableSimulationGlobal true;

player setVariable ["lmo_cash",(player getVariable "lmo_cash") - _price, false];
_cash = player getVariable "lmo_cash";
(uiNameSpace getVariable "hudCash") ctrlSetText format ["$%1", str _cash];

"build" remoteExec ["playSound"];

if (_tower == tower) exitWith {
  twr=true;
  ["Reward",["Tower Constructed", "Main Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];
};

["Reward",["Tower Constructed", "Perimeter Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];

 

 

 

 

 

I also tried running an addAction from the init.sqf, but the action would only take away the money and not show the tower unless it was selected by the host. 

If I ran it with if (isServer) then {} the action would only show for the host and not any of the other players.

 

init.sqf

 

if (isServer) then { //Tried running with this line and without
	[player, [format ["Purchase Tower <t color='#00ff00'>$%1</t>", TowerPrice], "scripts\towers.sqf", [Tower, towerTrig, TowerPrice], 5, true, true, "", "_this getVariable ['lmo_cash', 0] >= TowerPrice && _this distance Tower < 10"]] remoteExec ["addAction"];
}; //Tried running with this line and without

 

Is there a way to make this happen?

Share this post


Link to post
Share on other sites
10 hours ago, killzone_kid said:

Add trigger by script, use local flag, link with player with triggerAttachVehicle

 

Ok I actually have 5 towers total that I need this to work for (nTower, wTower, eTower, sTower, tower) so I just made a function that adds the trigger and action. Now the action doesn't appear at all though. Am I doing something wrong?

 

init.sqf

lmo_towers = {
  params ["_unit", "_price", "_tower","_action","_trig"];
  _unit = _this select 0;
  _price = _this select 1;
  _tower = _this select 2;

  _trig = createTrigger ["EmptyDetector", getPos _tower, false];
  _trig setTriggerArea [5, 5, 0, false];

  _action = _unit addAction [format ["Purchase Tower <t color='#00ff00'>$%1</t>", _price], "scripts\towers.sqf", [_tower, _trig, _price], 5, true, true, "", "_x getVariable ['lmo_cash', 0] >= _price"];

  _trig triggerAttachVehicle [_unit];
  _trig setTriggerActivation ["VEHICLE", "PRESENT", true];
  _trig setTriggerStatements ["this", "_action", "_unit removeAction _action;"];
};

player setVariable ["lmo_cash",2000, false];
pTowerPrice = 250;
towerPrice = 1000;

[player, towerPrice, tower] call lmo_towers;
[player, pTowerPrice, nTower] call lmo_towers;
[player, pTowerPrice, sTower] call lmo_towers;
[player, pTowerPrice, eTower] call lmo_towers;
[player, pTowerPrice, wTower] call lmo_towers;

 

scripts\towers.sqf

Spoiler

_player = _this select 1;
_action = _this select 2;
_tower = _this select 3 select 0;
_trigger = _this select 3 select 1;
_price = _this select 3 select 2;
_cash = player getVariable "lmo_cash";

deleteVehicle _trigger;
player removeAction _action;

_tower hideObjectGlobal false;
_tower enableSimulationGlobal true;

player setVariable ["lmo_cash",(player getVariable "lmo_cash") - _price, false];
_cash = player getVariable "lmo_cash";
(uiNameSpace getVariable "hudCash") ctrlSetText format ["$%1", str _cash];

"build" remoteExec ["playSound"];

if (_tower == tower) exitWith {
  twr=true;
  ["Reward",["Tower Constructed", "Main Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];
};

["Reward",["Tower Constructed", "Perimeter Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];

 

 

Edited by JR Nova
added all info for scripts

Share this post


Link to post
Share on other sites
add_tower_action = 
{
	_this getVariable "params" params ["_tower", "_price"];
	
	if (player getVariable ['lmo_cash', 0] < _price) exitWith {};
	
	_this setVariable ["tower_action", player addAction [format ["Purchase Tower <t color='#00ff00'>$%1</t>", _price], "scripts\towers.sqf", [_tower, _this, _price], 5, true, true, "", ""]];
};

remove_tower_action = 
{
	player removeAction (_this getVariable ["tower_action", -1]);
	_this setVariable ["tower_action", -1];
};

lmo_towers = 
{
	if (!hasInterface) exitWith {};
	
	params ["_tower", "_price"];

	_trig = createTrigger ["EmptyDetector", getPos _tower, false];
	_trig setVariable ["params", _this];
	_trig setVariable ["tower_action", -1];

	_trig setTriggerArea [5, 5, 0, false]; 
	_trig triggerAttachVehicle [player];
	_trig setTriggerActivation ["VEHICLE", "PRESENT", true];
	_trig setTriggerStatements ["this", "thisTrigger call add_tower_action", "thisTrigger call remove_tower_action"];
};

player setVariable ["lmo_cash", 2000];
towerPrice = 1000;

[tower, towerPrice] call lmo_towers;

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks that did the trick!

 

Had to change a few things because the action was only getting removed from the player who activated it, but everything is working now 😁

 

init.sqf

Spoiler

add_tower_action =
{
	_this getVariable "params" params ["_tower", "_price"];

  _cond = false;
  if (_tower == nTower) then { _cond = nBuilt; };
  if (_tower == sTower) then { _cond = sBuilt; };
  if (_tower == eTower) then { _cond = eBuilt; };
  if (_tower == wTower) then { _cond = wBuilt; };
  if (_tower == tower) then { _cond = tBuilt; };

  if (_cond isEqualTo true) exitWith {};

  if ((player getVariable ['lmo_cash', 0] < _price) && (_cond isEqualTo false)) exitWith { // Not enough money, Price is Red
  _this setVariable ["tower_action", player addAction [format ["Purchase Tower <t color='#ff0000'>$%1</t>", _price], {}, [], 5, true, false, "", ""]];
  };
  _this setVariable ["tower_action", player addAction [format ["Purchase Tower <t color='#00ff00'>$%1</t>", _price], "scripts\towers.sqf", [_tower, _this, _price], 5, true, true, "", ""]];
};

remove_tower_action =
{
	player removeAction (_this getVariable ["tower_action", -1]);
	_this setVariable ["tower_action", -1];
};

lmo_towers =
{
	if (!hasInterface) exitWith {};

	params ["_tower", "_price"];

	_trig = createTrigger ["EmptyDetector", getPos _tower, false];
	_trig setVariable ["params", _this];
	_trig setVariable ["tower_action", -1];

	_trig setTriggerArea [5, 5, 0, false];
	_trig triggerAttachVehicle [player];
	_trig setTriggerActivation ["VEHICLE", "PRESENT", true];
	_trig setTriggerStatements ["this", "thisTrigger call add_tower_action", "thisTrigger call remove_tower_action"];
};
                                                    
nBuilt = false;
sBuilt = false;
eBuilt = false;
wBuilt = false;
tBuilt = false;
                                                    
player setVariable ["lmo_cash",2000, false];
pTowerPrice = 250;
towerPrice = 1000;

[nTower, pTowerPrice] call lmo_towers;
[sTower, pTowerPrice] call lmo_towers;
[eTower, pTowerPrice] call lmo_towers;
[wTower, pTowerPrice] call lmo_towers;
[tower, towerPrice] call lmo_towers;

 

 

scripts\towers.sqf

Spoiler

_player = _this select 1;
_action = _this select 2;
_tower = _this select 3 select 0;
_trigger = _this select 3 select 1;
_price = _this select 3 select 2;
_cash = player getVariable "lmo_cash";

deleteVehicle _trigger;
player removeAction _action;

[_tower, false] remoteExec ["hideObjectGlobal", 0];
[_tower, true] remoteExec ["enableSimulationGlobal", 0];

player setVariable ["lmo_cash",(player getVariable "lmo_cash") - _price, false];
_cash = player getVariable "lmo_cash";
(uiNameSpace getVariable "hudCash") ctrlSetText format ["$%1", str _cash];

"build" remoteExec ["playSound"];

if (_tower == tower) exitWith {
  tBuilt = true;
  publicVariable "tBuilt";
  ["Reward",["Tower Constructed", "Main Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];
  [[box2, ["Arsenal",{["Open",true] call BIS_fnc_arsenal}]], "addAction", true, true] call BIS_fnc_MP;
};

if (_tower == nTower) then { nBuilt = true; publicVariable "nBuilt";};
if (_tower == sTower) then { sBuilt = true; publicVariable "sBuilt";};
if (_tower == eTower) then { eBuilt = true; publicVariable "eBuilt";};
if (_tower == wTower) then { wBuilt = true; publicVariable "wBuilt";};
["Reward",["Tower Constructed", "Perimeter Tower Purchased"]] remoteExec ["BIS_fnc_showNotification"];

 

 

  • Like 2

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

×