Jump to content
Vadym Mazur

Problem with getVariable

Recommended Posts

Hello everyone, im trying save player variables but do something wrong.

I want to add a unique variables that will be displayed after the server is restarted

 

initPlayer:

//Add variable if dont exist
if (isNil {profileNamespace getVariable "rankRating"}) then {
	profileNamespace setVariable ["rankRating", 0];
};

draw3D

_player = player;
rankRatingCall = {
	_unit = _this select 0;
	//_rating = score _unit; // <- working
	_rating = (_unit getVariable "rankRating"); // < - dont working
	_Rank = "";
	switch true do {
      case(_rating <= 0) : {_Rank = "PRIVATE";};
      case((_rating >= 1) && (_rating < 100)) : {_Rank = "CORPORAL";};
	}; 
		_rankPicture = "";
	if(_Rank isEqualTo "PRIVATE") then {
		_rankPicture = "";
	} else {
		_rankPicture = format["\A3\Ui_f\data\GUI\Cfg\Ranks\%1_gs.paa",_Rank];
	};
  	_rankPicture
};

//Draw3D
if ('ItemGPS' in (assignedItems _player)) then {
	{
		if (side _x == side player && isPlayer _x && {alive _x}) then {
			_dist = (player distance _x) / 15;
			_color = 	[0.7,0.5,0,1];
				if (cursorTarget != _x) then {
					_color set [3, 1 - _dist]
				};
				drawIcon3D [
				[_x] call rankRatingCall,
				[0,125,255,([1,0.5] select ((getPlayerChannel _x) isEqualTo -1))],
				(if (isNull (objectParent _x)) then {(_x modelToWorldVisual (_x selectionPosition 'Spine3'))} else {((objectParent _x) modelToWorldVisual [0,0,0])}),
				if(_x distance player < 500) then {0.3} else { 0.0},
				if(_x distance player < 500) then {0.3} else { 0.0},
				0,
				'',
				2,
				0.03,
				'PuristaMedium'
				];
		};
	} foreach allunits;
};

       

 

Share this post


Link to post
Share on other sites

@Vadym Mazur, you set rankRating variable in mission namespace, but get it's value from unit's namespace. Set value in unit's namespace, and, if it's multiplayer mission, make it public.

Share this post


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

@Vadym Mazur, you set rankRating variable in mission namespace, but get it's value from unit's namespace. Set value in unit's namespace, and, if it's multiplayer mission, make it public.

if (isNil {player getVariable "rankRating"}) then 
{ 
	player setVariable ["rankRating", 0,true]; 
};

Something like this?

Share this post


Link to post
Share on other sites
1 minute ago, Vadym Mazur said:

if (isNil {player getVariable "rankRating"}) then 
{ 
	player setVariable ["rankRating", 0,true]; 
};

Something like this?

Yes.

Share this post


Link to post
Share on other sites
13 minutes ago, Schatten said:

Yes.

if (isNil {player getVariable "rankRating"}) then   
 {   
  player setVariable ["rankRating", 0,true];   
 };  
_rating = player getVariable ["rankRating"]; 
_text = format ['%1',_rating ];  
_text remoteExec ['systemChat',-2,FALSE]; 


 

Return <null>

Share this post


Link to post
Share on other sites
32 minutes ago, Vadym Mazur said:

if (isNil {player getVariable "rankRating"}) then   
 {   
  player setVariable ["rankRating", 0,true];   
 };  
_rating = player getVariable ["rankRating"]; 
_text = format ['%1',_rating ];  
_text remoteExec ['systemChat',-2,FALSE]; 


 

Return <null>

Where this code is placed?

Share this post


Link to post
Share on other sites
3 minutes ago, Schatten said:

Where this code is placed?

Now i am trying to test this in console

Share this post


Link to post
Share on other sites
17 minutes ago, Vadym Mazur said:

Now i am trying to test this in console

Your code work's fine for me, but returns empty string, because the last line returns it (_text remoteExec ['systemChat',-2,FALSE];). It also won't systemChat of player's rankRating, because systemChat will be executed on each client, but not the server (in singleplayer or hosted game admin's computer is server).

Share this post


Link to post
Share on other sites
32 minutes ago, Schatten said:

Your code work's fine for me, but returns empty string, because the last line returns it (_text remoteExec ['systemChat',-2,FALSE];). It also won't systemChat of player's rankRating, because systemChat will be executed on each client, but not the server (in singleplayer or hosted game admin's computer is server).

Yes, my fail, its working in Editor

if (isNil {player getVariable "rankRating"}) then { player setVariable ["rankRating", 0,true]; };
_rating = player getVariable "rankRating"; 
hint format ["%1", _rating ];

But still cant get this value in rankRatingCall

 

rankRatingCall = { 
_unit = _this select 0;
_rating = _unit getVariable "rankRating";
	switch true do {
      case(_rating <= 0) : {_Rank = "PRIVATE";};
      case((_rating >= 1) && (_rating < 100)) : {_Rank = "CORPORAL";};
	};
}

UPDATE: Problem solved

Share this post


Link to post
Share on other sites

One more question. 

How can I save player's variables? Because after restarting the server, the player's variables disappear.

Thanks in advance

 

Share this post


Link to post
Share on other sites
1 hour ago, Schatten said:

You can use either database or profile namespace (profileNamespace, missionProfileNamespace).

if (isNil {profileNamespace  getVariable "rankRatingClient"}) then { 
		profileNamespace setVariable["rankRatingClient", 0]; 
};

if (isNil {player getVariable "rankRatingServer"}) then { 
		player setVariable ["rankRatingServer", 0,true]; 
	} else {
		_rating = profileNamespace getVariable "rankRatingClient";
		player setVariable ["rankRatingServer", _rating,true];
};	

Will this be the right decision?

Share this post


Link to post
Share on other sites
6 hours ago, Vadym Mazur said:

Will this be the right decision?

Maybe, it depends on what you want.

  • Like 1

Share this post


Link to post
Share on other sites

Remember that getVariable has an alternate syntax that allows you to specify a default value if it is not found

_rating = player getVariable ["rankRating", 0]; 

In my experience, assuming the variable exists often quickly leads to a script error, the alt syntax ensures that you will always get a value and hopefully avoid any script errors. It will remove the need to check with isNil

  • Like 1

Share this post


Link to post
Share on other sites
8 hours ago, dreadedentity said:

Remember that getVariable has an alternate syntax that allows you to specify a default value if it is not found


_rating = player getVariable ["rankRating", 0]; 

In my experience, assuming the variable exists often quickly leads to a script error, the alt syntax ensures that you will always get a value and hopefully avoid any script errors. It will remove the need to check with isNil

isNil is fine here, for initializing a variable. The default value for getter is useful later, as you said, in scripts, when checking conditions.

 

if (isNil {profileNamespace getVariable "rankRatingClient"}) then { profileNamespace setVariable["rankRatingClient", 0]; };

 player setVariable ["rankRatingServer",profileNameSpace getVariable ["rankRatingClient",0],true];

 

NOTE: It's weird to save this variable on client side: open cheating. Should be saved server side (which has also a profileNameSpace), even a serverNameSpace.

something like, on server:
 

addMissionEventHandler ["PlayerConnected", {
  params ["_id", "_uid", "_name", "_jip", "_owner", "_idstr"];
  if (isNil {serverNameSpace getVariable ("rankRatingClient"+ _uid)}) then {
    (_uid call BIS_fnc_getUnitByUID) setVariable ["rankRatingClient",0,TRUE];
  };
}];

Use also PlayerDisconnected MEH for saving this variable on server.

NOT TESTED, sorry, and must be continued by what you need exactly.

 

  • Like 2

Share this post


Link to post
Share on other sites
7 hours ago, pierremgi said:

isNil is fine here, for initializing a variable.

Why even check via isNil when you can just return a default value with getVariable if its nil.

This...

if (isNil {profileNamespace getVariable "rankRating"}) then { 
	player setVariable ["rankRating", 0, true]; 
} else {
	_rating = profileNamespace getVariable "rankRating";
	player setVariable ["rankRating", _rating, true ];
};	

...just becomes...

player setVariable[ "rankRating", profileNamespace getVariable[ "rankRating", 0 ], true ];

You only ask for the value from profileNamespace once rather than twice( if not nil ).

  • Like 1

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

×