Jump to content
pognivet

Problem with getting AI to reduce tickets on death in Sector Control

Recommended Posts

I'm using a code inside my init.sqf:

If (!isServer) exitwith {};
0 = [] spawn {while {true} do {  
sleep 2; 
{
_x addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];
} foreach allUnits;
}};

I overcame a huge obstacle. I finally managed to make it so that units spawned by the Spawn AI module reduce tickets upon their death. The obvious problem here is that every two seconds it adds the same event handler to the same troops. So after two seconds an AI death will result in a 1 ticket loss. After four seconds the same troop's death will be a 2 ticket loss. After six seconds a 3 ticket loss. And so on. After about 5 minutes if one guy dies that team will lose 150 tickets. I need to make it so that the event handler is only added once to the troops that spawn and never again - ensuring that on their death they will only deplete one ticket. I've hit a dead end so if anybody could help me out it would be much appreciated.

Share this post


Link to post
Share on other sites

Just add the EventHandler when you spawn the unit.  I believe the Expression field of the module should be able to do that for you.

 

You're looping ever 2 seconds and adding yet another handler to every unit in the game.

 

Adding code once is far better than looping constantly over every possible unit.  Not sure why people keep trying to do that. :)

Share this post


Link to post
Share on other sites

Just add the EventHandler when you spawn the unit.  I believe the Expression field of the module should be able to do that for you.

 

You're looping ever 2 seconds and adding yet another handler to every unit in the game.

 

Adding code once is far better than looping constantly over every possible unit.  Not sure why people keep trying to do that. :)

Thanks for the reply. What should I set _x to when I add it to the Expression field? Do I just leave it _x?

Share this post


Link to post
Share on other sites

Hover over the word "Expression" in editor and it'll tell you what values are passed to the code.  It'll probably be something like Parameters passed are [<unit spawned>, <favorite food>].  So you'd do:

(_this select 0) addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];

So for my example available to the code in the Expression would be:

 

_this = [<unit spawned>, <favorite food>];

_this select 0 = <unit spawned>;

_this select 1 = <favorite food>;

 

(I'm away from the editor at the moment and can't check directly to see what values they are. :))

Share this post


Link to post
Share on other sites

Hover over the word "Expression" in editor and it'll tell you what values are passed to the code.  It'll probably be something like Parameters passed are [<unit spawned>, <favorite food>].  So you'd do:

(_this select 0) addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];

So for my example available to the code in the Expression would be:

 

_this = [<unit spawned>, <favorite food>];

_this select 0 = <unit spawned>;

_this select 1 = <favorite food>;

 

(I'm away from the editor at the moment and can't check directly to see what values they are. :))

It says passed arugments are [<group>, <module>, <groupData>]. So I guess it targets the group and not the individual units.

 

So would something like this work?:

{_x  addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];} foreach units group (_this select 0)

Share this post


Link to post
Share on other sites

Almost perfect :)  You already know the group though, so:

{_x addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];} foreach units (_this select 0)

Share this post


Link to post
Share on other sites

 

Almost perfect :)  You already know the group though, so:

{_x addEventHandler ["killed",{[_this select 0,-1,true] call bis_fnc_respawnTickets;}];} foreach units (_this select 0)

Thanks. It seems to be working. I really appreciate your help and I'm sure all the frustrated people searching google for "how to make ai reduce tickets on death" and variations thereof will appreciate it too. You're the man. ;)

  • Like 1

Share this post


Link to post
Share on other sites

I am using the Spawn AI modules in combination with Sector Tactics but I want to be able to set a condition to where : If one side, i.e. BLUFOR, controls a sector that side is the only side that can spawn near it. Then the side that controls the sector can utilize the synchronized spawn points near that sector. If BLUFOR loses control of that sector then those spawn points will not be enabled for them until BLUFOR takes back control of that sector. Now this is just for AI not player respawns. How would I go about doing that?

Share this post


Link to post
Share on other sites

So negative values do work for the respawn tickets function? 

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

×