Jump to content
Ninjaisfast

Terminating a script midway through

Recommended Posts

Hi, I'm trying to use an event handler to terminate a script when an AI dies. The script will be manipulating items on an AI and making them use custom chat, etc. What I have works, but not for multiple AI. The script starts from an action selected on those AI. From what I can tell, if two of the several of the same script are running, the terminate script function will terminate the most recent of those scripts to have started. 

Here's my code (this is changed a little from when I last tested, so there may be a small error with the syntax):
rob1.sqf   (run from an action on AI)

Quote

_target=_this select 0;
_caller=_this select 1;
_id= _this select 2;
 

_target addEventHandler ["Killed", {hint "he dead" ; terminate rob1.sqf;}];

..._target do stuff...;


Essentially I want the script to run and affect an AI, but if that AI it to terminate. This script must be able to be run from multiple AI.

Sorry if this is hard to follow, I'm guessing my terminology is terrible. Thanks for any help.

Share this post


Link to post
Share on other sites
2 minutes ago, Ninjaisfast said:

terminate rob1.sqf

Terminate receives a script handle as argument. The handle that "spawn" returns.

Store the result of your "spawn" or "execVM" in a global variable, and then use that global variable to kill it.

  • Like 1

Share this post


Link to post
Share on other sites
8 minutes ago, Dedmen said:

Terminate receives a script handle as argument. The handle that "spawn" returns.

Store the result of your "spawn" or "execVM" in a global variable, and then use that global variable to kill it.

Sorry for the confusion, I think that might be what I was trying the first time. The action actually runs a different script, which runs the next one. 


Action starts this:
 

_target=_this select 0;
_caller=_this select 1;
_id= _this select 2;


rob1 = [_target,_caller,_id] execVM "rob1.sqf";


rob1.sqf uses:

_target addEventHandler ["Killed", {hint "he dead" ; terminate rob1;}];

Share this post


Link to post
Share on other sites

Once a script reached the end, and there are no running loops, it is terminated.

 

The tob1  script terminates as soon as it has added the event handler. The event handler is a separate process that becomes active when the assigned unit dies.. 

Share this post


Link to post
Share on other sites

Yep, I've got more code underneath for what the AI should do and adding items to them etc. But it runs regardless of if the AI is dead unless I add an event handler. 

Share this post


Link to post
Share on other sites

 

12 minutes ago, Ninjaisfast said:

Yep, I've got more code underneath for what the AI should do and adding items to them etc. But it runs regardless of if the AI is dead unless I add an event handler. 

 

You can terminate code by checking the alive state of a unit:

if (!alive MyUnit) exitWith {};

Share this post


Link to post
Share on other sites
24 minutes ago, whiztler said:

 

 

You can terminate code by checking the alive state of a unit:


if (!alive MyUnit) exitWith {};

This wouldn't work if I'm wanting to check if the unit dies mid instruction though right? If I wanted to have them say one thing, wait 10 seconds, then say another, it would still do these instructions if they died during the wait 10 seconds part? 

Share this post


Link to post
Share on other sites

startrob.sqf:

Quote

_target=_this select 0;
_caller=_this select 1;
_id= _this select 2;


rob1 = [_target,_caller,_id] execVM "rob1.sqf";

rob1.sqf
 

Quote

_target=_this select 0;
_caller=_this select 1;
_id= _this select 2;
 

_target addEventHandler ["Killed", {hint "he dead" ; terminate rob1;}];
 

_target customChat [1,"Okay okay! Don't hurt me! I'll give you everything!"];
sleep 1;
[_target, "REPAIR_VEH_KNEEL", "NONE"] call BIS_fnc_ambientAnim; sleep 10;
_target customChat [1,"Almost there! Just a few more seconds!"];
sleep 10; _target call BIS_fnc_ambientAnim__terminate;
_target addbackpack "B_Messenger_Black_F";
_target addItemToBackpack "Chemlight_red";
(unitbackpack _target) addItemCargoGlobal ["FirstAidKit", 15];
_target action ["PutBag"];
_target customChat [1,"There, take it! Now leave me alone!"];


This works fine on one person, but gets weird when two people are 'robbed', and one of them is killed. Say I started robbing person 1, ran to person 2 and started robbing them, then killed person 1 before they finished their actions, it terminates person 2's script (leaving them on their ambient animation), and continues giving person 1 items and having them talk. 

Is this the sort of thing where I just need to add if Alive _target to every single action I want to complete, else terminate the script?

Share this post


Link to post
Share on other sites
1 hour ago, Ninjaisfast said:

Is this the sort of thing where I just need to add if Alive _target to every single action I want to complete, else terminate the script?

Short answer is yes.

The quick and dirty solution is to get rid of the eventhandler and put this below each sleep: if(!alive _target) exitWith {};

Share this post


Link to post
Share on other sites

Damn, I was hoping there was something I was missing to make it neater, I'll do it the dirty way. Thanks to everyone for the replies.

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

×