Board index FlightGear Development Nasal

Is there a problem with this code?

Nasal is the scripting language of FlightGear.

Is there a problem with this code?

Postby legoboyvdlp » Fri Dec 02, 2016 10:39 am

I have a file, systems.nas
I added a block of code.
Code: Select all
##########  Fuel System ##########
setlistener("/sim/signals/fdm-initialized", func {
print("Fuel Nasal Controls: Initializing, please wait");
}

var timerctrfuel = func(fuelsys){
if ((/controls/fuel/tank[2]/pump-left == 0) and (/controls/fuel/tank[2]/pump-right == 0)) {
   setprop("consumables/fuel/tank[2]/selected",0);
} else {
   boeing737.fuelsys();
}}


Hi! Is there a problem here? Showing a parse error on the second to last line.

The common.xml shows
Code: Select all
<nasal>
...
   <boeing737>
...
      <file>Aircraft/737-800/Nasal/systems.nas</file>
...
    </boeing737>


Probably an obvious error, but I can't find it. Out of practice with nasal!
I thought you needed the 'boeing737' part of the function 'repeat' code?
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Is there a problem with this code?

Postby sanhozay » Fri Dec 02, 2016 10:52 am

Sometimes parsers get confused. You can't always rely on them telling you the line where you made a mistake, especially with things like forgetting to close a bracket somewhere. They play along with you until they realise you messed up some time ago, then they bail out with an error. When the line looks OK, work backwards looking for missing closing brackets.

Code: Select all
##########  Fuel System ##########
setlistener("/sim/signals/fdm-initialized", func {
print("Fuel Nasal Controls: Initializing, please wait");
} # I think you are missing ); here to close the setlistener function call

:idea: Consistent indentation helps a lot to spot these, as does an editor with syntax highlighting. It saves counting brackets.

Note there may be other errors that show up after fixing this one.

The "boeing737" prefix is the namespace: http://wiki.flightgear.org/Nasal_Namespaces
sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 12:57 pm
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04

Re: Is there a problem with this code?

Postby legoboyvdlp » Fri Dec 02, 2016 11:32 am

Thanks, sanhozay!
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Is there a problem with this code?

Postby legoboyvdlp » Fri Dec 02, 2016 12:03 pm

For the record and instruction of other nasal novices,
the correct code is

Code: Select all
##########  Fuel System ##########
setlistener("/sim/signals/fdm-initialized", func {
print("Fuel Nasal Controls: Initializing, please wait");
});

var fuelctrleft = getprop("/controls/fuel/tank[2]/pump-left");
var fuelctrright = getprop("/controls/fuel/tank[2]/pump-right");

var timerctrfuel = func(fuelsys){
if ((fuelctrleft == 0) and (fuelctrright == 0)) {
   setprop("consumables/fuel/tank[2]/selected",0);
} else {
   setprop("consumables/fuel/tank[2]/selected",1);
   boeing737.fuelsys();
}};


Thanks for the syntax highlighter tip, sanhozay -- v helpful!
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: Is there a problem with this code?

Postby sanhozay » Fri Dec 02, 2016 12:32 pm

No problem.

Some other ideas for extra bonus points ...

  • Unless you need to use fuelctrlleft and fuelctrlright outside of the function, it's good practice to make them local.
  • You don't need to check boolean values against zero, you can use the not (!) operator. See Nasal Conditionals. Eventually you will read this as "if not pump left and not pump right".
  • I think you have to ask yourself why the "fuelsys" parameter is there. It's not used inside the function. The call to a function called fuelsys later on makes this confusing.
  • Does timerctrfuel explain what the function does? Timer centre fuel? Timer control fuel?
  • Is this function repeated somewhere for tanks other than tank 2? Maybe the tank should be passed as a parameter?
Code: Select all
var timerctrfuel = func {

    var pump_left = getprop("/controls/fuel/tank[2]/pump-left");
    var pump_right = getprop("/controls/fuel/tank[2]/pump-right");

    if (!pump_left and !pump_right) {
        setprop("consumables/fuel/tank[2]/selected", 0);
    } else {
       setprop("consumables/fuel/tank[2]/selected", 1);
       boeing737.fuelsys();
    }
}

(The above is untested).
sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 12:57 pm
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04


Return to Nasal

Who is online

Users browsing this forum: No registered users and 6 guests