Jump to content
Sign in to follow this  
lawman_actual

Carry unit name through killed eventHandler?

Recommended Posts

Hi All

I'm attempting to keep an array of units that are still operational on the field throughout a mission, which involves removing a unit from the array once it is killed.

I've got the killed eventHandler working smoothly for the player, but when it comes to the AI I can't seem to carry the units name through using the event handler.

For example, I have my array of units that aren't dead: potentialunits = [..., ..., ...]

Previously I had been attempting to set a variable, deceased, to the units name using:

 this addEventHandler ["killed", {null = call  {deceased = this; execVM "respawni.sqf";}}];

However when the script, respawni.sqf is run it tells me that deceased is an undefined variable.

Conclusion

Either this doesn't work when used in a killed eventHandler, OR the variable is for some reason not being set to the units name and/or carried through to the script.

does anybody know how i can get this working?

If i can't obtain the unit ID through this how else do I subtract it from the array?*

* bare in mind that the units name may not be known to me as I have many units being created after mission start

Thanks!

Share this post


Link to post
Share on other sites

this addEventHandler ["killed", {null = call {_this execVM "respawni.sqf";}}];

In respawni.sqf the killed unit is: _this select 0

Share this post


Link to post
Share on other sites
this addEventHandler ["killed", {null = call {_this execVM "respawni.sqf";}}];

In respawni.sqf the killed unit is: _this select 0

Many thanks, I'll test it out when I get back to it in a few hours

Can you clarify what's happening exactly?

You're saying (and correct me if I'm wrong) that within respawni.sqf the unit is the first entry in an array known as _this ?

That doesn't make much sense to me but if that's the way it works then I guess I can accept it :)

Share this post


Link to post
Share on other sites

When the unit is killed, this get executed:

null = call {_this execVM "respawni.sqf";}

_this is an array of [theDeadGuy,Killer] which is passed to respawni.sqf as a parameter:

_deceased = _this select 0;

_killer = _this select 1;

Share this post


Link to post
Share on other sites
When the unit is killed, this get executed:

null = call {_this execVM "respawni.sqf";}

_this is an array of [theDeadGuy,Killer] which is passed to respawni.sqf as a parameter:

_deceased = _this select 0;

_killer = _this select 1;

Understood

Thanks for the assistance :)

---------- Post added at 15:17 ---------- Previous post was at 13:33 ----------

Slight hitch....

_this wasn't working as the handle, it still said undefined variable for _deceased

so I tried using the global variable of myvariable

Seemed to work, but when i tried to select the first value in this array it tells me that input was a script, when a string etc was expected.

Here's what I have;

init of unit:

this addEventHandler ["killed", {null = call  {myarray = execVM "respawni.sqf";}}];

respawni.sqf:

//start of script

_deceased = myarray select 0;
potentialunits = potentialunits - [_deceased];
_deceased = nil;
myarray = nil;

ticketcount = ticketcount +1;
//hint (format ["%1",ticketcount]);


if (ticketcount > 3) then {

ticketcount = ticketcount -4;
//hint (format ["%1",ticketcount]);


_newgroup = createGroup west;
_leadunit = _newgroup createUnit ["B_Soldier_GL_F",[135,122,0],[],0.5,"CORPORAL"];
_leadunit addEventHandler ["killed", {null = call  {_this = execVM "respawni.sqf";}}];
_leadunit moveInCargo ustruck1;
addSwitchableUnit _leadunit;



//spawnedunits = spawnedunits + [_leadunit];


_autounit = _newgroup createUnit ["B_soldier_AR_F",getMarkerPos "respawn1",[],0.5,"PRIVATE"];
_autounit addEventHandler ["killed", {null = call  {_this = execVM "respawni.sqf";}}];
_autounit moveInCargo ustruck1;
addSwitchableUnit _autounit;


_medunit = _newgroup createUnit ["B_medic_F",getMarkerPos "respawn1",[],0.5,"PRIVATE"];
_medunit addEventHandler ["killed", {null = call  {_this = execVM "respawni.sqf";}}];
_medunit moveInCargo ustruck1;
addSwitchableUnit _medunit;


_rifleunit = _newgroup createUnit ["B_soldier_F",getMarkerPos "respawn1",[],0.5,"PRIVATE"];
_rifleunit addEventHandler ["killed", {null = call  {_this = execVM "respawni.sqf";}}];
_rifleunit moveInCargo ustruck1;
addSwitchableUnit _rifleunit;


potentialunits = potentialunits + [_leadunit];
potentialunits = potentialunits + [_autounit];
potentialunits = potentialunits + [_medunit];
potentialunits = potentialunits + [_rifleunit];


doGetOut _leadunit;
_leadunit leaveVehicle ustruck1;
sleep 1;

doGetOut _autounit;
_autounit leaveVehicle ustruck1;
sleep 1;

doGetOut _medunit;
_medunit leaveVehicle ustruck1;
sleep 1;

doGetOut _rifleunit;
_rifleunit leaveVehicle ustruck1;
sleep 1;

_wpPos = getMarkerPos "point1";
_radius = 10;
_thisobjective = _newgroup addWaypoint [_wpPos, _radius];
_newgroup setCurrentWaypoint _thisobjective;

base = [west, "HQ"];
base sideChat "Fireteam Deployed at Hawk";


} else { 

};


//end of script

Share this post


Link to post
Share on other sites

_this isn't a script handle, it's the parameter passed to the sqf.

Try:

this addEventHandler ["killed", {null = call {_this execVM "respawni.sqf";}}];

And:

_deceased = _this select 0;

potentialunits = potentialunits - [_deceased];

etc.

Edited by Greenfist

Share this post


Link to post
Share on other sites

Can i ask why you are calling code that execVM's a script?

It can just be ...

this addEventHandler ["killed", { nul = _this execVM "respawni.sqf"; }];

//Respawni.sqf

_killed = _this select 0;
_killer = _this select 1;

Share this post


Link to post
Share on other sites

Larrow

I'm confused; your proposal still seems to be calling respawni.sqf using execVM?

As for WHY I'm going this; I'm working on a system whereby units are redeployed on their death, which requires the event handler and some scripting.

I appreciate that I could work the code from respawni.sqf into the event handler's outcome - but I just haven't been bothered yet ;)

I may get around to it, but doesn't it achieve the same thing either way?

Is there a particular reason that using execVM to achieve the effect is less desirable than writing it into the event handler?

Share this post


Link to post
Share on other sites

I was just confused that you were call code to spawn a thread that starts respawni.sqf, that is all.

As in as much as this would be useless todo but would work

this addEventHandler ["killed", {
_this call {
	_this call {
		_this call {
			_this call {
				nul = _this execVM "respawni.sqf";
			};
		};
	};
}

}];

Share this post


Link to post
Share on other sites

Ahh ok, I see what you mean.

I'm still fairly new to scripting and when I wrote this I had a lot of errors trying to run the .sqf from the event handler.

The script I posted was just one attempt I made that happened to work properly (probably adapted from an example I saw somewhere else) - and since it worked I never thought to look back at it (until now)

Now that you have pointed it out, and I know a bit more about it, I can see what you mean!

Thanks for the advice :)

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  

×