Board index FlightGear Development Nasal

if test for vector error

Nasal is the scripting language of FlightGear.

if test for vector error

Postby gkick » Sun Jun 09, 2019 4:49 pm

Hi,

guess the code explains it better then I could

lookup atis on change of airport
##below is working but need to eliminate error if no ATIS or different name aka eham egkk loih

Code: Select all
var airport = airportinfo(getprop("sim/presets/airport-id"));
var atis = airport.comms('atis');
if (!size(atis))
    atis = airport.comms('awos');   
printf('%s %.2f', airport.id, size(atis) ? atis[0] : 'Not found');

   setprop("/systems/catis",atis[0]);
   setprop("/instrumentation/comm/frequencies/selected-mhz", (getprop("/systems/catis")));



### if atis is not 0 it throws up the following nasal error in the console
Nasal runtime error: vector index 0 out of bounds <size:0> at ....

So the question is how to prevent that runtime error ? Thanks
Last edited by Johan G on Mon Jun 10, 2019 7:32 am, edited 1 time in total.
Reason: Added [code] tags. They can be added manually or with the [^_ ] button.
gkick
 
Posts: 106
Joined: Sun Jan 01, 2017 11:04 pm

Re: if test for vector error

Postby gkick » Tue Jun 11, 2019 2:30 am

solved - just use a standard if function

## lookup atis on change of ap

Code: Select all
var airport = airportinfo(getprop("sim/presets/airport-id"));
var atis = airport.comms('atis');
if (!size(atis))
    atis = airport.comms('awos');
if (size(atis)== 0){
      print ("no atis data found, comm1 set to 111.1");         
      setprop("/instrumentation/comm/frequencies/selected-mhz", 111.11);
      } else {   
   printf('%s %.2f', airport.id, size(atis) ? atis[0] : 'Not found');
   setprop("/systems/catis",atis[0]);
   setprop("/instrumentation/comm/frequencies/selected-mhz", (getprop("/systems/catis")));   
}   


sorry no caret on my machine
Last edited by Johan G on Wed Jun 12, 2019 10:02 am, edited 1 time in total.
Reason: Added [code] tags
gkick
 
Posts: 106
Joined: Sun Jan 01, 2017 11:04 pm

Re: if test for vector error

Postby Johan G » Wed Jun 12, 2019 10:08 am

gkick wrote in Tue Jun 11, 2019 2:30 am:sorry no caret on my machine

Sorry, not a keyboard key, but the fifth blue and white button from left above the text box when editing a message.
Low-level flying — It's all fun and games till someone looses an engine. (Paraphrased from a YouTube video)
Improving the Dassault Mirage F1 (Wiki, Forum, GitLab. Work in slow progress)
Johan G
Moderator
 
Posts: 5451
Joined: Fri Aug 06, 2010 5:33 pm
Location: Sweden
Callsign: SE-JG
IRC name: Johan_G
Version: 3.0.0
OS: Windows 7, 32 bit

Re: if test for vector error

Postby gkick » Wed Jun 12, 2019 4:46 pm

Ok, thanks Johan
gkick
 
Posts: 106
Joined: Sun Jan 01, 2017 11:04 pm

Re: if test for vector error

Postby Johan G » Thu Jun 13, 2019 9:16 am

You are welcome. :)
Low-level flying — It's all fun and games till someone looses an engine. (Paraphrased from a YouTube video)
Improving the Dassault Mirage F1 (Wiki, Forum, GitLab. Work in slow progress)
Johan G
Moderator
 
Posts: 5451
Joined: Fri Aug 06, 2010 5:33 pm
Location: Sweden
Callsign: SE-JG
IRC name: Johan_G
Version: 3.0.0
OS: Windows 7, 32 bit


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest