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 it0uchpods » Mon Apr 17, 2017 7: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
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

Postby jam007 » Mon Apr 17, 2017 7: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: 477
Joined: Sun Dec 16, 2012 10:04 am
Location: Uppsala, Sweden
Version: 2017.3.1
OS: Ubuntu 16.04

Re: Read/Write from files via Nasal

Postby Thorsten » Tue Apr 18, 2017 5: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: 9997
Joined: Mon Nov 02, 2009 8:33 am

Re: Read/Write from files via Nasal

Postby sanhozay » Tue Apr 18, 2017 10: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 11:57 am
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04

Re: Read/Write from files via Nasal

Postby it0uchpods » Tue Apr 18, 2017 6:56 pm

I need to use regular files.

Where can nasal write to?
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

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

jam007
 
Posts: 477
Joined: Sun Dec 16, 2012 10:04 am
Location: Uppsala, Sweden
Version: 2017.3.1
OS: Ubuntu 16.04

Re: Read/Write from files via Nasal

Postby it0uchpods » Wed Apr 19, 2017 10:34 pm

So I could write to FGAIRCRAFT/A320Family/Aircraft Config/settings.conf ?
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

Postby it0uchpods » Wed Apr 19, 2017 10: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?
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

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

Kansas(2-27-15)/Ohio/Midwest scenery development.
KEQA (2-27-15), 3AU, KRCP Airport Layout
User avatar
wlbragg
 
Posts: 4183
Joined: Sat Aug 25, 2012 11:31 pm
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Debain/nVGT640

Re: Read/Write from files via Nasal

Postby it0uchpods » Thu Apr 20, 2017 11:55 am

I've already read that. It doesn't explain it.
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

Postby jam007 » Thu Apr 20, 2017 12: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: 477
Joined: Sun Dec 16, 2012 10:04 am
Location: Uppsala, Sweden
Version: 2017.3.1
OS: Ubuntu 16.04

Re: Read/Write from files via Nasal

Postby it0uchpods » Fri Apr 21, 2017 11:20 pm

Thank you.
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

Postby it0uchpods » Sat May 20, 2017 5: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.
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Re: Read/Write from files via Nasal

Postby Thorsten » Sat May 20, 2017 5: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: 9997
Joined: Mon Nov 02, 2009 8:33 am

Re: Read/Write from files via Nasal

Postby it0uchpods » Sun May 21, 2017 2:19 pm

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

I'll check it.
it0uchpods/WTF411

FGFS Aircraft Developer
Lead Programmer at it0uchpods Design Group
Download High Quality Aircraft from it0uchpods Hangar
User avatar
it0uchpods
 
Posts: 3567
Joined: Tue Oct 06, 2015 12:51 pm
Version: 2018.2.1
OS: Windows 10 x64

Next

Return to Development

Who is online

Users browsing this forum: No registered users and 2 guests