Jump to content
Sign in to follow this  
captncaps

Passing _x to EventHander, to Script

Recommended Posts

Hi.

I don't really know how to explain it, could you just look at my scripts?

My script "check.sqf", which is executed from the init.sqf:

while {true} do {
_heli = nearestObjects[player, ["Air"], 50000];
{_x addEventHandler ["HandleDamage", {0 =[[color="#FF0000"]_x[/color]]execVM "helicrash.sqf"}];}forEach _heli;
sleep 10;
};

Heli crash script starts like this:

 _heli = _this select 0;

So, _x is any Air Vehicle spawned, and should be passed into _heli (in the helicrash.sqf) which them modifies that vehicle.

You can see the red marked _x. I think this is the problem.

I guess from the "forEach" loop, I would need to pass _x into the eventhandler to then pass it into my script?

But how do I do that?

Or should I do it completly different? I need the script to run parallell for multiple objects, else only 1 Heli can crash at a time.

Thanks in advance :)

Share this post


Link to post
Share on other sites

Just use _this instead of _x

Your script is wrong by the way. You should check if the eh has been already assigned and, if not, assign it. You could use setVariable command for that.

Share this post


Link to post
Share on other sites

Ok, thanks for the info.

I just started scripting recently.

But I don't really understand now how I should check if the EH has already been assigned, and then, if needed, assign it.

Could you help me there some more?

Edit: I tried

{_x addEventHandler ["HandleDamage", {0 =[_this]execVM "helicrash.sqf"}];}forEach _heli;

(meaning _this is the same object as _x right?)

But now I get "type Array, expected Object" in my helicrash.sqf

I thought _x represents an item of the array?

Edited by CAPTNCAPS

Share this post


Link to post
Share on other sites

I thought _x represents an item of the array?

Yes exactly, but this case is different, because you can't pass the _x to the code assigned with addEventHandler. _x is nothing inside the eh.

_this in this case is already an array, so you don't need the [].

{_x addEventHandler ["HandleDamage", {_this execVM "helicrash.sqf"}];}forEach _heli;

A rough example for checking if the eh is already assigned:

if (isNil {_x getVariable "eh_assigned"} then {_x setVariable ["eh_assigned", true]; _x addEventhandler [bla bla];};

Share this post


Link to post
Share on other sites

Have you noticed any game freezes/stutters? Because you should.

Running nearestObjects, in a loop, with 50km radius, is a very bad idea. nearestObjects with a large radius is extremely heavy on the engine.

Please use nearEntities instead, and think if you could lower the radius, maybe (2000) 2km or so? If not, you should rethink the structure of your system.

As Giallustio said, event handlers have their own variables, which you can pass on. For HandleDamage, the object itself (the heli) would be _this select 0.

I'd suggest (incl. Giallustio's tip):

while {true} do {

_heli = player nearEntities ["Air", 2000]; // nearEntities is better for performance, keep the radius to a minimum

if ((count _heli) > 0) then { // Anything found at all?
	{
		if (isNil {_x getVariable "eh_assigned"}) then { _x setVariable ["eh_assigned", true]; _x addEventHandler ["HandleDamage", { 0 = [(_this select 0)] execVM "helicrash.sqf"; }]; };
	} forEach _heli;
};

sleep 10;

};

Also keep in mind that "Air" will include planes as well, not just helicopters. "Helicopter" might be a better choice.

Edited by sxp2high

Share this post


Link to post
Share on other sites

Ok, thank you

@sxp2high:

Yes, it did stutter a little bit (not much more than usual though)

But for some reason it seems the script is run every second or so.

The fire effects I create in the script are spawned every second... (dropping fps very much)

Do you know why?

Ok, my stupidness, I now use EH "hit" ; maybe I should check the whole list of EH's next time, before using the first one that may or may not do what I want ^^'

Edited by CAPTNCAPS

Share this post


Link to post
Share on other sites

That approach to find aircrafts is wonky as hell.

There's no need to do a 50.000m search with nearobjects, ouch.

I wouldn't even use nearentities for that,

since there's already the allvehicles array where you can find the aircrafts.

Share this post


Link to post
Share on other sites

If you mean vehicles, nearEntities is still the better choice for this task.

You made me curious, and I just compared 4 methods... nearEntities is superior, by far:

IhEPcom.jpg

yzeuw5d.jpg

ddtrvKg.jpg

WKxjmzO.jpg

The code I posted is just fine, you might even crank it up to 3000 if you deem it necessary. nearEntities doesn't seem to bother the engine at all, it's a nice command.

Edited by sxp2high

Share this post


Link to post
Share on other sites

Interesting results, would have never thought that, the question now is, is nearEntities actually faster when there's a lot going on on the map with a bigger radius like 10-15k?

Thanks for the share, appreciate it!

Cheers

Share this post


Link to post
Share on other sites

I actually ran all these test in a "real environment", a mission with a bit over 100 AIs fighting each other, vehicles driving around, a FOB with a lot of objects, civilians, and quite a few scripts running.

10.000 & 20.000 still have pretty good results, 387 objects were returned with "All".

tp5WH36.jpg

tETFvqi.jpg

Edited by sxp2high

Share this post


Link to post
Share on other sites
I actually ran all these test in a "real environment", a mission with a bit over 100 AIs fighting each other, vehicles driving around, a FOB with a lot of objects, civilians, and quite a few scripts running.

10.000 & 20.000 still have pretty good results, 387 objects were returned with "All".

http://i.imgur.com/tp5WH36.jpg

http://i.imgur.com/tETFvqi.jpg

Oh well, gotta make more use of that performance check.

Time to rewrite stuff I guess.

Thanks for the info!

Share this post


Link to post
Share on other sites
I actually ran all these test in a "real environment", a mission with a bit over 100 AIs fighting each other, vehicles driving around, a FOB with a lot of objects, civilians, and quite a few scripts running.

10.000 & 20.000 still have pretty good results, 387 objects were returned with "All".

http://i.imgur.com/tp5WH36.jpg

http://i.imgur.com/tETFvqi.jpg

Lol, why is it faster with a bigger radius? :D

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  

×