Server regulated client script executing

ExClouds

New Member
Hey there. :)
Lets imagine i have script "server_script.sqf" that contains this:

--------------------------------------------
while {true} do {
if (CONDITION) {
execVM "client_script_1.sqf" // execute on all clients
} else {
execVM "client_script_2.sqf" // execute on all clients
}

sleep 15
}
--------------------------------------------

and then i have two different script files ("client_script_1.sqf", "client_script_2.sqf"), that should be executed by server command.. So, when server says, all users execute the same file at the same time.

How do i do this?
I tried using "Remote Script Launcher (RSL)" http://www.armaholic.com/page.php?id=13912
But it had internal errors, that i couldn't fix.

Any ideas?
 
These are the files you want
server_script.sqf
Code:
while {true} do {
if (CONDITION) {
client_script = client_script_1;
publicvariable "client_script";

} else {
client_script = client_script_2;
publicvariable "client_script";
};

sleep 15
};
What this file is going to do is monitor your CONDITION and set the variable to the name of the script we want to execute. What is important is the PUBLICVARIABLE. This broadcasts that variable (client_script) to all computers. On each computer will be a publicvariable EVENTHANDLER that will run code whenever that public variable is broadcast

client_script.sqf
Code:
switch (client_script) do{

      case "client_script_1": {
            hint "CLIENT_SCRIPT_1 EXECUTED";
       };

      case "client_script_2": {
            hint "CLIENT_SCRIPT_2 EXECUTED";
      };
};

and finally to tie it all together, we need to ADD our public eventhandler to each client so they know to run the code when that variable is broadcast, and we need to run the server script on the server.
Add this to your init.sqf
Code:
//run the server script on the server ONLY
if (isdedicated) then { execvm "server_script.sqf"};

//add the event handlers to the clients ONLY
if (!isdedicated) then {
"client_script" addpublicvariableeventhandler { execvm "client_script.sqf"};
};

I just wrote this here, its untested and may have some errors but its 99% correct anyways. What I suggest you do first is a simple test to make sure it works ... if it works you will get a hint box pop up saying which code is being claled.
When you KNOW its working, then you can replace the hints with actual code.
And of course make sure you use the correct file paths rather than my 'root folder' generic code paths.

Let me know how it works out and any errors. Post your arma2oaserver.rpt and the files if there is any problems.
 
Last edited:
These are the files you want
server_script.sqf
Code:
while {true} do {
if (CONDITION) {
client_script = client_script_1;
publicvariable "client_script";

} else {
client_script = client_script_2;
publicvariable "client_script";
};

sleep 15
};
What this file is going to do is monitor your CONDITION and set the variable to the name of the script we want to execute. What is important is the PUBLICVARIABLE. This broadcasts that variable (client_script) to all computers. On each computer will be a publicvariable EVENTHANDLER that will run code whenever that public variable is broadcast

client_script.sqf
Code:
switch (client_script) do{

      case "client_script_1": {
            hint "CLIENT_SCRIPT_1 EXECUTED";
       };

      case "client_script_2": {
            hint "CLIENT_SCRIPT_2 EXECUTED";
      };
};

and finally to tie it all together, we need to ADD our public eventhandler to each client so they know to run the code when that variable is broadcast, and we need to run the server script on the server.
Add this to your init.sqf
Code:
//run the server script on the server ONLY
if (isdedicated) then { execvm "server_script.sqf"};

//add the event handlers to the clients ONLY
if (!isdedicated) then {
"client_script" addpublicvariableeventhandler { execvm "client_script.sqf"};
};

I just wrote this here, its untested and may have some errors but its 99% correct anyways. What I suggest you do first is a simple test to make sure it works ... if it works you will get a hint box pop up saying which code is being claled.
When you KNOW its working, then you can replace the hints with actual code.
And of course make sure you use the correct file paths rather than my 'root folder' generic code paths.

Let me know how it works out and any errors. Post your arma2oaserver.rpt and the files if there is any problems.

Will test it sometime next week.
Thank You for answer. Will let You know.
 
Just to clarify. in the client_script.sqf, you are not going to execute any other scripts (although you could spawn one) but just replace the hint "" with the code you would have put into client_script_1.sqf
 
hate to necro this thread but what if I just want to move a custom client script into the server.pbo..
I understand I'd need something like
Code:
if (!isServer) exitWith {};
at the top of the script but how exactly do I execute the script? I tried calling it like a map addition in the server_functions with no luck. Should I execute it in the init.sqf somehow? Whats the best way to go about doing this. Basically Ive been having a bit of trouble with thieves and I want to hide it in my server PBO.
 
Guys,
the key functionality here is 'publicvariable'. Look it up on arma 2 and read all about it.

For example look at this piece of code:
Code:
CD_ADMIN_KillZed = {
    private ["_unit"];
    _unit = _this select 0;
    if (local _unit) then {
        _unit setDamage 1;
    } else {
        CD_ADMIN_KillZedCall  = _this;
        if (isDedicated) then {
            (owner _unit) publicVariableClient "CD_ADMIN_KillZedCall";
        } else {
            publicVariableServer "CD_ADMIN_KillZedCall";
        };
    };
};
"CD_ADMIN_KillZedCall" addPublicVariableEventHandler {
    (_this select 1) call CD_ADMIN_KillZed;
};

This would be placed both on the server and the client. No matter which one you call it from, it finds who owns the Zed and kills it.

Typically from the mission file in a location to make sure its client I launch a master script that then launches all my other client scripts. I do the same for the server usually from the server_monitor file or such.

Now to your particular question
  • Setup your client / server scripts as ShootingBlanks mentioned.
  • Then setup something a script on the server to send a public variable to the server stating to start it. Or, you could just periodically launch it from the server. No idea what it is you are wanting to do.
    • Some examples are you could add a right click option to a soda can using a 'extra_rc.hpp' file. Lots of tuts on how to do that out there.
    • You could have a marker launch the code
    • Endless ways, but the key is you are sending a 'publicvariable' to the server to kick it off.
 
Was just an idea I had for a few custom scripts because I was having some trouble with other servers stealing them out of my mission file.
Im actually pretty terrible at coding. I wanted to move the entire script to the server PBO and I suppose I would need a client file to execute that script on the client but I didnt want any of the actual custom script to be included. I suppose it's not that big of a deal for such a small script. I was just wondering how difficult it would be to do.
 
Back
Top