Board index FlightGear Development

Read/Write from files via Nasal

FlightGear is opensource, so you can be the developer. In the need for help on anything? We are here to help you.
Forum rules
Core development is discussed on the official FlightGear-Devel development mailing list.

Bugs can be reported in the bug tracker.

Read/Write from files via Nasal

Postby Octal450 » Mon Apr 17, 2017 8:10 pm

Is this possible?
I'd like to store settings for an aircraft in a file (like txt, ini etc), and build a GUI program for Windows Linux and Mac which can change the settings?

Thanks.
Josh
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby jam007 » Mon Apr 17, 2017 8:43 pm

Yes.
Eg:
Code: Select all
var read_beacons = func {
  print("Reading beacons");
  var fh = io.open(getprop("/sim/aircraft-dir")~"/beacons.txt", "r");
  var b="";
  while (b != nil) {
    b = io.readln(fh);
    if (b != nil) {
      append(beacons, new_beacon(split(",", b)));
      print("Adding: "~b);
    }
  }
  io.close(fh);
}
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Read/Write from files via Nasal

Postby Thorsten » Tue Apr 18, 2017 6:19 am

There's Nasal commands to write whole property tree sections to file (or read them in).

Beware though, the places where Nasal is allowed to write are fairly restricted for security reasons.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Read/Write from files via Nasal

Postby sanhozay » Tue Apr 18, 2017 11:54 am

Would a Phi module make more sense?

a. It's inherently cross-platform because it is accessed via a web browser
b. The plumbing is already there to access the property tree
c. You don't need to worry about the issues involved in creating files
d. It extends an existing tool rather than creating a new one
e. jQuery and other JavaScript libraries are useful to know

Or do you need to write the files to load at Flightgear startup?
sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 12:57 pm
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04

Re: Read/Write from files via Nasal

Postby Octal450 » Tue Apr 18, 2017 7:56 pm

I need to use regular files.

Where can nasal write to?
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby jam007 » Tue Apr 18, 2017 8:49 pm

jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Read/Write from files via Nasal

Postby Octal450 » Wed Apr 19, 2017 11:34 pm

So I could write to FGAIRCRAFT/A320Family/Aircraft Config/settings.conf ?
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby Octal450 » Wed Apr 19, 2017 11:39 pm

Can I make it read a specific line?

If not, how can I grab load each number or string in the file for a different option?
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby wlbragg » Thu Apr 20, 2017 2:48 am

Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Read/Write from files via Nasal

Postby Octal450 » Thu Apr 20, 2017 12:55 pm

I've already read that. It doesn't explain it.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby jam007 » Thu Apr 20, 2017 1:33 pm

The code example above reads line by line. Every line is a comma (,) separated string of data. The string is then split into a list.

Storing the data in a XML-property file and using XML-reader is probably the best solution if you have configuration data.
jam007
 
Posts: 579
Joined: Sun Dec 16, 2012 11:04 am
Location: Uppsala, Sweden
Callsign: LOOP
Version: 2020.4.0
OS: Ubuntu 22.04

Re: Read/Write from files via Nasal

Postby Octal450 » Sat Apr 22, 2017 12:20 am

Thank you.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby Octal450 » Sat May 20, 2017 6:52 pm

???
http://prntscr.com/fa3s11
???
Code: Select all
var file = io.open(getprop("/sim/aircraft-dir")~"/AircraftConfig/settings.conf","r+");

???

Kind Regards,
Josh

PS: Check the wiki. Did not find any helpful information.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Re: Read/Write from files via Nasal

Postby Thorsten » Sat May 20, 2017 6:57 pm

The upshot is that for security reasons, you're only allowed to write files in very few specific places from Nasal.

Check Nasal/save.nas in the Shuttle, I believe it pretty much does what you want.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Read/Write from files via Nasal

Postby Octal450 » Sun May 21, 2017 3:19 pm

I can't write in the aircraft's own folder?

I'll check it.
Skillset: JSBsim Flight Dynamics, Systems, Canvas, Autoflight/Control, Instrumentation, Animations
Aircraft: A320-family, MD-11, MD-80, Contribs in a few others

Octal450's GitHub|Launcher Catalog
|Airbus Dev Discord|Octal450 Hangar Dev Discord
User avatar
Octal450
 
Posts: 5583
Joined: Tue Oct 06, 2015 1:51 pm
Location: Huntsville, AL
Callsign: WTF411
Version: next
OS: Windows 11

Next

Return to Development

Who is online

Users browsing this forum: No registered users and 7 guests