Jump to content
Sign in to follow this  
Tankbuster

sending player ojbect via a fired EH param?

Recommended Posts

Is there any issues that I need to be aware of with this?

I'm getting the player object from a sqf that runs a dialog and passes the player and a few other params to a script that runs on the server only. That works fine, the player object arrives safely via a parameter.

This script then places a fired EH on a vehicle and one of the parameters the EH send to another script is the same player object, but it never arrives in the last script.

Why might this be happening?

Share this post


Link to post
Share on other sites

It may be due to this, which is from the description on the addEventHandler command:

Every event will create an array named _this, which contains specific information about the particular event. (e.g. the "killed" EH will return an array with 2 elements: the killed unit, and the killer.)

So, you might need to refer to the player as one of the elements of the "_this" array. But I'm kind of talking passed the line of my understanding on event handlers, but I figured I could give it a shot :p.

Share this post


Link to post
Share on other sites

Ah, ok. I see your point. I didn't quite understand when I first read what you said. :)

The thing is, I'm using the fired EH and it's on an AI unit, so none of the results the EH gives is the player unit.

Share this post


Link to post
Share on other sites

Your running the fired EH on a vehicle

Maybe the fired EH is returning a different firing object that the expected player, maybe the effective commander etc.

What does the debug output for the Fired EH return?

Share this post


Link to post
Share on other sites

I only take the select 0 and 6 from the EH. That's the round it's released and the UAV itself and it returns them both reliably.

_uaveh = _uav addeventhandler ["Fired",{[_this select 6, _dude, _this select 0] execVM "x_server\x_f\fn_snapshot.sqf"}];

_dude (player, in this case alpha_1) exists in the script that adds the EH, but by the time snapsot runs, _this select 1 is ANY. Very weird.

Share this post


Link to post
Share on other sites

Tried replacing _dude with a global variable? I know its not the best alternative but my best guess (and I'll probably be wrong as always) is that _dude doesnt find its way onto the new script as its in a completely seperate instance. How did you debug it so you know that _dude is a proper value inside the array?

Share this post


Link to post
Share on other sites

_dude in the EH is undefined, the piece of code run by the EH is a new scope that has no association with the scope that initialized it.

You can format _dude into the code as long as it has a VarName e.g

call compile format["%1 sidechat 'hello'", player]

does not work as the string ends up looking like "B Alpha 1-1:1 (Larrow) sidechat 'hello'" and will throw an error.

So you can either

if (vehicleVarName _dude == "") then {
_dude setVehicleVarName "somename";
};
_uaveh = _uav addeventhandler ["Fired", format["[_this select 6, %1, _this select 0] execVM 'x_server\x_f\fn_snapshot.sqf'",_dude]];

or

_uaveh = _uav addeventhandler ["Fired", format["[_this select 6, %1, _this select 0] execVM 'x_server\x_f\fn_snapshot.sqf'",_dude call BIS_fnc_objectVar]];

Where BIS_fnc_objectVar does pretty much the same as the IF statement, if it has a VarName it just returns it, if it does not then it make one for it a returns it.

VarName is not quite the same beast as a global variable as it becomes the actually string representation of the object but for scripting purposes can used and thought of as one.

Share this post


Link to post
Share on other sites

Guys,

Thanks for explaining this, I'll try all of the above.

But meantime, I could use some clarification on this scope/instance thing. _dude is privated at the top of the script so I thought it would be defined across the entire script, but you are telling me that the code launched by the EH is a brand new, totally unconnected script. So in this code, I can't use local variables from the originating script? And grabbing dude from OUTSIDE the new scope using format or objectvar means it's still in the scope of the original script?

Well, I didn't know all of this. And now I do. Thanks guys, much appreciated.

PS. I might go the global variable route first. It's ugly but simple, though if I'm honest, I'm pretty sure I've already tried that and got the same results. I'll try again now.

edit: yep, using a global works a treat and of course, it's a simple find replace in the script to deploy!

---------- Post added at 19:21 ---------- Previous post was at 18:22 ----------

Edited by Tankbuster

Share this post


Link to post
Share on other sites
But meantime, I could use some clarification on this scope/instance thing. _dude is privated at the top of the script so I thought it would be defined across the entire script, but you are telling me that the code launched by the EH is a brand new, totally unconnected script.
Yes. You are passing a bit of code to the engine to be run when the event fires. This code has no association with the scope you sent it from.
So in this code, I can't use local variables from the originating script?
No. Think of the code you are passing to the EH as a string, you are not passing something that is interpreted before it is sent, _dude will not interpreted to be the object it holds it will be passed exactly as written (thats why is say think of it like a string). When the event fires the engine looks at the code and tries to interpret it but when it gets to the word/variable _dude it has no idea what this is.
And grabbing dude from OUTSIDE the new scope using format or objectvar means it's still in the scope of the original script?
As above if you think of the passed code like a string (which with format it now is) you are specifically inserting a reference to the object straight into the string and not relying on a local variable that holds a reference like _dude was.

Trying to think of a way to show this off....

Start a new editor session and just place a soldier as a player and hit preview

Run the below code from the debugconsole

fnc_myFunction = {
_code = _this select 0;
systemChat format ["Recieved: %1", str _code];

if (typeName _code == typeName "") then {
	systemChat format["Trying to run: call compile %1;", str _code];
	call compile _code;
}else{
	systemChat format["Trying to run: call %1;", str _code];
	call _code;
};
systemChat "------------";
};

Imagine this is the engine trying to process your EH, the EH has fired straight away and this is it trying to compile the code/string snippet you sent it

Then in turn run each one of these code snippets from the debugConsole

//_dude is not interpreted before it is sent as code
h = [] spawn {
_dude = player;
[compile "hint name _dude"] spawn fnc_myFunction;
};

//_dude is not interpreted before it is sent as a string
h = [] spawn {
_dude = player;
["hint name _dude"] spawn fnc_myFunction;
};

//_dude is specifically inserted into a string before it is sent
h = [] spawn {
_dude = player;
[format["hint name %1", _dude call BIS_fnc_objectVar]] spawn fnc_myFunction;
};

//_dude is specifically inserted into code before it is sent
h = [] spawn {
_dude = player;
[compile format["hint name %1", _dude call BIS_fnc_objectVar]] spawn fnc_myFunction;
};

Take note of the chat message showing what the function received and what it is trying to do with it.

Maybe not the best example but :/

Share this post


Link to post
Share on other sites

It's a great example, Larrow and I'm very grateful. Thank you very much for explaining it.

And thank you to tryteyker for the suggestion. I went the global variable route and it worked first time. :)

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  

×