Home   Help Search Login Register  

Author Topic: Nested while loops?  (Read 1650 times)

0 Members and 1 Guest are viewing this topic.

Offline KaRRiLLioN

  • Members
  • *
  • Grits -n- Peanut Butter are a wonderful thing
    • OFP RTS-3 Real Time War
Nested while loops?
« on: 26 Jan 2011, 23:05:36 »
I'm having issues with a function with a nested while loop in it.  It's like the function just dies at the nested loop.  Do nested While loops work in Arma 2?

Perhaps I'm making a very elementary mistake that I'm not seeing.  The nested while loop is while {_s < _ss}.  I've seen some other odd stuff in Arma scripting like local variables in nested if statements that don't get passed to the main if statement and such, so maybe the nested while statement is another such quirk.  In which case I'm not sure what to do to achieve the required results.

Thanks!

Here's the sample code:
Quote
if (_class in [512,768,1536]) then
   {
      _set = FALSE;
      _mslots = _class/256;
      if (karr_moreAmmo) then {_mslots = 2};
      _c = count karr_mags0;
      _slotsOpen = 12 - (count (karr_mags0 - ["EMPTY"]));
      _checkSlots = _slotsOpen - _mSlots;
      _i = 0;
      _picID = IDMagSlot1;
      _bkgID = IDBkgMagSlot1;
      player sidechat format ["LARGE MAG: Open %1, MSlots %2, Avail: %3",_slotsOpen,_mslots,_checkSlots];

      while {_i < _c && !_set && _checkSlots >= 0} do

         {
            if (karr_mags0 select _i == "Empty") then
               {
                  _s = _i;
                  _ss = _i + _mSlots;
                  _u = [];
                  player groupChat format ["FIRST: _s %1, _mSlots %2, _u %3, _ss %4",_s,_mSlots,_u,_ss];
                  player groupchat format ["%1",_s < _ss];

                  while {_s < _ss} do
                     {
                        _u = _u + (karr_mags0 select _s);
                        _s = _s + 1;
                        player groupChat format ["SECOND: _u %1",_u];
                     };

                  player sideChat format ["THIRD: _u %1",_u];
                  player groupChat format ["%1, type %2, slots %3, pic %4",_i,_type,_mSlots,_pic];
                        
               };
            _i = _i + 1;
            _picID = _picID + 1;
            _bkgID = _bkgID + 1;
         };

   };

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Nested while loops?
« Reply #1 on: 27 Jan 2011, 09:49:39 »
I think you need to put EVERY LOCAL variables in an private array at the top.
Example :

private ["_c","_picID","_bkgID","_i","_s","_ss"]
if (_class in [512,768,1536]) then
   {
      _set = FALSE;
      _mslots = _class/256;
etc etc
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline RKurtzDmitriyev

  • Former Staff
  • ****
Re: Nested while loops?
« Reply #2 on: 30 Jan 2011, 13:54:03 »
There should be no problem with a nested while loop. The following code causes a noticeable jolt of lag, but it works:

Code: [Select]
private ["_it","_sit","_existencepoints","_return"];

_it = 0;
_existencepoints = 0;

while {_it < 400} do

{
_it = _it + 1;
_sit = 0;
while {_sit < 300} do
{
_sit = _sit + 1;
_existencepoints = _existencepoints + 1;

}
};
if (_existencepoints == 120000) then {hint "I exist"} else {hint "error!"};

_return = _existencepoints;
_return

The function should tell you it exists (isn't that nice?) and return 120000.

I don't really have time to investigate your function in too much detail, but some general tips:

--See if there's a general scripting error. Consult your arma2.rpt or arma2OA.rpt file, or enable in-game scripting error reports using the startup parameter -showScriptErrors .

--Try to isolate the problem. Build in tests (similar to the "I exist!" hint above) to verify that certain values are what you think they should be when they should be.

--Something I noticed from glancing at your script:
Code: [Select]
_u = _u + (karr_mags0 select _s)

Is (karr_mags0 select _s) an array or a string? If it's a string, the above code is not going to work, because _u is an array. You'll have to write [(karr_mags0 select _s)] instead. I've made that mistake many times.

--You may have to define some local variables BEFORE you use them inside of curly braces {}. Local variables get deleted as soon as the braces end, unless they were defined before they began.

--As haroon says, make sure you've defined all local variables as private at the beginning of the function.
« Last Edit: 30 Jan 2011, 13:55:42 by RKurtzDmitriyev »
The OFP Editing Center wishes to remind you that the faithful COMREF will never threaten to stab you and, in fact, cannot speak.
However, in the event that it does speak, you are encouraged to heed its advice. ;)

Offline KaRRiLLioN

  • Members
  • *
  • Grits -n- Peanut Butter are a wonderful thing
    • OFP RTS-3 Real Time War
Re: Nested while loops?
« Reply #3 on: 31 Jan 2011, 16:35:20 »
Thanks--turns out I didn't have all my local vars defined at the start and then the nested loops started working right.  Most of the time my issue seems to be a missing semicolon or comma somewhere.  I also didn't know I could turn on script errors.  They used to be on by default in OFP so it's great knowing they're still in Arma2.  Right now my scripts are chock full of hints and sidechat to see what's happening with my vars as they wend their way through the script.