我正在尝试创建一个包含多个图像的广告牌,这些图像可以通过下一个/上一个操作进行切换。
但是,AddAction 无法识别在其范围之外定义的变量,并在按下操作(下一个或上一个)时抛出错误
Error undefined variable in expression: _billboard
。[_updateImage] call _updateImage
也不起作用,并且会抛出相同的错误,因为不知道 _updateImage
是什么。
广告牌的Init Field
_imageArray = ["assets\image1.jpg", "assets\image2.jpg"];
[this, _imageArray] execVM "scripts\initBillboard.sqf";
脚本\initBillboard.sqf
params [
["_billboard", objNull, [objNull]],
["_images", [], [[]]]
];
_billboard setVariable ["_currentIndex", 0, true];
//This is working and it is hinting to 0!
hint format ["Current index: %1", _billboard getVariable "_currentIndex"];
private _updateImage = {
private _currentIndex = _billboard getVariable "_currentIndex";
_billboard setObjectTexture [0, (_images select _currentIndex)];
};
// Initial display
// This is also working and setting the image to the first in the array
[_updateImage] call _updateImage;
_billboard addAction ["Next", {
private _currentIndex = _billboard getVariable "_currentIndex";
_currentIndex = _currentIndex + 1;
if (_currentIndex >= (count _images)) then {
_currentIndex = 0;
};
_billboard setVariable ["_currentIndex", _currentIndex, true];
[_updateImage] call _updateImage;
}];
_billboard addAction ["Previous", {
private _currentIndex = _billboard getVariable "_currentIndex";
_currentIndex = _currentIndex - 1;
if (_currentIndex < 0) then {
_currentIndex = (count _images) - 1;
};
_billboard setVariable ["_currentIndex", _currentIndex, true];
[_updateImage] call _updateImage;
}];
我正在寻找一种方法(如果我在理论上所做的事情是正确的)来解决并能够使用 addAction 内部范围之外的变量。
在 Arma 3 脚本中,addAction 命令有自己的作用域,这意味着它们无法直接访问在其外部定义的变量。但是,您可以使用 params 将必要的变量传递到 addAction 的范围中。您可以通过以下方式修改代码来实现此目的:
如果您需要进一步帮助或无法弄清楚,请告诉我。