Home   Help Search Login Register  

Author Topic: Locality of Actions  (Read 1273 times)

0 Members and 1 Guest are viewing this topic.

Offline Rommel92

  • Members
  • *
Locality of Actions
« on: 31 Mar 2008, 10:04:26 »
I have an action, the player pushes it, it launches a script. (player addaction ["action","script.sqf"];)
Simple enough so far.
Now I want to get the server to execute something from that script.

script.sqf
Code: [Select]
[server] execVM "script2.sqf"
And to make a variable/code global from that script I would just:
Code: [Select]
publicVariable "It";???

$10 says that the server doesn't execute it because its local to the client or...???[/i]

I bet the experts get sick of these questions, but I guess it just can't be said enough.

Offline Loyalguard

  • Former Staff
  • ****
Re: Locality of Actions
« Reply #1 on: 31 Mar 2008, 11:33:38 »
You are correct in that scripts executed by an addAction are local to the client it was run on.  To achieve what you are looking for me I would recommend something like this (requires 1.09 or higher although there is a way to do it with 1.08).

In your init.sqf or somewhere else at mission start put the following code:

init.sqf
Code: [Select]
if (isServer) then
{
     "clientAction" addPublicVariableEventHandler {/*Insert code you want executed when client runs addAction*/};
};

This sets up a public variable event handler on the server only that waits for the for pertinent public variable to be changed and then executes whatever code you want every time it does.

Then in your script.sqf add the following code:

script.sqf
Code: [Select]
clientAction = true; publicVariable "clientAction";
This broadcasts the public variable that the server event handler is waiting for so that the server can run the code you want.  I made the variable boolean but you can you any type of variable.

There is more to the addPublicVariableEventHandler command that is available so check it in the Comref.  I hope this helps!

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Locality of Actions
« Reply #2 on: 31 Mar 2008, 12:28:17 »
And if you dont want to use betas, in your init.sqf:
Code: [Select]
// init.sqf
clientAction = false;

if (isServer) then
{
   []spawn
   {
      while {true} do
      {
         waitUntil {clientAction};
         clientAction = false;
         [] spawn
         {
            // Do whatever is needed here
         };
      };
   };
};

Your local client action should
Code: [Select]
clientAction = true; publicVariable "clientAction"