Jump to content
Sign in to follow this  
wyattwic

event handlers and triggers

Recommended Posts

Hello everyone! Something that has had me stumped for a day or two now...

I am able to easily add and remove an event handler for player within a trigger however I am unable to figure out how to add and remove the following event handler for vehicles from triggers.

I am attempting to add and remove the following event handler to vehicles from a trigger.

"["Fired",{deleteVehicle (_this select 6)}];"

Any ideas? I have already tried every direct and foreach approach I could think of.

Share this post


Link to post
Share on other sites

In the onAct-Field;

{
   if (_x isKindOf "Tank") then
   {
       hndl = _x addEventHandler ["Fired",{deleteVehicle (_this select 6)}];
   };
} forEach thislist;

You can either replace "Tank" with the Superclass of the vehicles or add them with OR's in the if-statement.

Share this post


Link to post
Share on other sites

Thanks! Will be posting finished script here once done!

---------- Post added at 20:14 ---------- Previous post was at 20:09 ----------

Okay, another question. Is there an easier way to select players and all vehicles?

Share this post


Link to post
Share on other sites

Unfortunately I got nowhere after a few tries.

I mainly attempted different iterations of the following statements. Without fail it gives the men on the ground the EH, but refuses to give it to the vehicles.

{if (_x isKindOf "land" or _x isKindOf "air" or _x isKindOf "man") then {safe = _x addEventHandler ["Fired",{deleteVehicle (_this select 6)}];};} forEach thislist;

{if (_x isKindOf "Tank" or _x isKindOf "APC" or _x isKindOf "Car" or _x isKindOf "Helicopter" or _x isKindOf "Plane" or _x isKindOf "Man") then {safe = _x addEventHandler ["Fired",{deleteVehicle (_this select 6)}];};} forEach thislist;

Any ideas?

Share this post


Link to post
Share on other sites

Okay, Figured it out!

{
           position[]={4389.1934,339,10748.47};
           a=230;
           b=80;
           angle=59.558201;
           rectangular=1;
           activationBy="ANY";
           repeating=1;
           timeoutMin=1;
           timeoutMid=1;
           timeoutMax=1;
           interruptable=1;
           age="UNKNOWN";
           name="safezone";
           expCond="vehicles in thislist or player in thislist";
           expActiv="{safe = _x addEventHandler [""Fired"",{deleteVehicle (_this select 6)}];} forEach thislist;";
           class Effects
           {
           };
       };

Now, the next problem!

This isn't working...

		{
		position[]={4290.3022,339,10687.246};
		a=240;
		b=30;
		angle=59.558201;
		rectangular=1;
		activationBy="ANY";
		repeating=1;
		interruptable=1;
		age="UNKNOWN";
		name="safezone_1";
		expActiv="{_x removeEventHandler [""Fired"", safe];} foreach thislist;";
		class Effects
		{
		};
	};

any ideas?

Share this post


Link to post
Share on other sites

Yeah, well, the variable safe is overwritten every cycle of your forEach-loop where you assign the eventhandlers.

You could assign the number to the object itself and use it afterwards:

{_x setVariable [""firedHandler"", _x addEventHandler [""Fired"",{deleteVehicle (_this select 6)}]];} forEach thislist;

{_x removeEventHandler [""Fired"", _x getVariable [""firedHandler"", 0]];} foreach thislist;

Share this post


Link to post
Share on other sites

Thanks for that!

I attempted the following.

		class Item26
	{
		position[]={4389.1934,339,10748.47};
		a=230;
		b=80;
		angle=59.558201;
		rectangular=1;
		activationBy="ANY";
		repeating=1;
		timeoutMin=1;
		timeoutMid=2;
		timeoutMax=3;
		interruptable=1;
		age="UNKNOWN";
		name="safezone";
		expCond="vehicles in thislist or player in thislist";
		expActiv="{_x setVariable [""firedHandler"", _x addEventHandler [""Fired"",{deleteVehicle (_this select 6)}]];} forEach thislist;  ";
		expDesactiv="{_x removeEventHandler [""Fired"", _x getVariable [""firedHandler"", 0]];} foreach thislist;";
		class Effects
		{
		};
	};

For some reason it is adding the EH but still is not removing it. I don't understand why.

I am currently not testing on a dedicated box, if that would make a difference.

---------- Post added at 19:47 ---------- Previous post was at 19:30 ----------

After some additional hunting, I found that there is a generic error in the expression just before the getvar. Possibly _x is not being given to getvar?

Going to play around and see what happens.

---------- Post added at 19:57 ---------- Previous post was at 19:47 ----------

Im so damn fustrated!!!

 {_x removeAllEventHandlers ""Fired"";} foreach thislist;

That wont even remove it! Anyone have any ideas?

---------- Post added at 20:24 ---------- Previous post was at 19:57 ----------

Getvariable is just not liking anything I am throwing at it. Keeps saying generic error.

Edited by Wyattwic

Share this post


Link to post
Share on other sites

Hmm maybe there are multiplayer locality issues here, so you could try to broadcast the values you assign them.

I also forgot to check if the vehicles in the trigger needs their eventhandler to be removed, after all there could be vehicles which didnt get any eventhandlers assigned to them.

{_x setVariable [""firedHandler"", (_x addEventHandler [""Fired"",{deleteVehicle (_this select 6)}]), true];} forEach thislist;

{
evtHndl = _x getVariable [""firedHandler"", -1];
if (evtHndl > -1) then
{
	_x removeEventHandler [""Fired"", evtHndl];
};
} foreach thislist;

Share this post


Link to post
Share on other sites

I also think there is another issue.

You assign event handlers to the units in the trigger area.

When A unit leaves you try to remove them from units in thislist, I think it needs to be list triggesname btw.

However it still wont work because if there are units still in the trigger area they cause the trigger to reactivate replacing the evenhandlers that were removed.

units out of the trigger are not in thislist anymore so any unit leaning still has the EVH.

Hope that makes some sort old sense.

Share this post


Link to post
Share on other sites

I considered that possibility as well.

During testing, I attempted the following as well.

Trigger safezone covers the spawn areas. It only adds the event handler.

Trigger safezone_1, 2, 3 and 4 cover the exits. It only removes the event handler. Doesn't work still.

If anyone wants to chat about it, I am on the AWG TS3 under the name of Wyatt. ts3.anzuswargames.info

Share this post


Link to post
Share on other sites

Okay. I am attempting another approach that seems more promising. This time my fight seems to be with getvariable.

I have two scripts. The trigger execvm's them when appropriate.

When inside the trigger, safety = [this] execVM "awg\basesafety_in.sqf";

When leaving the trigger, safety = [this] execVM "awg\basesafety_out.sqf";

I keep getting an error with getvar on both of them. My code for both is below.

Basesafety_in.sqf

notsafe = _this getVariable ["notsafe"];

if (notsafe = true or isnull) then
{
safevalue = this addEventHandler ["Fired",deleteVehicle (_this select 6)];
_this sideChat "Ammunition safety engaged";
notsafe = false;
_this setVariable ["safevalue", safevalue];
_this setVariable ["notsafe", notsafe];
};

Basesafety_out.sqf

notsafe = _this getVariable ["notsafe"];
safevalue = _this getVariable ["safevalue"];
if (notsafe = false) then
{
_this removeEventHandler ["Fired",safevalue];
_this sideChat "Ammunition safety disengaged";
notsafe = true;
_this setVariable ["safevalue", safevalue];
_this setVariable ["notsafe", notsafe];
};

Any ideas what I am doing wrong with getvariable? It says that I am giving it type any, and that it needs an object, namespace, etc.

Any other tips would be nice too. :bounce3:

Share this post


Link to post
Share on other sites

You have a more pressing issue

When inside the trigger, safety = [this] execVM "awg\basesafety_in.sqf";

When leaving the trigger, safety = [this] execVM "awg\basesafety_out.sqf";

this has no meaning in a trigger

this works in SP, no idea about MP

anyone present repeating / trigger name safetrig

cond

round (time %1)==1

on act

{safety = [_x,safetrig] execVM "awg\basesafety_in.sqf";} foreach thislist

save as "basesafety_in.sqf"

private ["_safevalue","_unit"];
_unit = _this select 0;
_trig = _this select 1;


if (_unit getVariable ["notsafe",true]) then
   {
   _safevalue = _unit addEventHandler ["Fired",{deleteVehicle (_this select 6)}];
   _unit sideChat "Ammunition safety engaged";
     _unit setVariable ["notsafe",false,true];

 waituntil {_unit distance _trig >  (triggerarea _trig) select 0};

     _unit removeEventHandler ["fired",_safevalue];
    _unit setVariable ["notsafe",true,true];
    _unit sideChat "Ammunition safety Disengaged";
   };

Edited by F2k Sel

Share this post


Link to post
Share on other sites

Thanks F2k,

I see what I was doing wrong with the _this and _unit. From what I have seen _this does work.

I implemented that script exactly as provided; Result is a large amount of lag and no real action from the script.

Lets see what I can do.

Edited by Wyattwic

Share this post


Link to post
Share on other sites

F2k, no matter how I looked at your code, I could not get it do as needed.

Since then, I have spent many hours working on a solution. I managed to make things work flawlessly for players, but not for vehicles. Here is what I have, let me know if you have any ideas.

Trigger

- Condition

player in thislist;

-On activation

_nul = player execVM "awg\spawnzone.sqf";

-On deactivation

player removeAllEventHandlers "Fired";

-awg\spawnzone.sqf

spawnsafe = player addeventhandler ["fired",{
deleteVehicle (_this select 6);
player sideChat "Firing of live ammunition is not permitted at base.";
}];

Again, everything is very helpful.

Share this post


Link to post
Share on other sites

I can only test in SP and it worked ok, I realised later that because it was using a trigger it would be running multiple times a lot more than just the number of players.

You could use vehicle player in every instance but then you will hit another problem.

If you get out of the vehicle you will be able to shoot as the trigger hasn't been reactivated.

Although you can add an EVH while in a vehicle it seems to be lost when exiting so you need to add two.

cond

vehicle player in thislist 

on act

{_x addeventhandler ["fired",{  deleteVehicle (_this select 6);  vehicle player sideChat "Firing of live ammunition is not permitted at base.";  }] } foreach [player,vehicle player]

{_x removeAllEventHandlers "Fired";} foreach [player,vehicle player]

Only tested in SP, also why not use the protection object I though that was the idea. You spawn it and no one can be shot inside it.

http://forums.bistudio.com/showthread.php?146507-How-to-get-mission-protection-system-just-like-in-THE-LONGEST-DAY-mission-BY-XENO&highlight=protection

Update I just thought of another fail condition

If there is an empty vehicle and you get in it as gunner you can still open fire.

Edited by F2k Sel

Share this post


Link to post
Share on other sites

I already attempted vehicle player without success just after I made that post. You would think it would work, but apparently not.

The protection object you had posted a few messages ago did not work. It caused the game to lag badly without any result.

I am going to keep experimenting in the meantime. Ill post here if anything new happens.

Share this post


Link to post
Share on other sites

This is all really starting to frustrate me to an unbelievable level.

I currently am able to restrict players shooting at base quite well. I need to also restrict all vehicles from firing while at base.

Right now, I have a marker called "safemarker". I want all vehicles within 250 meters of the marker to be effected a event handler that would prevent them from firing.

I will give $5 to anyone who can get me a solution to this issue!

Share this post


Link to post
Share on other sites

OK last attempt.

for each player give him this in his init.

player addeventhandler ["fired",{  if (vehicle player in list base ) then {deleteVehicle (_this select 6)}; }];

for each vehicle add this

this addeventhandler ["fired",{  if (vehicle (_this select 0) in list base ) then {deleteVehicle (_this select 6)}; }]

base being the trigger name

anybody present repeating.

Share this post


Link to post
Share on other sites

Hm, okay since you're really frustrated and searching for something to work....

I'm also really frustrated when I try to implement something using triggers and keep track of whoever is getting in and out of the trigger....

In these cases I rely on goo' ol' scripting :D I don't know if this will work in MP (thus I need you to try this out) but just give it a shot :D

My idea is rather simple: Instead of assigning and deleting the eventhandler everytime someone steps into a trigger, just assign the fired-handler to

every player and vehicle in your server and check in the handler if we need to delete the projectiles. This way we don't need to figure out how to

safely remove the handler because there is no need to.

I put a marker over my base and set it to 250/250 in size, naming it "safemarker".

Now putting this following code into the init.sqf did the trick:

safeZoneArea 	= getMarkerPos "safemarker";
safeZoneRadius 	= (getMarkerSize "safemarker") select 0;

firedInSafeZone = {
_unit = _this select 0;
if (local _unit) then
{
	if (_unit distance safeZoneArea <= safeZoneRadius) then
	{
		deleteVehicle (_this select 6);
	};
};
};

if (isServer) then
{
[] spawn {
	_firedHndl = 0;
	while {true} do
	{
		{
			{
				if (alive _x) then
				{
					_firedHndl = _x getVariable ["safezoneHandler", -1];
					if (_firedHndl == -1) then
					{
						_firedHndl = _x addEventHandler ["fired", firedInSafeZone];
						_x setVariable ["safezoneHandler", _firedHndl];
					};
				};
			} forEach _x;
		} forEach [vehicles, allUnits];

		sleep 1;
	};
};
};

This code checks every player (Even AI ones, if you do not plan to use AI's, just replace "allUnits" with "playableUnits") and vehicles in your server for

the lack of the eventhandler and adds it individually. The loop runs every second, if you want to relieve some server load, you could consider letting

the loop sleep some more time.

If you do not plan to create vehicles dynamically in your server, you could even save some more computation time in omitting "vehicles" and assigning

the eventhandler to the player everytime he joins the server, which would effectively get rid of the continuous loop.

/edit:

On second thought, the code in the post above mine looks way more simple and effective :D

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites

Sorry for the slow reply. F2k, your snipplet is awesome, here are some new notes and problems.

The player snipplet did not work, however the vehicle snipplet seems to be universal for both players and most vehicles.

The vehicle snipplet seems to not work on attack helicopters only.

It seems to have broken my vehicle respawn script.

Any idea why it wont work on attack helicopters?

Share this post


Link to post
Share on other sites

I just pasted back previous two lines of code for vehicle and player and it it works in all cases in SP mode in A3, I'll try A2/OA tomorrow.

The only exception is if a player starts in a vehicle, when he gets out he has no EVH as I forgot about that.

This will add the EVH to crew already in a vehicle at mission start so they won't fire when getting out in the safe zone.

{_x addeventhandler ["fired",{ if (vehicle (_this select 0) in list base ) then {deleteVehicle (_this select 6)}; }] } foreach crew this+[this]

Any other issues must be multiplayer related.

Edited by F2k Sel

Share this post


Link to post
Share on other sites

F2k, the vehicle line previously posted works for all vehicles and players in SP and MP. The issue with the helicopters seems to be an external init replacement that I cant find... The player line does not work for players in MP (may work in SP, IDC about SP though).

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  

×