#Help me understand scope please?

1 messages · Page 1 of 1 (latest)

alpine anchor
#

So I have a function here that is broken.

hollow gate
#

You didn't show anything

alpine anchor
#

word limit. un momento
Edit: So this code has a scope issue when calling calling itself in the last if block. The solution is to private the two variables _newItemContainers and _oldItemContainers.

with the solution, no privated variables:
We have a root container, with a subcontainer. The subcontainer has no subcontainers.
When recursively calling...

#
params ["_newContainer", "_oldContainer"];

// Items
(getItemCargo _oldContainer) params ["_itemList", "_itemCount"];
{
    _item = _x;
    _count = _itemCount select _forEachIndex;
    _newContainer addItemCargoGlobal [_item, _count];
} forEach _itemList;

// Backpacks
(getBackpackCargo _oldContainer) params ["_itemList", "_itemCount"];
{
    _item = _x;
    _count = _itemCount select _forEachIndex;
    _newContainer addBackpackCargoGlobal [_item, _count];
} forEach _itemList;

// Recursion shouldn't be much of an issue, ...
_oldItemContainers = everyContainer _oldContainer;
_newItemContainers = everyContainer _newContainer;

// Solution:
private ["_oldItemContainers", "_newItemContainers"];

if (count _oldItemContainers > 0) then {
    _oldItemContainers = [_oldItemContainers, [], { _x select 0 }] call BIS_fnc_sortBy;
    _newItemContainers = [_newItemContainers, [], { _x select 0 }] call BIS_fnc_sortBy;
    for "_i" from 0 to (count _oldItemContainers - 1) do {
        _oldItemContainer = _oldItemContainers select _i select 1;
        _newItemContainer = _newItemContainers select _i select 1;
        [_newItemContainer, _oldItemContainer] call bax_campaignBags_fnc_transferCargo;
    };
};
hollow gate
#

Okay, but what is your issue / question

#

private ["_oldItemContainers", "_newItemContainers"];
You're trying to define variables a second time after you define them

alpine anchor
#

So this function is recursively called.
local variables are not privated by the individual call scope then?

hollow gate
#

I'm not really sure what you're trying to ask

alpine anchor
#

i understand now.

#

nevermind.

hollow gate
#

The _xItemContainers variables will be re-defined when the function is called again anyway

alpine anchor
#

yes AND no

#

idk

#

🙃

hollow gate
#

No, they will be redefined

alpine anchor
#

Haha...

#

yes. you are right. I think i completely understand what was happening now.