Solved for loop issue

Question that is answered or resolved.

msmalik681

OpenBOR Developer
Staff member
I have a issue running more then 1 for loop in a function when I separate into 2 functions it works fine is there any other way around this ?
 
msmalik681 said:
I have a issue running more then 1 for loop in a function when I separate into 2 functions it works fine is there any other way around this ?

Nested loops are legal and should run without any problems. I have several that work just fine.

That said, breaking work down into smaller functions is usually best practice anyway, so it's not like you did a bad thing. Still, the original code should have worked. Post it here and let's have a look.

DC
 
well here is the animation script function do excuse all the camel case. Now the line before the second for loop does not update the menuName

And as a off topic can i ask if there is any way to check if a text object if valid without crashing the engine as you can see my for loop runs up to 30 but if it hits a uninitialized textobj the whole engine will crash.

Code:
void menugo(char sfx, char newName)
{
   void textbox = getindexedvar("speech");          //place speech entity in indexedvar
   int i; int x;                     //for loop counter
   if(sfx==NULL()) sfx="move";                //set default sfx if nothing set
   void movesfx = loadsample("data/sounds/menu_"+sfx+".wav");   //load sound effect to be used
   void menuList = getindexedvar("menuList");         //get menu array
   int menuCount = getentityvar(textbox,"menuCount");      //get menu index
   char menuName = get(menuList,getentityvar(textbox,"menuCount"));//get current menu location by name


            for(x=0;x<=30;x++)
            {
              if(get(menuList,x)==newName)         //look for text set by user
              {
              setentityvar(textbox, "menuCount",x);      //change menu index to match
        for(i=0;i<=30;i++)
        {
          if(gettextobjproperty(i,"text")==menuName)          //scroll all text objects untill a mach is found
          {
          setentityvar(textbox, "x",gettextobjproperty(i,"x")-20);    //new menu pointer loction
          setentityvar(textbox, "y",gettextobjproperty(i,"y")-2);     //new menu pointer location
          playsample(movesfx, 0, 150, 150, 100, 0);         //play menu movement sample
          break;
          }
        }
              break;
              } 
            }


}


just fixed it i just had to add the line:


Code:
menuName = get(menuList,getentityvar(textbox,"menuCount"));


at the end of the first for loop to update the script. but any insight on my text box question would really be appreciated.
 
Sorry, but at the moment there isn't, and I'm too tied up still with family and making up stuff from last week to add a check. You'll just have to monitor very carefully which textobjects you have initialized and not try to call one that doesn't exist.

I would suggest using an array, it's tailor made for that sort of work.

DC
 
You still helped me get this into one function confirming for loops can be nested so thanks.
 
Back
Top Bottom