PDA

View Full Version : generate sleep in eventhandler execution



AKerOn
Aug 28 2010, 14:41
Hi, i am creating a script which is used to spawn a group of unit on a previously created marker. My goal is to manage in the best way how units are deleted when they are killed to prevent useless treatment. The script is called with an action added to the player action list. A random number is used to choose the marker with a switch on it. Then units are created and event handler "killed" a added on each units.



_rNumber=random 1;
_index=round _rNumber;

switch(_index) do
{
case 0 :
{
hint("OK GrpM");
_Leader = Grp1 createUnit ["RU_Soldier_Pilot", getMarkerPos "GrpM", [], 0, "FORM"];
_Unit2= Grp1 createUnit ["RU_Secretary1", getMarkerPos "GrpM", [], 0, "FORM"];
//_Unit3="RU_Secretary1" createUnit [getMarkerPos "GrpM", Grp1, "", 0.1, "Corporal"];
//_Unit4="RU_Secretary1" createUnit [getMarkerPos "GrpM", Grp1, "", 0.1, "Corporal"];
//_Unit5="RU_Secretary1" createUnit [getMarkerPos "GrpM", Grp1, "", 0.1, "Corporal"];
//_Unit6="RU_Secretary1" createUnit [getMarkerPos "GrpM", Grp1, "", 0.1, "Corporal"];
//_Unit7="RU_Secretary1" createUnit [getMarkerPos "GrpM", Grp1, "", 0.1, "Corporal"];

{
_x addEventHandler ["killed" , {
hint format["%1 a tué %2", (_this select 1), (_this select 0)];
sleep 20;
(_this select 0) removeAllEventHandlers "killed";
deleteVehicle (_this select 0);
}
];
} foreach units Grp1;


};
case 1 :
{
hint("OK GrpM2");
_Leader = Grp1 createUnit ["RU_Soldier_Pilot", getMarkerPos "GrpM2", [], 0, "FORM"];
_Unit2= Grp1 createUnit ["RU_Secretary1", getMarkerPos "GrpM2", [], 0, "FORM"];
//_Unit3="RU_Secretary1" createUnit [getMarkerPos "GrpM2", Grp1, "", 0.1, "Corporal"];
//_Unit4="RU_Secretary1" createUnit [getMarkerPos "GrpM2", Grp1, "", 0.1, "Corporal"];
//_Unit5="RU_Secretary1" createUnit [getMarkerPos "GrpM2", Grp1, "", 0.1, "Corporal"];
//_Unit6="RU_Secretary1" createUnit [getMarkerPos "GrpM2", Grp1, "", 0.1, "Corporal"];
//_Unit7="RU_Secretary1" createUnit [getMarkerPos "GrpM2", Grp1, "", 0.1, "Corporal"];

{
_x addEventHandler ["killed" , {
hint format["%1 a tué %2", (_this select 1), (_this select 0)];
sleep 20;
(_this select 0) removeAllEventHandlers "killed";
deleteVehicle (_this select 0);
}
];
} foreach units Grp1;

};
};


It seems that the sleep called in the eventhandler "killed" is not executed, the insctruction seems to be passed as the unit is deleted directly after i killed it. I didn't find anything about eventhandler context which is not interruptible, but it seem's to be the problem for me. Can somebody tell me if i am wrong or if this is not the good way to delete units.

Thanks

ruebe
Aug 28 2010, 15:27
event handlers are called (like functions), so you can't sleep in there. But it's actually quite easy to do what you want. Instead of sleeping in the event handler, you spawn a new thread and inthere you can sleep until you're funny:


(_this select 0) spawn {
sleep 20;
_this removeAllEventHandlers "killed";
deleteVehicle _this;
};


Oh, and you're in the wrong forum.