Jump to content
tom.machu

setVariable on ctrlDisplay

Recommended Posts

Hi guys,

 

can I set setVariable on displayCtrl and retrieve it in another script?

The goal is to set the angle of the (rounded) button and save its position for every time I open dthe dialog?

 

 

zChanged.sqf (executed from controls.hpp - onMouseZChanged="_this execVM 'zChanged.sqf'"😉

params ["_displayOrControl", "_scroll"];

private _ctrl = findDisplay 23 displayCtrl 1400;
private _getDir = ctrlAngle _ctrl;
private _dir = _getDir select 0;

if (_scroll > 0) then {
  _ctrl ctrlSetAngle [round _dir + 1, 0.5, 0.5];
  //hint str (round _dir + 1);
  _dirScrl = (round _dir + 1);
  _ctrl setVariable ["dir", _dirScrl];
};

if (_scroll < 0) then {
  _ctrl ctrlSetAngle [round _dir - 1, 0.5, 0.5];
  //hint str (round _dir - 1);
  _dirScrl = (round _dir - 1);
  _ctrl setVariable ["dir", _dirScrl];
};

initPlayerLocal.sqf

player addAction 
[
	"Open Dialog", 
	{
    	createDialog "radioDiag";
		private _ctrl = findDisplay 23 displayCtrl 1400;
  		private _dirScrl = _ctrl getVariable "dir";
  		private _ctrl ctrlSetAngle [round _dirScrl, 0.5, 0.5];
  		hint str _dirScrl;
	}
];

 

Share this post


Link to post
Share on other sites

yes sure that should work. why, doesn't your code work?

 

you may need to use the default parameter for getVariable "dir" in case it's not set anywhere

 

private _dirScrl = _ctrl getVariable ["dir",0];

 

 

Share this post


Link to post
Share on other sites
4 hours ago, gc8 said:

yes sure that should work. why, doesn't your code work?

 

 

If I change _dirScrl to global variable dirScrl in both .sqf, then it works. Is it okay?

player addAction 
[
	"Open Dialog", 
	{
		
    	createDialog "radioDiag";
		if (isNil "dirScrl") exitWith {};
		private _ctrl = findDisplay 23 displayCtrl 1400;
  		private _dirScrl = _ctrl getVariable ["dir", dirScrl];
  		private _ctrl ctrlSetAngle [round dirScrl, 0.5, 0.5];
  		hint str dirScrl;
	}
];

 

 

 

Share this post


Link to post
Share on other sites

Yes you can:

https://community.bistudio.com/wiki/setVariable

 

Don't forget to use disableSerialization

 

if (isNil "dirScrl") exitWith {}; // What for???  you don't need that. And global dirScrl and local _dirScrl variables are not same. Keep local one everywhere as you defined it findDisplay 23 displayCtrl 1400 after disableSerialization, or just write (findDisplay 23 displayCtrl 1400) everywhere. why not.

 

findDisplay 23 ? your own display? Add waitUntil {!isNull findDisplay 23}; at the beginning of your addAction code (just after creating "radioDiag" if it refers to this dialog).

 

Share this post


Link to post
Share on other sites
1 hour ago, tom.machu said:

 

If I change _dirScrl to global variable dirScrl in both .sqf, then it works. Is it okay?


player addAction 
[
	"Open Dialog", 
	{
		
    	createDialog "radioDiag";
		if (isNil "dirScrl") exitWith {};
		private _ctrl = findDisplay 23 displayCtrl 1400;
  		private _dirScrl = _ctrl getVariable ["dir", dirScrl];
  		private _ctrl ctrlSetAngle [round dirScrl, 0.5, 0.5];
  		hint str dirScrl;
	}
];

 

 

 

 

 

not sure if i follow but if you change it to global variable you don't really need setVariable anymore

Share this post


Link to post
Share on other sites
25 minutes ago, pierremgi said:

And global dirScrl and local _dirScrl variables are not same. Keep local one everywhere as you defined it findDisplay 23 displayCtrl 1400 after disableSerialization

 

 

Thank you. But how do I get the position of the button in addAction with local variables, when the position was defined by zChanged.sqf. It stays on its default (0 degrees) every time, I open the dialog unless I use the global variable 🙂

params ["_displayOrControl", "_scroll"];
disableSerialization;
private _ctrl = findDisplay 23 displayCtrl 1400;
private _getDir = ctrlAngle _ctrl;
private _dir = _getDir select 0;

if (_scroll > 0) then {
  _ctrl ctrlSetAngle [round _dir + 1, 0.5, 0.5];
  private _dirScrl = (round _dir + 1);
  _ctrl setVariable ["dir", _dirScrl];
};

if (_scroll < 0) then {
_ctrl ctrlSetAngle [round _dir - 1, 0.5, 0.5];
private _dirScrl = (round _dir - 1);
_ctrl setVariable ["dir", _dirScrl];
};                

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

Without your full code, I can't guess what is your display and how do you actualize your variable _scroll, to say the least.

Share this post


Link to post
Share on other sites

I think displayCtrl can't preserve the local variable  from setVariable and lose it once the main display is shut down (similarly like when an object is deleted, you can't get getVariable anymore). I used misionNamespace instead and it works.

 

player addAction ["Open Dialog", 
	{
		createDialog "radioDiag";
		{!isNull findDisplay 23};
		private _ctrl = findDisplay 23 displayCtrl 1400;
		private	_dirScrl = missionNamespace getVariable ["dir",0];
		_ctrl ctrlSetAngle [round _dirScrl, 0.5, 0.5];
		hint str _dirScrl;
	}
];
params ["_displayOrControl", "_scroll"];

disableSerialization;
private _ctrl = findDisplay 23 displayCtrl 1400;
private _getDir = ctrlAngle _ctrl;
private _dir = _getDir select 0;

if (_scroll > 0) then 
{
	private _dirScrl = (round _dir + 1);
	_ctrl ctrlSetAngle [_dirScrl, 0.5, 0.5];
	missionNamespace setVariable ["dir", _dirScrl];
};

if (_scroll < 0) then 
{
	private _dirScrl = (round _dir - 1);
	_ctrl ctrlSetAngle [_dirScrl, 0.5, 0.5];
	missionNamespace setVariable ["dir", _dirScrl];
};

 

Share this post


Link to post
Share on other sites
16 hours ago, tom.machu said:

I think displayCtrl can't preserve the local variable  from setVariable and lose it once the main display is shut down

Yes, variable space of the control is lost when the display is closed.

 

16 hours ago, tom.machu said:

I used misionNamespace instead and it works

MissionNamespace and global variables are the same things.

missionNamespace setVariable[ "test", "hello" ];
hint test; //hints hello

If you want it to persist over game restarts use profileNamespace, if you want it to persist for a game session use UINamespace, if you want it to persist for a mission use localNamespace.

Namespaces

  • 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

×