Home   Help Search Login Register  

Author Topic: publicVariable help  (Read 2933 times)

0 Members and 1 Guest are viewing this topic.

Offline Hawke

  • Members
  • *
publicVariable help
« on: 02 Jan 2009, 12:57:23 »
Hey,

Im just learning how to use the publicVariable command and i was woundering if you can make a publicVariable call a script.

Code: [Select]
global_command = "[flag,Lgate,Rgate,0,3] exec (log\scripted_gate.sqs)"
publicvariable "global_command"


Offline i0n0s

  • Former Staff
  • ****
Re: publicVariable help
« Reply #1 on: 02 Jan 2009, 13:14:02 »
Code: [Select]
global_command addPublicVariableEventHandler {call compile (this select 1)};

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: publicVariable help
« Reply #2 on: 04 Jan 2009, 17:32:03 »
Remember that publicVariable handlers are only run remotely, not locally, so you need to do a bit more to ensure your code runs globally (both remotely and locally).

To do what you asked for is an entirely generic system to run code:
Code: (add handler) [Select]
global_command_handler = { call compile _this };
"global_command" addPublicVariableEventHandler {  (_this select 1) call global_command_handler };
Code: (run function globally) [Select]
global_command = "[flag, Lgate, Rgate, 0, 3] exec ""log\scripted_gate.sqs""";
publicVariable "global_command"; // Run remotely.
global_command call global_command_handler; // Run locally.

However, what you should probably do in this case, assuming you really have to use sqs, is:
Code: (add handler) [Select]
open_gate_handler = { _this exec "log\scripted_gate.sqs" };
"open_gate" addPublicVariableEventHandler { (_this select 1) call open_gate_handler };
Code: (run function globally) [Select]
open_gate = [flag, Lgate, Rgate, 0, 3];
publicVariable "open_gate"; // Run remotely.
open_gate call open_gate_handler; // Run locally.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Hawke

  • Members
  • *
Re: publicVariable help
« Reply #3 on: 06 Jan 2009, 22:52:28 »
@ spooner

Hey, your idea worked when i tryed hosting a session but when i tryed it on my dedicated server, the server wouldn't move the wall but it would on the clients. so all you would see is a wall moveing backwards and forwards like the server didnt run the script so it kept pushing the wall back where it came from after every 3 seconds.

any idea why ??? ???
« Last Edit: 07 Jan 2009, 00:15:52 by Hawke »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: publicVariable help
« Reply #4 on: 07 Jan 2009, 19:14:42 »
There are any number of reasons why this wouldn't work as you'd expect. However, without knowing anything about the gate object, about "log\scripted_gate.sqs", or how you are running the code I gave you, I have no idea why exactly it wouldn't be working...
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Hawke

  • Members
  • *
Re: publicVariable help
« Reply #5 on: 07 Jan 2009, 20:21:19 »
Ok,

first off there is a flag on the map named flag that has the open called "open gate", once you have hit the action it open a dialog in which you have to enter a password pops up,

Code: [Select]
#ACOY
_ok = createDialog "DlgTutorial2"
?!(dialog): exit
#ACOY1

_buffer = ctrlText 202
_action = format ["%1", _buffer]
buttonSetAction [204, _action]


? _action == "password" : goto "Open"



?!(dialog): goto "death"
goto "ACOY1"



#Open
closedialog 0
titletext [format ["Welcome to 3 PARA's armoury , %1",name player],"plain down"]
[] exec "log\doors.sqs"

#death
closedialog 0
titletext [format ["The area you are trying to access is restricted area, %1",name player],"plain down"]

exit

when the password is entered it runs the door.sqs which has the publicvariable you sent me. At the moment its in a .sqs, i even tryed a .sqf
Code: [Select]
open_gate_handler = { _this exec "log\scripted_gate.sqs" };
"open_gate" addPublicVariableEventHandler { (_this select 1) call open_gate_handler };

open_gate = [flag, Lgate, Rgate, 0, 3];
publicVariable "open_gate";
open_gate call open_gate_handler;

Then the publicvariable should run the scripted_gate.sqs

Code: [Select]
_obj = _this select 0
_leftgate = _this select 1
_rightgate = _this select 2
_open_or_close = _this select 3
_dist = _this select 4


_gatedir_left = getdir _leftgate
_gatedir_right = getdir _rightgate
_dir = _gatedir_left - 90
_dir2 = _gatedir_right + 90


_sp = 0.01



?(getdammage _obj> 0.5 OR GETdammage _leftgate > 0.5 OR Getdammage _rightgate > 0.5) : hint "Mechanism is damaged"; exit

;reverse the direction when to gate closes
?_open_or_close == 0:_dir = _dir - 180
?_open_or_close == 0: _dir2 = _dir2 - 180

_pos = getpos _leftgate
_pos2 = getpos _rightgate


_leftgate setpos _pos
_rightgate setpos _pos2


_x = (_pos select 0) - ((getpos _leftgate) select 0)
_y = (_pos select 1) - ((getpos _leftgate) select 1)
_z = (_pos select 2) - ((getpos _leftgate) select 2)

_x2 = (_pos2 select 0) - ((getpos _rightgate) select 0)
_y2 = (_pos2 select 1) - ((getpos _rightgate) select 1)
_z2 = (_pos2 select 2) - ((getpos _rightgate) select 2)


_pos = getpos _leftgate
_pos2 = getpos _rightgate



_leftgate setpos [(_pos select 0)+2*_x,(_pos select 1)+2*_y,(_pos select 2)+2*_z]
_rightgate setpos [(_pos2 select 0)+2*_x2,(_pos2 select 1)+2*_y2,(_pos2 select 2)+2*_z2]


_md = _dist / (1 - _sp)

#1oop
_pos = getpos _leftgate
_pos2 = getpos _rightgate


_leftgate setpos [(_pos select 0)+_x+((sin _dir)*_sp),(_pos select 1)+_y+((cos _dir)*_sp),(_pos select 2)+_z]
_rightgate setpos [(_pos2 select 0)+_x2+((sin _dir2)*_sp),(_pos2 select 1)+_y2+((cos _dir2)*_sp),(_pos2 select 2)+_z2]

~_sp

_md = _md - _sp
?(_md >= 0): goto "1oop"

exit

so don't know where it's going wrong. :dunno:

Offline Hawke

  • Members
  • *
Re: publicVariable help
« Reply #6 on: 10 Jan 2009, 20:55:27 »
Sorry for bumbing, but is any one going to help me????

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: publicVariable help
« Reply #7 on: 11 Jan 2009, 00:51:26 »
I gave you these bits of code in two parts for a reason:
Code: (add handler (run this at the start of the mission, for example in init.sqs)) [Select]
open_gate_handler = { _this exec "log\scripted_gate.sqs" };
"open_gate" addPublicVariableEventHandler { (_this select 1) call open_gate_handler };
Code: (run function globally (run this locally, to have scripted_gate.sqs run globally)) [Select]
open_gate = [flag, Lgate, Rgate, 0, 3];
publicVariable "open_gate"; // Run remotely.
open_gate call open_gate_handler; // Run locally.

The first part adds the handler and must be done on all machines at the start of the mission (e.g. from init.sqs or init.sqf). It sets up an event handler which allows everyone to see the effect. The second part actually invokes the handler on all machines. I think the problem is that if you run all that only when you open the gate via an action, the handlers have not been created on all machines already, so the animation (scripted_gate.sqs) will only occur on the gate-opener's machine.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Hawke

  • Members
  • *
Re: publicVariable help
« Reply #8 on: 11 Jan 2009, 10:55:16 »
It works now, cheers. But you never said any thing about adding the EH in the init on the first post  :P


Offline Rommel92

  • Members
  • *
Re: publicVariable help
« Reply #9 on: 28 Jan 2009, 14:24:00 »
OR....

Code: [Select]
_this setvehicleinit format["%1 exec 'log\scripted_gate.sqs'", _this];
processinitcommands

 ;)