Page MenuHomeFeedback Tracker

_ctrl buttonSetAction format ["[%1] call My_function;",_string];
Closed, ResolvedPublic

Description

Since you can not pase a _var to a buttonSetAction "[_var] call My_function;" the wiki suggests using a string format workaround to pase _var.
But apparently this only works with Scalar and Arrays not Strings.
This is not essential but really annoying to debug so you might want to mention this on the wiki page.
https://community.bistudio.com/wiki/buttonSetAction

Details

Severity
Feature
Resolution
Not A Bug
Reproducibility
Always
Operating System
Windows 10 x64
Category
Visual-GUI
Steps To Reproduce

Runn this in Debug

[] spawn
{
	disableSerialization;
	private _display = findDisplay 46 createDisplay "RscDisplayEmpty";
	private _ctrlGroup = _display ctrlCreate ["RscControlsGroupNoScrollbars", -1];
	private _ctrlBackground = _display ctrlCreate ["RscTextMulti", -1, _ctrlGroup];
	_ctrlBackground ctrlSetPosition [0, 0, 0.5, 0.5];
	_ctrlBackground ctrlSetBackgroundColor [0.2, 0.2, 0.2, 0.4];
	_ctrlBackground ctrlEnable false;
	_ctrlBackground ctrlCommit 0;
	private _button1 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button1 ctrlSetText "B1 hint Scalar";
	private _button2 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button2 ctrlSetText "B2 hint String";
	private _button3 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button3 ctrlSetText "B3 Fnc Scalar";
	private _button4 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button4 ctrlSetText "B4 Fnc String(ERROR)";
	private _button5 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button5 ctrlSetText "B5 Fnc Array [Scalar,String]";
	private _button6 = _display ctrlCreate ["RscButton", -1, _ctrlGroup];
	_button6 ctrlSetText "B6 Fnc Array [Scalar,String] select 1";
	_button1 ctrlSetPosition [0.005,	0.01,		0.24,	0.04];
	_button2 ctrlSetPosition [0.255,	0.01,		0.24,	0.04];
	_button3 ctrlSetPosition [0.005,	0.06,		0.24,	0.04];
	_button4 ctrlSetPosition [0.255,	0.06,		0.24,	0.04];
	_button5 ctrlSetPosition [0.005,	0.11,		0.24,	0.04];
	_button6 ctrlSetPosition [0.255,	0.11,		0.24,	0.04];
	_ctrlGroup ctrlSetPosition [0.25, 0.25,	0.5,	0.5];
	{_x ctrlCommit 0; } forEach [_button1,_button2,_button3,_button4,_button5,_button6,_ctrlGroup];

	_scalar = 1111;
	_string = "test";
	_array = [_scalar,_string];

	_button1 buttonSetAction format ["hint 'hint Scalar %1'", _scalar];
	_button2 buttonSetAction format ["hint 'hint String %1'", _string];

	My_function3 = {
		hint format["My_function3 Scalar %1",_this select 0];
	};
	_button3 buttonSetAction format ["[%1] call My_function3;",_scalar];

	My_function4 = {
		hint format["My_function4 String %1",_this select 0];
	};
	_button4 buttonSetAction format ["[%1] call My_function4;",_string];	// Calling a function and parsing a string thrwos Error

	My_function5 = {
		hint format["My_function5 array %1",_this select 0];
	};
	_button5 buttonSetAction format ["[%1] call My_function5;",_array];

	My_function6 = {
		hint format["My_function6 array select 1 %1",_this select 0 select 1];
	};
	_button6 buttonSetAction format ["[%1] call My_function6;",_array];

};

Rpt Error
16:50:14 Error in expression <[test] call My_function4;>
16:50:14 Error position: <test] call My_function4;>
16:50:14 Error Nicht definierte Variable in Ausdruck: test

Additional Information

A Workaround for this is to pase a Array that contains teh string you want to use.
eg.

My_function = {
	hint format["%1",_this select 0 select 0];
};
_array = ["STRING"];
_button buttonSetAction format ["[%1] call My_function;",_array];

Event Timeline

dedmen added a subscriber: dedmen.EditedOct 15 2021, 5:36 PM

It works with strings if you quote them correctly.

format ["[%1] call My_function;","STRING"];
->

[STRING] call My_function;

format ["[%1] call My_function;","""STRING"""];
->

["STRING"] call My_function;

format ["[""%1""] call My_function;","STRING"];
->

["STRING"] call My_function;

format ["['%1'] call My_function;","STRING"];
->

['STRING'] call My_function;

format ["[%1] call My_function;","'STRING'"];
->

['STRING'] call My_function;

format ["[%1] call My_function;", str "STRING"];
->

["STRING"] call My_function;

dedmen closed this task as Resolved.Oct 15 2021, 5:43 PM
dedmen claimed this task.
dedmen changed Resolution from Open to Not A Bug.