Announcement

Collapse
No announcement yet.

main() function reload by return/break commands

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • main() function reload by return/break commands

    Hi, I got some trouble using the return or break (switch case break) commands in functions i call out of my main loop, more precisely out of my touch detection.

    main loop touch check:

    HTML Code:
       touch_Set(TOUCH_ENABLE);
    // reacts on screen touched
    state := touch_Get(TOUCH_STATUS);
    if(state == TOUCH_PRESSED)
    x := touch_Get(TOUCH_GETX);
    y := touch_Get(TOUCH_GETY);
    
    // default value for no buttons touched
    n := -1;
    //checks for touch events
    if(currentView == 0)
    mainView();
    endif
    if(currentView == 1)
    graphView();
    endif
    
    
    // pushes the button
    if(n != -1)
    btnState[n] := 1;
    // method one: use buttons
    if(n == 0 || n == 1)
    drawButton(n);
    // method two: use any button image
    else
    img_Show(img, 1);
    img_Darken(hndl, n);
    img_Show(hndl, n);
    endif
    endif
    endif
    
    The problems occur in the mainView() or graphView() functions, if return or switch/case/break statements are used.
    
    My mainView() function currently looks like this, it holds the x/y touch region-checks for mymainView.
    func mainView()
    if( x > 10 && x < 100 && y > 234)
    currentView := 1;
    showView(1);
    n := 0;
    endif
    if( x > 106 && x < 196 && y > 234)
    n := 1;
    endif
    if( x > 202 && x < 292 && y > 234)
    n := 2;
    endif
    if(x > 298 && x < 388 && y > 234)
    n := 3;
    endif
    
    endfunc
    If I try to write the mainView() function with switch case (...) break; case (...) break; endswitch oder adding simple return; in the if/endif statements to skip execution of unnecessary code the function will cause the main() function to be called out of some reason I can't figure out. Any mistakes on my side i miss or a compiler fail?


    edit: the forum doesnt show > and && commands in the code tags.

  • #2


    Sorry, Case is not working.



    use a string of 'else if's
    Mark

    Comment


    • #3


      multiple return commands in one function isn't working either? i get some really confusing irreproducible function calls when trying to end a function with an empty return.

      Comment


      • #4
        Sorry, you'll have to give an example for that one. The following seems to work just fine.

        func funcx (var i)
        if (i == 1)
        print(10) ;
        return ;
        else if (i == 2)
        print(22) ;
        return ;
        else if (i == 3)
        print(333) ;
        return ;
        else if (i == 4)
        print(16) ;
        return ;
        endif
        print(-1) ;
        return ;
        endfunc
        func main()
        funcx(1) ;
        print("\n") ;
        funcx(2) ;
        print("\n") ;
        funcx(3) ;
        print("\n") ;
        funcx(4) ;
        print("\n") ;
        funcx(5) ;
        print("\n") ;
        repeat forever
        endfunc
        Mark

        Comment


        • #5


          i pretty much did a similar thing like your example, but i skipped the "else" and had 2 functions build like that. it happened then always that the return command from one function called the 2nd function with multiple returns before going back to the main function producing some really nasty bugs.

          here an example what it looked like (i already changed it when i posted the bug):

          func graphView()
          if( x > 10 && x < 100 && y > 234)
          n := 0;
          paused := 0;
          graphSpeed := 1;
          speedIter := 0;
          return;
          endif
          if( x > 106 && x < 196 && y > 234)
          n := 1;
          paused := 1;
          return;
          endif
          if( x > 202 && x < 292 && y > 234)
          n := 2;
          graphSpeed *= 2;
          return;
          endif
          if( x > 298 && x < 388 && y > 234)
          n := 3;
          viewToShow := 0;
          previousView := 1;
          return;
          endif
          if( x > 210 && x < 410 && y > 25 && y < 225)
          zoomIn(0);
          return;
          endif
          if( x > 10 && x < 210 && y > 25 && y < 225)
          zoomIn(1);
          return;
          endif
          if (x > 410 && x < 480)
          zoomOut();
          return;
          endif

          endfunc

          func numpadView()
          if(x > 10 && x < 390 && y > 234)
          n := 4;
          viewToShow := previousView;
          previousView := 5;
          return;
          endif
          // numpad 1
          if(x > 20 && x < 70 && y > 20 && y < 70)
          numPressed(1);
          endif
          endfunc
          so practically i tried to avoid the else ifs with simply letting him end the function if he matched one condition, because the function wasn't supposed to do anything but checking the touch region.

          in my main view i used a variable (currentView) to determine in which view i currently am and thatfor which touch regions i have to check. When i was in my numpad view and pushed the display first the numpadView() was executed correctly but to what ever reason then the graphView got called too (both functions with same x-y regions) and the progam executed code it shouldn't have. Simply removing the return statements in both functions without changing anything else solved the issue.

          anyway using the else/if strings now, but te compiler seems to have some serious trouble if i don't structure the code with else/if.

          Comment


          • #6


            There's not enough code there to create a useful demo.



            If you have 'backup files' turned on, you should be able to find the version that was causing issues in the __HISTORY folder, below your project folder.



            PS Use 'Wrap Quotes', not 'Wrap Code Tags'. Code Tags means HTML code tags, that's what stuffs up your code when it is posted
            Mark

            Comment


            • #7


              I didn't have backup files turned on, so nevermind. I doubt i will stumble on that issue again, as i'm now simply staying with if/else.

              thanks for the tip with the code/quote tags.

              Comment

              Working...
              X