Jump to content
Sign in to follow this  
eagledude4

Public variables

Recommended Posts

I've created several units that have this in their init line:

if (alive this) then {
["Alive"] execVM "copcount.sqf";
}; 
if (!(alive this)) then {
["Dead"] execVM "copcount.sqf";
};

And created this script to change the values:

copcount.sqf:

//By OneShot.J

_Type = _this select 0;

if (_Type == "Alive") then {
copcount = copcount + 1;
};

if (_Type == "Dead") then {
copcount = copcount - 1;
};
publicvariable "copcount";

But I'm not sure if I need to include "publicvariable "copcount";" every time I reference the copcount variable.

This command broadcasts a variable to all clients, but as soon as you change the variable again, you have to use publicVariable again

Reading that, I've come to the conclusion that I don't have to, but I wanted to ask the community for reassurance.

So for example, If I have:

if (copcount < 3) exitwith {
player groupchat "There needs to be atleast 3 cops online to rob the bank.";
};

in a script, do I need to also include "publicvariable "copcount";" in the script?

I'm also interested in a simpler solution.

Edited by eagledude4

Share this post


Link to post
Share on other sites

You only need to rebroadcast a publicVariable whenever it's value is changed.

Share this post


Link to post
Share on other sites

Thanks for confirming.

I'm having an issue where the variable isn't being increased when the unit is alive.

hint format ["%1", copcount];

Is showing 0 instead of 11 (the number of alive units with the

if (alive this) then {
["Alive"] execVM "copcount.sqf";
}; 
if (!(alive this)) then {
["Dead"] execVM "copcount.sqf";
};

code in their init boxes.

Edited by eagledude4

Share this post


Link to post
Share on other sites
I don't fully understand the meaning of the above. I noticed that in the mission I'm working with, certain public variables are modified from separate scripts, making the above untrue. (from my prespective)

It means that if you create a local entity in the server for example and you broadcast it to the clients through publicVariable you can't do anything with it, because it does not exist elsewhere.

An example would be a camera entity or a local object created using createVehicleLocal.

Share this post


Link to post
Share on other sites

Ooh, I see. Thanks for clarifying. Still would appreciate an answer for post 3 though :P

Share this post


Link to post
Share on other sites
The only limitation is you cannot transfer references to entities which are local, like scripts

To use the given example; this does not make sense:

MyVar = [some, Args] spawn MySuperScript;
publicVariable "MyVar";

What are the other computers supposed to do with the value in MyVar? The script is not running on their computer; they cannot use scriptDone or terminate on it for example.

Share this post


Link to post
Share on other sites

Yeah, you're executing that script once, before the game starts.

You could use a killed event handler. Supposing then that you can count the number of alive cops at the beginning.

Using MPkilled, serverside only, might be a bit easier.

Share this post


Link to post
Share on other sites

If one side consists of the "cops" you could just use something like this, you could run it on the server

if (!isServer) exitWith {};

FNC_UpdateCopCount =
{
private["_iUnit"];
copcount = 0;
for "_i" from 0 to (count playableUnits) do
{
	_iUnit = playableUnits select _i;
	if (side _iUnit == WEST) then
	{
		copcount = copcount + 1;
	};
};
publicVariable "copcount";
};

private["_units"];
while {true} do
{
_units = playableUnits;
[] call FNC_UpdateCopCount;
waitUntil {count _units != count playableUnits};
};

To keep track of the number of "cops" assuming they're on blufor, may be a bit too complicated but it should work anyways.

Share this post


Link to post
Share on other sites

Thanks for the suggestions. Latest code:

//By OneShot.J
_Cops = [cop3,cop4,cop5,cop6,cop7,cop8,cop9,cop10,cop11,cop12,cop13];

{
copcount = copcount + 1;
_x addMPEventHandler ["MPRespawn",{copcount = copcount + 1}];  
_x addMPEventHandler ["MPKilled",{copcount = copcount - 1}];
} forEach _Cops;

publicvariable "copcount";

Value starts at 11, and decreases for every unit killed, but this behavior is only present in the editor. In a multiplayer environment, the variable starts as scaler instead of 0, and any scripts I have that try to retrieve a specific value (2 or 3) still meet the condition.

Edited by eagledude4

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  

×