Board index FlightGear Development Nasal

How do I add/load a custom nasal file?

Nasal is the scripting language of FlightGear.

How do I add/load a custom nasal file?

Postby chriscalef » Wed Feb 20, 2019 9:32 pm

Sorry for the total newbie question here, but I'm having a hard time finding out where FG loads all the nasal on startup. I created a file, simearth.nas, which has a function called saveTerrain() in it. I put that file in my FGDATA/Nasal folder, with the hope that it would get autoloaded just by virtue of being there.

I then went to menubar.xml and created a menu with a menu item that tries to bind a function from that file, like so:

Code: Select all
   <menu>
      <name>SimEarth</name>
      <item>
        <name>save-terrain</name>
        <binding>
          <command>nasal</command>
          <script>simearth.saveTerrain("Save Terrain Dialog")</script>
        </binding>
      </item>
   </menu>


Unfortunately my file does not get loaded, apparently, because I get the message "Nasal runtime error: undefined symbol: simearth" in my console.

Can anybody point me at the init routine, or list, or whatever it is that defines which nasal files get loaded?

Thanks!
Chris
chriscalef
 
Posts: 236
Joined: Wed Feb 20, 2013 9:28 pm

Re: How do I add/load a custom nasal file?

Postby wlbragg » Wed Feb 20, 2019 10:02 pm

I put that file in my FGDATA/Nasal folder, with the hope that it would get autoloaded just by virtue of being there

That is correct.

It isn't recognizing the class "simearth"
Can we see the code?
Kansas(2-27-15)/Ohio/Midwest scenery development.
KEQA (2-27-15), 3AU, KRCP Airport Layout
User avatar
wlbragg
 
Posts: 4684
Joined: Sat Aug 25, 2012 11:31 pm
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Debain/nVGT640

Re: How do I add/load a custom nasal file?

Postby Thorsten » Thu Feb 21, 2019 6:02 am

The general idea is correct, so likely there's a typo somewhere - a parse error in the Nasal file would also prevent it from being loaded and lead to that error message downstream.
Thorsten
 
Posts: 10638
Joined: Mon Nov 02, 2009 8:33 am

Re: How do I add/load a custom nasal file?

Postby Hooray » Thu Feb 21, 2019 2:46 pm

Like others said already, this is basically the correct way - why this isn't working for you, remains unclear until you share your exact code and some more details (OS, case sensitive file system ?).

Other than that, I would generally recommend to always have a single one-liner at the bottom of your Nasal code:
Code: Select all
print("my module loaded successfully");


This is so that you can see if the module is loaded or not - if there are any unhandled parse errors, the parser will bail out and you won't get to see that line.

Also, for testing purposes, I would suggest to put stuff into $FG_HOME/Nasal instead of $FG_ROOT/Nasal, which is generally intended to be read-only on most OS.

Obviously, YMMV - i.e. if you are planning on shipping/sharing your work with others, the latest "best practice" is actually using a dedicated Nasal Addon ($FG_ROOT/Docs/README.addons or see the wiki).

There is also a skeleton add-on that you can use as a template to prototype new-ones.
As far as I've heard, there are also plans being discussed to extend the add-on mechanism so that it also supports networking

If in doubt, it would be best to share more background information/context with us so that we can provide tailored help...
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: 11317
Joined: Tue Mar 25, 2008 8:40 am

Re: How do I add/load a custom nasal file?

Postby chriscalef » Thu Feb 21, 2019 3:57 pm

Wow, thanks for the help everybody!

Sure enough, it was trouble in the nasal code: I forgot to put a semicolon after the function definition. :oops:

Nice to know I was right about how things were supposed to work anyway!

EDIT: also, thanks Hooray for the tips. I forgot I shouldn't be putting things in FG_ROOT. Will check out the Nasal Add-On thing.

So on that subject, if I want to modify xml files, eg menubar.xml, should I make copies of the files I want to edit, modify them, put them in FG_HOME, and then expect FG to load my modified version instead of the original? (Testing, I guess I'll find out before anyone has time to answer my question...) :P
Last edited by chriscalef on Thu Feb 21, 2019 4:07 pm, edited 1 time in total.
chriscalef
 
Posts: 236
Joined: Wed Feb 20, 2013 9:28 pm

Re: How do I add/load a custom nasal file?

Postby Hooray » Thu Feb 21, 2019 4:05 pm

Right, I noticed the missing semicolon in your bindings code, too - and forgot to mention that, still.
The thing is, due to some FG/Nasal internals, it isn't strictly needed there (i.e. implicitly added by the parser when compiling the bindings code), but you should probably consider it a "best practice" to use it there, too. For the same reason, it would make sense to also ALWAYS use CDATA section for any XML sections containing Nasal blobs (script code).

Other than that, the add-on framework really is what you want to be using when creating "plugin"-like features, i.e. stuff that you may sooner or later want to be able to easily share with others, but also if you want to be able to get others involved in contributing to your work (think github repo), as well as distributing updates for it (e.g. new versions)
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: 11317
Joined: Tue Mar 25, 2008 8:40 am

Re: How do I add/load a custom nasal file?

Postby chriscalef » Thu Feb 21, 2019 4:13 pm

Ah, yeah, I just went off the existing examples, and they didn't use a semicolon on the bindings so I didn't either. Always does feel a little sloppy doing that though.

Re: the xml files however, I see that it does *not* work to make a new gui folder in FG_HOME and expect it to supersede the FG_ROOT version. Any tips for proper menu modifications?

Or does the add on framework cover that too? Sounds like I need to dig into that.

EDIT: Having Rd the first page of TFM... ah, I see! Yes I definitely need to create a legit add-on here.
chriscalef
 
Posts: 236
Joined: Wed Feb 20, 2013 9:28 pm

Re: How do I add/load a custom nasal file?

Postby Hooray » Thu Feb 21, 2019 4:18 pm

chriscalef wrote in Thu Feb 21, 2019 3:57 pm:So on that subject, if I want to modify xml files, eg menubar.xml, should I make copies of the files I want to edit, modify them, put them in FG_HOME, and then expect FG to load my modified version instead of the original? (Testing, I guess I'll find out before anyone has time to answer my question...) :P


Actually, add-ons are the answer here, too.
Other than that, there is a gui.nas module to help modify/update the menubar dynamically.
But this stuff got recently implemented to work much better for add-ons, so I guess that's what I'd recommend to use. Most non-addon solutions are rather messy to be honest. Rominet (Florent) has specifically worked out a way to create a custom menubar file for each add-on, you could still update that separately though.

PS: This stuff is actually covered in other topics here, so it would be a good idea to search the forum first, or at least create a new topic for new/unrelated questions, so that others can more easily follow
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: 11317
Joined: Tue Mar 25, 2008 8:40 am

Re: How do I add/load a custom nasal file?

Postby chriscalef » Thu Feb 21, 2019 4:41 pm

Cool, thanks Hooray! Yeah, it seems that an AddOn is clearly what I need here, but regarding my original question the TL/DR is:

A) Yes, autoloading of Nasal works, if you have legal code sitting in the FG_ROOT/Nasal directory, but

B) the better way is to put your legal code into FG_HOME/Nasal so it doesn't get overwritten with updates etc, and also

C) it is wise to seriously consider putting all changes into an Add On structure, details of which can be found in $FG_ROOT/Docs/README.add-ons.

With that, I'll consider this thread closed. Thanks, everybody! :D
chriscalef
 
Posts: 236
Joined: Wed Feb 20, 2013 9:28 pm

Re: How do I add/load a custom nasal file?

Postby Hooray » Thu Feb 21, 2019 7:21 pm

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: 11317
Joined: Tue Mar 25, 2008 8:40 am


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest

cron