Jump to content
Sign in to follow this  
metaoin

Problem with Variabe scope

Recommended Posts

Hello,

i am fairly new to scripting in arma and i ran into a problem. I think it has something to do with scopes.

for "_i" from 0 to _n do{
_pb = _paths select _i select count _paths select _i - 1;
if ([getPos _pb,getPos _end] call BIS_fnc_areEqual) then {
           ...
       };
};

I get an error that _pb is not defined at getPos _pb. Why is that and how can i fix it?

Share this post


Link to post
Share on other sites

Well, first of all, you should declare your (local) variable outside of loops. This will (in most cases) solve scope problems and has a minor speed benefit.

Furthermore, you should try to format your code, as it always will not only look better, but prevent errors, like the one you're having.

If you have nested commands, you should try to wrap the commands/parameters into paranthesis - the problem, if there is any, will be visible that way.

_pb = objNull;
for "_i" from 0 to _n do
{
_pb = [color="#0000FF"]_paths select ([/color] [color="#008000"]_i select ([/color] [color="#FF0000"][b](count _paths)[/b][/color] [color="#DAA520"]select  (_i - 1)[/color] [color="#008000"])[/color] [color="#0000FF"])[/color];
if ([getPos _pb,getPos _end] call BIS_fnc_areEqual) then {
           ...
       };
};

The red bit of code is (most likely) the culprit here: select is a command to get the n'th element out of an array. count is a command to get the number of elements in an array.

So in the end, you're trying to get the (_i - 1)'th element from a number, which won't work obviously (integer != array). The same thing with the select before it.

I think it will be more helpful if you could enlighten us in your intent, so we could figure out how the code would have to look like :)

Share this post


Link to post
Share on other sites

Thanks, paths is a array of arrays so it should work. The code worked until i changed it so that it would be spawned. If it helps i will post more of the code.

Share this post


Link to post
Share on other sites

Even if _paths is a multi-dimensional array, you can't "select" an integer, what you actually do, twice.

What you actually wanted is something like this:

[color="#A52A2A"]([/color] [color="#0000FF"]([/color] [color="#008000"](_paths select ( INDEX_DIMENSION_1) )[/color] [color="#0000FF"]select ( INDEX_DIMENSION_2) )[/color] ... [color="#A52A2A"]select ( INDEX_DIMENSION_N ) )[/color]

The first block returns an array, so you can use a subsequent select, to return another array in the second block.

You can keep using selects as long as you've fetched an array in the select before.

Edited by XxAnimusxX

Share this post


Link to post
Share on other sites

okay i could fix the problem by adding correct paranthesis.

_pb = (_paths select _i) select (count (_paths select _i) - 1);

By the way is there a better way to get the last element of an array then the one above ?

Thanks for the fast response.

Share this post


Link to post
Share on other sites

Nah, not really, to get the last element out of an array, you have to select using the count of the array minus one - what you actually did :D

The only way to "fasten up" things would be storing (_paths select _i) into another local variable, so you don't have to "select" it several times (an existing reference to an array-element is faster than trying to get the array-element several times).

From the coding angle, you should always prevent code redundancy as it will always bite you back when your code gets more complex.

Everytime you see repeating code-lines , you should try to enclose them into a seperate function and for repeating blocks of code (like the meantioned above), store them into local variables.

End of lesson.

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  

×