Search code examples
sqf

Arma 3 AddAction does not recognize variable from outside it scope


I am trying to create a billboard with several images that can be toggled via next/previous Actions.
However, the AddAction does not recognize the variables defined outside it scope and throwing the error Error undefined variable in expression: _billboard on pressing the action (next or previous).
I also noticed that even calling [_updateImage] call _updateImage won't work and will throw the same error about not knowing what _updateImage is.

The Init Field of the billboard

_imageArray = ["assets\image1.jpg", "assets\image2.jpg"];
[this, _imageArray] execVM "scripts\initBillboard.sqf";

The scripts\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;
}];

I am looking for a way (if what i am doing in theory is right) to work around and be able to use variables from outside the scope of the addAction inside it.


Solution

  • In Arma 3 scripting, addAction commands have their own scope, which means they cannot directly access variables defined outside of them. However, you can use params to pass the necessary variables into the scope of the addAction. Here's how you can modify your code to achieve this:

    1. Modify the initBillboard.sqf script to accept additional parameters for the addAction commands.
    2. Pass the required variables into the addAction commands using params.

    If you need further help or unable to figure it out, Please let me know.