Arma 3 AddAction 无法识别其范围之外的变量

问题描述 投票:0回答:1

我正在尝试创建一个包含多个图像的广告牌,这些图像可以通过下一个/上一个操作进行切换。
但是,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 内部范围之外的变量。

sqf
1个回答
0
投票

在 Arma 3 脚本中,addAction 命令有自己的作用域,这意味着它们无法直接访问在其外部定义的变量。但是,您可以使用 params 将必要的变量传递到 addAction 的范围中。您可以通过以下方式修改代码来实现此目的:

  1. 修改 initBillboard.sqf 脚本以接受 addAction 命令的附加参数。
  2. 使用 params 将所需的变量传递到 addAction 命令中。

如果您需要进一步帮助或无法弄清楚,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.