Jump to content
Taylor1984

Make eventhandler script run on all clients

Recommended Posts

Hi

 

I have this addEventHandler with "fired" that runs a script with some burning flames effect when you fire your rifle. Problem is only the host can see the effects, even though the eventhandler is run from the soldier of a client. I've read some about how to make eventhandlers global but I don't really understand much of it. Does anyone know how to make the script run on all machines?

 

Thanks

Share this post


Link to post
Share on other sites

Based on the Biki for the Fired EH, the Fired EH will trigger even when the firing unit is remote (not local). The "effect", being the code that is executed, is local to the machine that the EH was added from (not necessarily local to the machine that triggered the EH). The importance of this is that you can do whatever you want from wherever you want so long as you respect locality. In other words:

YourObject addEventHandler ["Fired", {
	//Local to clientOwner, not necessarily owner YourObject
	{local flame thrower code}; //Only seen on this machine
}];

Knowing the above and about remoteExec, you should recognize two options:

1. Define your Fired EH on all machines (can do this a number of ways), execute code as-is

2. Define your Fire EH on one machine, and execute the code in a public manner (usually global effect commands and/or remoteExec). 

 

Hope that helps!

Share this post


Link to post
Share on other sites

Thanks. It seems as if the "fired" eventhander is actually run on all machines and everything within the script works except for

null = [_f1,5,2,0.8,true,true,"small"] spawn compile preprocessFile "ALfire\fire.sqf"

which is a script spawning fire and it only seem to be visible to the host. So how would I make that script run on all machines?
*Edit*
I found a solution that works: 

[[[_f1,5,2,0.8,true,true,"small"],"ALfire\fireflamethrow.sqf"],"BIS_fnc_execVM",true,true] spawn BIS_fnc_MP;

However I'm afraid this is bad for performance when used in multiplayer? Isn't compile better to use?

 

Thanks

Edited by Taylor1984
Found a possible solution

Share this post


Link to post
Share on other sites
22 minutes ago, Taylor1984 said:

Thanks. It seems as if the "fired" eventhander is actually run on all machines and everything within the script works except for


null = [_f1,5,2,0.8,true,true,"small"] spawn compile preprocessFile "ALfire\fire.sqf"

which is a script spawning fire and it only seem to be visible to the host. So how would I make that script run on all machines?

 

Thanks

 

Regarding the event handler triggering on all machines, this is probably because you have added it in an event script executed by all machines (or something coming after it), such as init.sqf. Which is fine; that is point #1 above.

 

Regarding only the host seeing the effect, we would need to see fire.sqf (or at least the relevant parts). A quick locality test would be to remoteExec your compiled preprocessed fire.sqf to everyone and just see what happens. However, if the EH really triggers on every client, this should not be your solution, just a test. 

 

On that note, a quick optimization tip:

Consider using execVM instead of what you are doing now.

Next, because fire.sqf will probably be executed quite a bit, consider preprocessing it into a function that you can then spawn. 

Share this post


Link to post
Share on other sites
4 minutes ago, KC Grimes said:

Next, because fire.sqf will probably be executed quite a bit, consider preprocessing it into a function that you can then spawn. 

Thanks. Right, here's the part that's tricky for me because I have no idea how to do that. It would be nice, though. :)
Also, you noticed that I've edited my second post and found a solution that worked?

 

Thanks

Share this post


Link to post
Share on other sites
fnc_Fire = compile preprocessFileLineNumbers "ALfire\fire.sqf";

YourObject addEventHandler ["Fired", {
	//Local to clientOwner, not necessarily owner YourObject
	_yourReturn = [_f1,5,2,0.8,true,true,"small"] spawn fnc_Fire;
}];

@Taylor1984 Well like this! Just a matter of moving around what you already have. Make sure you look at compile and preprocessFileLineNumbers so you know what is actually happening, and why this will be more efficient.

 

But that said, no use optimizing if it isn't doing what you want in the first place. Let us know how the testing goes. 

Share this post


Link to post
Share on other sites
_yourReturn = [_f1,5,2,0.8,true,true,"small"] spawn fnc_Fire

works great in singleplayer but I still only get the following to work in multiplayer:

[[[_f1,5,2,0.8,true,true,"small"],"ALfire\fireflamethrow.sqf"],"BIS_fnc_execVM",true,true] spawn BIS_fnc_MP

The following actually works but gives me an error message at the bottom of the screen:

[[[_f1,5,2,0.8,true,true,"small"]spawn fnc_Fire],"BIS_fnc_execVM",true,true] spawn BIS_fnc_MP

The error message is:

 [BIS_fnc_execVM] Error: size 1, expected 2, in [<spawn>#line 1 (and then the path to the mission file)

Is there a way to combine the two lines and make it work in multiplayer? I surely have no idea what I'm doing but some of you are fortunately pros.

 

Cheers

Share this post


Link to post
Share on other sites
16 hours ago, Taylor1984 said:

_yourReturn = [_f1,5,2,0.8,true,true,"small"] spawn fnc_Fire

works great in singleplayer but I still only get the following to work in multiplayer:


[[[_f1,5,2,0.8,true,true,"small"],"ALfire\fireflamethrow.sqf"],"BIS_fnc_execVM",true,true] spawn BIS_fnc_MP

The following actually works but gives me an error message at the bottom of the screen:


[[[_f1,5,2,0.8,true,true,"small"]spawn fnc_Fire],"BIS_fnc_execVM",true,true] spawn BIS_fnc_MP

The error message is:


 [BIS_fnc_execVM] Error: size 1, expected 2, in [<spawn>#line 1 (and then the path to the mission file)

Is there a way to combine the two lines and make it work in multiplayer? I surely have no idea what I'm doing but some of you are fortunately pros.

 

Cheers

 

As you can see in the biki for BIS_fnc_MP, you should be using remoteExec instead. 

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

×