Home   Help Search Login Register  

Author Topic: "sleep" within "For"  (Read 2708 times)

0 Members and 1 Guest are viewing this topic.

Offline Ferdin

  • Members
  • *
"sleep" within "For"
« on: 17 May 2009, 10:33:45 »
hi all,i'm confusing with follow:
Code: [Select]
private ["_i"];

for [{_i=0},{_i<100},{_i=_i+1}] do {
ctrlSetText [100,format["%1",_i]];
_i = _i - 1;
sleep 1;
};

seems "sleep" doesn't work well;
myDialog just type "100"...
can't i use sleep in "for[]do{}"?
thanks for your view :)

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: "sleep" within "For"
« Reply #1 on: 17 May 2009, 12:28:28 »
Then the "100" is hardcoded somewhere else since in your loop _i will will always be "0".
try { return true; } finally { return false; }

Offline Ferdin

  • Members
  • *
Re: "sleep" within "For"
« Reply #2 on: 18 May 2009, 15:00:23 »
thanks a lot ;)
i'm a beginner :D

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: "sleep" within "For"
« Reply #3 on: 31 May 2009, 06:20:23 »
Code: [Select]
private ["_i"];

for [{_i=0},{_i<100},{_i=_i+1}] do {
ctrlSetText [100,format["%1",_i]];
_i = _i - 1;
sleep 1;
};

The string in "For" {_i=_i+1} increases your zero by 1 while "_i=_i -1;" reduces by 1.
So 0 + 1 - 1 = 0.
Change the above to:

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

for [{_i=0},{_i<100},{_i=_i-1}] do {
ctrlSetText [100,format["%1",_i]];
sleep 1;
};

and it will start at 0 and go down by 1 each second.

if you want it to start at 100 and go down replace the {_i=0} with {_i=100};

Hope that helps.

Luke
Pesky Human!!
Wort Wort Wort.

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: "sleep" within "For"
« Reply #4 on: 31 May 2009, 07:37:52 »
Code: [Select]
private ["_i"];

for [{_i=0},{_i<100},{_i=_i-1}] do {
ctrlSetText [100,format["%1",_i]];
sleep 1;
};

and it will start at 0 and go down by 1 each second.

Therefore _i will never be greater than hundred and the loop will run forever. Without the sleep this would freeze your ArmA.
try { return true; } finally { return false; }

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: "sleep" within "For"
« Reply #5 on: 31 May 2009, 21:01:04 »
Without the sleep this would freeze your ArmA.

This is something to avoid, and makes the sleep necessary.
Alternatively, you could remove it to punish those you hate,
but I believe that violates certain humanitarian conventions.

Luke
Pesky Human!!
Wort Wort Wort.

Offline Denisko-Redisko

  • Members
  • *
Re: "sleep" within "For"
« Reply #6 on: 01 Jun 2009, 23:22:10 »
There are more convenient and obvious way to create a fixed-iteration-loop:
Code: [Select]
for "_i" from 0 to 99 do {
    // something
};
for "_i" from 1 to 100 do {
    // something
};
for "_i" from 100 to 1 step -1 do {
    // something
};
I like this method for cleaner code.
« Last Edit: 02 Jun 2009, 00:28:36 by Denisko-Redisko »
sorry for my english

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: "sleep" within "For"
« Reply #7 on: 03 Jun 2009, 01:24:05 »
Yeah, I'm a big fan of that for. Also note that the iterator, in this case _i, is automatically made private inside the loop (it does't even exist outside it). Saves you worrying about privating it.
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)