Jump to content
Sign in to follow this  
Rommel

Scope Issues

Recommended Posts

Now compared to my last thread on OFPEC, for something less trivial (hopefully...)

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

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 number 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,

smile_o.gif

EDIT:

Whilst I know that an If command works just as well (and actually works), I am still interested in an answer for this.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_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; tounge2.gif

Share this post


Link to post
Share on other sites

This is the way to go smile_o.gif:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_index"];

startSide = west;

_index = (

   switch (startSide) do {

       case WEST: {0};

       case EAST: {1};

   }

);

hint format ["%1", _index];

The following will work too:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_index"];

startSide = west;

_index = (if (startSide == west) then {0} else {1});

hint format ["%1", _index];

Xeno

Share this post


Link to post
Share on other sites

Haha thanks mate, I just edited above post to say I knew that an IF would work, but wasn't sure about why the Switch wouldnt.

Thanks thumbs-up.gif .

Share this post


Link to post
Share on other sites
Haha thanks mate, I just edited above post to say I knew that an IF would work, but wasn't sure about why the Switch wouldnt.

Thanks thumbs-up.gif .

The switch is working correctly. The switch returns the results of the very last command processed. In your example, the last command processed is:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">hint format ["%1", _index]

Obviously, the 'hint' command doesn't return anything, which is why _index is being set to null after the switch.

Share this post


Link to post
Share on other sites

Yeh thanks mate, Spooner just made me aware of this, I keep forgetting how many people check these forums (both BIS and OFPEC) per hour, I think i'll just keep it localized to one of em unless it gets really hairy.

tounge2.gif

Thanks all. Problems solved.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×