Skip to content

Instantly share code, notes, and snippets.

@Fusselwurm
Last active April 28, 2022 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fusselwurm/64b0e54778e1c364f85e1b6bbc6e4fd6 to your computer and use it in GitHub Desktop.
Save Fusselwurm/64b0e54778e1c364f85e1b6bbc6e4fd6 to your computer and use it in GitHub Desktop.
`private` is important to use in nested for loops
// not using private for `_i`
_x = "";
for [{_i = 1}, {_i <=2}, {_i = _i + 1}] do {
_x = _x + "o";
for [{_i = 1}, {_i <= 2}, {_i = _i + 1}] do {
_x = _x + "i";
};
};
_x == "oii" // welp. inner loop re-used the outer variable
// do use private to prevent re-using
_x = "";
for [{_i = 1}, {_i <=2}, {_i = _i + 1}] do {
_x = _x + "o";
for [{private _i = 1}, {_i <= 2}, {_i = _i + 1}] do {
_x = _x + "i";
};
};
_x == "oiioii"
// yay.
// HINT functions do inherit the scope from the caller.
// a `for` loop in, say, a CBA event loop may create an _i that you inadvertently re-use.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment