Home   Help Search Login Register  

Author Topic: Scope Issues [SOLVED]  (Read 1432 times)

0 Members and 1 Guest are viewing this topic.

Offline Rommel92

  • Members
  • *
Scope Issues [SOLVED]
« on: 19 Jun 2008, 01:13:47 »
Now compared to my last thread, for something less trivial (hopefully...)  :P

Code: [Select]
private ["_index"];

_index = switch (startSide) do
{
case WEST: {_index = 0; hint format ["%1", _index]};
case EAST: {_index = 1; hint format ["%1", _index]};
};

hint format ["%1", _index];

_Index Returns <NULL> in the last hint, however it returns the assigned numb
er within the Switch scope. I was aware that the private at the beginning would fix this issue by making the variable applicable to all scopes?

Rommz,

EDIT:

Whilst I know that an If command works just as well (and actually works), I am still interested in an answer for this.
Code: [Select]
_index = switch (startSide) do
{
case WEST: {_index = 0; hint format ["%1", _index]};
case EAST: {_index = 1; hint format ["%1", _index]};
};
if (startSide == WEST) then
{
_index = 3;
};
_Index returns 3;

SOLVED on BIS Forums:
Code: [Select]
private ["_index"];

_index = (
   switch (startSide) do {
       case WEST: {0};
       case EAST: {1};
   }
);

hint format ["%1", _index];
Fixed by Xeno, I hadn't realised I'd actually done: _Index = _Index = 0;

 :whistle:
« Last Edit: 19 Jun 2008, 01:38:48 by Rommel92 »

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Scope Issues
« Reply #1 on: 19 Jun 2008, 01:40:51 »
The returned value of a switch or if statement is the value returned by the case block actually run. A block, {}, returns the value manually returned by use of the if-exitWith command, else returns the value returned by the last statement in the block, else nil. In this case, you are setting _index again to the return value from the inner hints. If the hints weren't inside the case blocks, the code actually would still work, since "{_index = 0}" returns 0, just like "{0}". This is because the command, hint, always returns nil, which is then passed back to the switch, which then overwrites the valid value of _index and leads the last hint to show a bad value.

What I think you want to do is:
Code: [Select]
private ["_index"]; // Scope _index so it will still be visible after the switch.

// No point hinting inside the cases, since the one after the switch will overwrite it.
switch (startSide) do
{
case WEST: { _index = 0 };
case EAST: { _index = 1 };
};

hint format ["%1", _index];  // --> 0 if west, 1 if east.

If you actually do want to use the returned value from the switch to set _index, however:
Code: [Select]
// Don't need to use private here, since _index is created at the same scope as the later hint.
_index = switch (startSide) do
{
case WEST: { 0 }; // This "returns" a value of 0 to the overall switch.
case EAST: { 1 }; // This "returns" a value of 1 to the overall switch.
};

hint format ["%1", _index]; // --> 0 if west, 1 if east.

I do hope that makes some sense. Either set the value in the cases (1st example), or use the cases to hand back the value to the switch itself (2nd example). Definitely don't do both at once ;P
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Trexian

  • Members
  • *
Re: Scope Issues [SOLVED]
« Reply #2 on: 19 Jun 2008, 03:19:54 »
Would that also work if you assigned _index = 9 or something immediately after the private declaration?

My impression was that such an assignment would instantiate the variable outside the case, which would make it available inside and outside.

:thumbs:
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Scope Issues [SOLVED]
« Reply #3 on: 22 Jun 2008, 01:17:58 »
The private command defines the scope of the variable, without assigning a value to it. Assignments (=) may define the scope of a variable, but always set its value. Thus:
Code: (define scope here, masking any variable called _fish that is accessible, but which is in an outer scope. Also set the value to 9) [Select]
private "_fish"
_fish = 9;

Code: (define scope here, masking any variable called _fish in an outer scope. Value is still undefined) [Select]
private "_fish"

Code: (If variable is already defined in an outer scope, set that variable's value to 9 without altering the scope. If the variable is not already defined in an outer scope, then define scope here and assign value) [Select]
_fish = 9;
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Rommel92

  • Members
  • *
Re: Scope Issues [SOLVED]
« Reply #4 on: 23 Jun 2008, 23:41:28 »
From what I know of it:
Code: [Select]

_fish = 10;
if (true) then
{
_fish = 5;
player GlobalChat format ["%1", _Fish];
};
player GlobalChat format ["%1", _Fish];


First message will print: "5", second will print "10". Hope this gives a better idea of scopes.

A private at the start simply "globalises" the variable throughout the entire script, throughout all the scopes (running in the same instance anyhow).

Code: [Select]
if (true) then
{
_fish = 5;
player GlobalChat format ["%1", _Fish];
};
player GlobalChat format ["%1", _Fish];

"5"
"<NULL>"

Code: [Select]
private ["_fish"];

if (true) then
{
_fish = 5;
player GlobalChat format ["%1", _Fish];
};
player GlobalChat format ["%1", _Fish];

"5"
"5"

 :good:

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Scope Issues [SOLVED]
« Reply #5 on: 24 Jun 2008, 01:16:40 »
Your first example is actually wrong, since both messages will show as "5" (there is only one _fish in this script). I think you were wanting to use this example:
Code: [Select]
_fish = 10;
if (true) then
{
        private "_fish"; // Masks the previous version of _fish inside this block.
_fish = 5;
player GlobalChat format ["%1", _fish]; // => 5
};
player GlobalChat format ["%1", _fish]; // => 10

Your other examples are good though (And better examples of how to use scope, rather than my more abstract approach) ;P

[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Trexian

  • Members
  • *
Re: Scope Issues [SOLVED]
« Reply #6 on: 24 Jun 2008, 22:20:12 »
I'm not sure why you would ever want to do this, but conceptually, you could use the same variable (_fish) inside and outside a block, by using the private command inside the loop?
Sic semper tyrannosauro.

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Scope Issues [SOLVED]
« Reply #7 on: 25 Jun 2008, 20:49:32 »
In this case, it would be two completely different variables, that happen to have the same name. Really, there are lots of times when you need to do this, but I can't think of a good example off the top of my head.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)