Page MenuHomeFeedback Tracker

Possible variable leak from a function
Closed, ResolvedPublic

Description

I have two functions used in hiring soldier procedure: one for finding an equipment set in a virtual storage, and one for "compiling" the soldier.
I've noticed that some of hired soldiers had no ammo, while I was sure the ammo is in storage.
After some test I firured it out: both funtions have the same variable name inside its scope, and these variables are not private.
But shouldn't different functions have different scopes?

Details

Severity
None
Resolution
Not A Bug
Reproducibility
Always
Operating System
Windows 7 x64
Category
Engine
Steps To Reproduce
fnc_getitems = {
    _magstoremove = [123];
    [_magstoremove] 
}; 

fnc_check = {
    _set = ["",0,[]]; 
    _magstoremove = _set#2;  
    _magstoremove
};

call fnc_check;

this one returns [] (empty array);

fnc_getitems = {
    _magstoremove = [123];
    [_magstoremove] 
}; 

fnc_check = {
    _set = ["",0,[]]; 
    _magstoremove = _set#2;  
    call fnc_getitems; 
    _magstoremove
};

call fnc_check;

In this case [123] returned.
I suppose it should not happens?

Event Timeline

MaxP created this task.Oct 3 2023, 6:39 PM
MaxP updated the task description. (Show Details)Oct 3 2023, 6:46 PM
Leopard20 closed this task as Resolved.EditedOct 3 2023, 7:16 PM
Leopard20 changed Resolution from Open to No Bug.
Leopard20 added a subscriber: Leopard20.

Not a bug. Read this:
https://community.bistudio.com/wiki/Variables
TL;DR, prefix your local variable initializations with private to prevent what you describe as "leak" (which is actually a feature in SQF)

fnc_getitems = {
    private _magstoremove = [123];
    [_magstoremove] 
}; 

fnc_check = {
    private _set = ["",0,[]]; 
    private _magstoremove = _set#2;  
    call fnc_getitems; 
    _magstoremove
};

call fnc_check;
Leopard20 changed Resolution from No Bug to Not A Bug.Oct 3 2023, 7:24 PM
Leopard20 removed a subscriber: Leopard20.
MaxP added a comment.EditedOct 3 2023, 7:33 PM

P.S. Ah, ok, this is how SQF realy works :)
I need to live with it now.