Board index FlightGear Development Nasal

Starting a loop problem.  Topic is solved

Nasal is the scripting language of FlightGear.

Starting a loop problem.  

Postby kuifje09 » Sun Mar 09, 2014 7:58 pm

This is my code :
Code: Select all
Area.Run = func () {
   # Start loop, ploting all enabled lines with property
   printf("Start plotting is called");
   if(me._Run == 1){
      gui.popupTip("Plot already running",2);
      return me;
   };
   me._Run = 1;
   var Do_Plot = func(){
      var MyTimeout = func(){
         gui.popupTip("Plot already running",1);
         printf("plotting");
         if(me._Run == 1 ){
            settimer(MyTimeout,2);
         };
      };  # end Mytimeout
      MyTimeout();
   };
   return me;
};


it is a part of a bigger Class, and it shows it is started and already running but, The printf/popup in the loop is never executed.
What do I wrong ? I think the construction has to be a little different but I don't see it.
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm

Re: Starting a loop problem.

Postby Hooray » Sun Mar 09, 2014 8:02 pm

apparently, you are never calling the outer function, i.e. "Do_Plot" here ?
You just declared a nested function, without actually calling it - to call it, you would need to add Do_Plot() or call the function directly:

var Do_Plot = func() {} ();

But note that those nested functions are NOT methods, for them to be methods, you would need to add them to the outer hash, and not as nested functions that are only visible within the enclsoing function scope.

and please don't use settimer() for these things, maketimer() is much better here: http://wiki.flightgear.org/List_of_Nasa ... 2.11.2B.29
settimer/setlistener are far too low-level APIs for most purposes (and users), because they require explicit management, i.e. they may leak and cause issues that cannot be currently diagnosed without being intimately familiar with FG internals.
Last edited by Hooray on Sun Mar 09, 2014 8:09 pm, edited 2 times in total.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Starting a loop problem.

Postby kuifje09 » Sun Mar 09, 2014 8:06 pm

Found it already:

Changed this part:
Code: Select all
   me._DoPlot = func(){
         gui.popupTip("Plot running",1);
         if(me._Run == 1 ){
            settimer(me._DoPlot,2);
         };
   };
   me._DoPlot();
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm

Re: Starting a loop problem.

Postby Hooray » Sun Mar 09, 2014 8:07 pm

yes, now you're actually calling the function - but again: 1) you'll usually not want to use nested functions if they're supposed to be methods, and 2) don't use settimer - but favor maketimer() instead.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest