CBATeam / CBA_A3

Community Base Addons for Arma 3
https://github.com/CBATeam/CBA_A3/wiki
GNU General Public License v2.0
366 stars 148 forks source link

Toggling 2D and 3D on optics doesn't consistently work unless you hit both applicable keybinds at once. #1626

Open PetMudstone opened 9 months ago

PetMudstone commented 9 months ago

Mods (complete and add to the following information):

Description: When attempting to toggle optics state on a CUP optic that supports toggling of 2D and 3D modes, hitting the relevant keybinds will not actually change your optics' state unless you hit both the "Next optics state" and "Prev optics state" keybinds at the same time. Strangely, attempting to toggle 2D scopes normally will keep them 2D but attempting to toggle 3D scopes normally will turn them 2D.

Notably, other optics with different kinds of toggleable states (such as the flippable sight on the M68 CCO + AimM, adjustable reticle on the EKP-8-02 and Kobra, or filter on the 1P63) work completely fine.

Steps to reproduce:

Expected behavior: I expected the various CUP optics to be properly able to switch 2D and 3D modes without hitting both applicable keybinds at once, and for other kinds of adjustable optics to be broken as well.

Where did the issue occur?

Log Files:

Additional context: I should note that I haven't tested other mods with adjustable optics courtesy of CBA. This is simply because I do not know of any other mods with relevant adjustable optics.

Screenshots: Below I demonstrate the inconsistency of toggling 2D and 3D.

https://github.com/CBATeam/CBA_A3/assets/38266309/c2f3dbca-0856-4248-a693-ddd5341a0eed

10Dozen commented 7 months ago

I can reproduce, but don't know why this happens. CUP Weapons didn't update from Dec 2021.

For optics that supports both 2d and 3d MRT Switch scheme is the same bi-directional 2D <-> 3D and one-directional PIP -> 3D (2D switches to PIP variant by CBA logic).

Configs related to M145 scopes (2d (default), PIP and 3D variant):

// 2D variant (default)
class CUP_optic_ElcanM145: ItemCore
{
    scope = 2;
    weaponInfoType = "CBA_ScriptedOptic";
    class CBA_ScriptedOptic
    {
        reticleTexture = "\CUP\Weapons\CUP_Weapons_West_Attachments\Elcan_M145\data\elcan_m145w_reticle_ca.paa";
        reticleTextureNight = "\CUP\Weapons\CUP_Weapons_West_Attachments\Elcan_M145\data\elcan_m145w_reticle_n_ca.paa";
        reticleTextureSize = 1;
        manualReticleNightSwitch = 1;
        bodyTexture = "\CUP\Weapons\CUP_Weapons_West_Attachments\Elcan_M145\data\body\elcan_m145w_body_ca.paa";
        bodyTextureNight = "\CUP\Weapons\CUP_Weapons_West_Attachments\Elcan_M145\data\body\elcan_m145w_body_night_ca.paa";
        bodyTextureSize = 1.95;
    };
    MRT_SwitchItemNextClass = "CUP_optic_ElcanM145_3D";
    MRT_SwitchItemPrevClass = "CUP_optic_ElcanM145_3D";
    MRT_switchItemHintText = "$STR_CUP_dn_OpticsMode_2D";
};

// PIP variant
class CUP_optic_ElcanM145_PIP: CUP_optic_ElcanM145
{
    author = "$STR_CUP_AUTHOR_STRING";
    scope = 1;
    class ItemInfo: ItemInfo
    {
        modelOptics = "\x\cba\addons\optics\cba_optic_big_pip.p3d";
    };
    MRT_SwitchItemNextClass = "CUP_optic_ElcanM145_3D";
    MRT_SwitchItemPrevClass = "CUP_optic_ElcanM145_3D";
    MRT_switchItemHintText = "$STR_CUP_dn_OpticsMode_2D";
};

// 3D variant
class CUP_optic_ElcanM145_3D: ItemCore
{
    author = "$STR_CUP_AUTHOR_STRING";
    dlc = "CUP_Weapons";
    scope = 1;
    inertia = 0.1;
    MRT_SwitchItemNextClass = "CUP_optic_ElcanM145";
    MRT_SwitchItemPrevClass = "CUP_optic_ElcanM145";
    MRT_switchItemHintText = "$STR_CUP_dn_OpticsMode_3D";
};
10Dozen commented 7 months ago

After some testing:

Looks like there was a change in accessory functions in https://github.com/CBATeam/CBA_A3/pull/1595.

Tried older version of the CBA_accessory_fnc_switchAttachment - https://raw.githubusercontent.com/CBATeam/CBA_A3/953cdfe1ff97c17c6e2fbcfa4a98dfaf81fdbf8e/addons/accessory/fnc_switchAttachment.sqf - and it works good, but i can't tell why :)

10Dozen commented 7 months ago

Something is wrong with this part:

https://github.com/CBATeam/CBA_A3/blob/8bb83aa0283200070ff6c3da80a6e22dcf11b9f6/addons/accessory/fnc_switchAttachment.sqf#L55-L65

TBH I don't quite understand what is the purpose of entire loop, but in this specific case the next happens: 1) On first step it finds target class successfully, but as _testItem is not "" - iteration continues

Test item: CUP_optic_ElcanM145_PIP.
Configs found: [bin\config.bin/CfgWeapons/CUP_optic_ElcanM145_PIP/MRT_SwitchItemNextClass].
Configs not empty, test item class: CUP_optic_ElcanM145_3D,
Test against UsageArray passed.
_switchItem = CUP_optic_ElcanM145_3D.

2) Tests found 3D item and successfully finds 'next' class for it. But again - _testItem is not empty and loop continues

Test item: CUP_optic_ElcanM145_3D.
Configs: [bin\config.bin/CfgWeapons/CUP_optic_ElcanM145_3D/MRT_SwitchItemNextClass].
Configs not empty, test item class: CUP_optic_ElcanM145.
Test against UsageArray passed.,
_switchItem = CUP_optic_ElcanM145.

Due to cyclic scheme of MRT Switch used for CUP scopes - loop continues until reaches execution limit (10000 iterations)

3) Loop is terminated. Resulting in _switchItem to be "CUP_optic_ElcanM145". After switching, optics handlers replaces 2d scope to PIP variant. From player's perspective nothing was changed at all - the very same scope reamined.

When user is spamming switch button, i guess, sometimes script starts when 2D variant is added, but not yet replaced with PIP variant by optics. So in this case on loop termination - 3D variant is the last one and switch works.

When PIP optics is disabled, the loop is only 2 step -- on step 1 in 2D class config reference to 3D is found, on step 2 in 3D class config reference to 2D is found, and as reference is equal to original 2d variant class - loop breaks.

Previously no while loop was used, so _switchItem was picked from MRT_SwitchItemNextClass property and no additional checks were made.

@PabstMirror Could you, please, tell - is it possible to fix on CBA side or should CUP use diffrent MRT-switch scheme (and what scheme should be used)?

Mike-MF commented 4 months ago

This is something I think I fixed.

Caused in CUP by having only a next cycle being Regular > 3D > PIP. Would be nice to have some people confirm this is no longer an issue before closing.

10Dozen commented 4 months ago

This is something I think I fixed.

Caused in CUP by having only a next cycle being Regular > 3D > PIP. Would be nice to have some people confirm this is no longer an issue before closing.

Works fine for me.