Page MenuHomeFeedback Tracker

Private variable in a function is overwritten if a function returns a value with same private variable name.
Closed, ResolvedPublic

Description

Two functions, one being main function, another being a function that returns a value inside main function, if sharing same variable(private) name, the previous value is getting overwritten.(Examples of behavior are properly described in Additional Information)

Details

Severity
Major
Resolution
Not A Bug
Reproducibility
Always
Operating System
Windows 7 x64
Category
Engine
Steps To Reproduce
  1. Create a function that returns a value. Mark down the variable.
  2. Create a function that uses previous function created.
  3. Try to use the 1st function's returning variable inside 2nd function, it will be defined and overwriting any value with same name.

(2 Examples are provided in Additional Information)

Additional Information

Example 1:

fnc_getVariable = { 
  _var1=5;
  _var1
};

fnc_mainCode = { 
  _var1 = 3;
  _var2 = [] call fnc_getVariable; 
  _varResult = _var1 + _var2;
  systemChat str _varResult
};
[] call fnc_mainCode; //returns 10 (Expected 3+5 = 8 instead)

What happens in here is that _var2, as expected, getting value 5 by fnc_getVariable, however it also overwrites _var1 because fnc_getVariable returns variable named _var1 , and also overwrites _var1 inside fnc_mainCode.

Example 2:

fnc_getVariable = {
  _var1=5;
  _var1
};

fnc_mainCode = {
  _var1 = null;
  [] call fnc_getVariable;
  hint str _var1;
}; 
[] call fnc_mainCode; // Returns: 5 , Expected: nothing.

I guess this example shows the problem better, I don't know if this is intended behavior or not, but it really looks like it shouldn't be as it is confusing and I personally found this unexpected behavior and fixed my code in a very long period.

I also apologize if category is unfitting, it looked like the most fitting.

Event Timeline

Talya_taya updated the task description. (Show Details)Jul 10 2017, 3:40 AM
JiriWainar added a subscriber: JiriWainar.EditedJul 10 2017, 2:41 PM

The behavior is correct, examples are flawed. The name of returned variable is not important, problem is in how you work with script/variable scopes.

In both examples you define variable _var1 in fnc_mainCode and then you overwrite it in fnc_getVariable with new value.

If you want the desired effect, you need to use keyword private to define the variable _var1 in the fnc_getVariable. This way the brand new variable _var1 (same name, but that's fine) will be created inside the fnc_getVariable scope and it's value will not interfere with the value of variable _var1 in the fnc_mainCode scope.

This will fix both issues.

fnc_getVariable = {
  private _var1=5;
  _var1
};

Note: What's the _var1 = null;? Nothing like null exists in Arma 3 API, the definition doesn't make sense and generates script error. If you want to create a new local variable without value, use:

private "_var1";

or

private ["_var1","_var2","_myAwesomeVariableWithoutValue"];
razazel closed this task as Resolved.Jul 10 2017, 2:53 PM
razazel changed Resolution from Open to Not A Bug.