Board index FlightGear Development Nasal

cockpit-view: from --config format to --addon format

Nasal is the scripting language of FlightGear.

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 7:27 pm

DON'T FORGET: if i leave everything in the config.xml exactly as it is in the original cockpit_view.xml file and create main.nas with an empty main(), it all loads and works just like the original... doing it this way loads all the nasal code into

/nasal/model-cockpit-view
/nasal/model-cockpit-view/loaded = 'true' (bool)
/nasal/model-cockpit-view/script = 'actual nasal code here'

i want the same thing but instead of the above "script" key with the raw nasal code, i want the file key like /nasal/console and others in the /nasal branch have... i think this is much cleaner and it should be easy to do but apparently not...
Last edited by wkitty42 on Mon Aug 14, 2017 7:29 pm, edited 1 time in total.
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby Hooray » Mon Aug 14, 2017 7:28 pm

Okay, just briefly (not having tested anything yet) - I think it seems that AndersG is right: the error is related to your bindings not using the proper namespace - to see if that's the case, just uncomment your Nasal bindings in the config.xml to make them do nothing - i.e. prepend a hash/pound sign and see if the issue (error) persists or not (it probably won't).

The next step, would be to add a new line to the binding to make it dump the namespace, e.g. beginning at globals, or the addons own namespace using debug.dump(globals) - you will want to look for stuff like __addon[n]__

Besides, I may have missed it, but you also need to io.include() view_handler.nas, or none of its symbols will be loaded into the addon's namespace

Again, I think AndersG is right, and that you'd be well served by looking at Philosopher's namespace browser - which is the equivalent of the property browser, just for browsing nasal namespaces instead of properties - with a "namespace" really being just that: a named dump space for variables that fit into a certain category/class, e.g. because they are specific to a certain addon.

Looking at the problem at hand, I believe we can do better to make this more convenient, and less awkward admittedly ... once you have found a generic solution, you may want to keep Torsten in the loop, who's explicit encouraged addon related feedback to hopefully improve the framework over time.

PS: Regarding your edit/last comment, I believe/assume, that <nasal> modules would still be loaded into an addon specific namespace that lives under the addon's namespace, but Torsten should know more ... Again, the namespace browser certainly has all answers, and more :D
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: 11100
Joined: Tue Mar 25, 2008 8:40 am

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 7:49 pm

Hooray wrote in Mon Aug 14, 2017 7:28 pm:Okay, just briefly (not having tested anything yet) - I think it seems that AndersG is right: the error is related to your bindings not using the proper namespace

i think so, too, but i've tried several variants and even specify one in the io.load_nasal() call...
io.load_nasal(model_cockpit_view_dir ~ "view_handler.nas","model_cockpit_view");
but it is using underscores... maybe that should be dashes? does specifying the namespace in io.load_nasal() place the nasal code in the /nasal property tree branch??? doesn't it have its underscores automatically changed to dashes??

Hooray wrote in Mon Aug 14, 2017 7:28 pm: - to see if that's the case, just uncomment your Nasal bindings in the config.xml to make them do nothing - i.e. prepend a hash/pound sign and see if the issue (error) persists or not (it probably won't).

it won't/can't persist because the bindings won't be there and the mod won't work to switch to other craft... so yeah, it'll remove those errors but the mod won't work, either...

Hooray wrote in Mon Aug 14, 2017 7:28 pm:The next step, would be to add a new line to the binding to make it dump the namespace, e.g. beginning at globals, or the addons own namespace using debug.dump(globals) - you will want to look for stuff like __addon[n]__

add this line to the binding??? example using the posted code, please...

Hooray wrote in Mon Aug 14, 2017 7:28 pm:Besides, I may have missed it, but you also need to io.include() view_handler.nas, or none of its symbols will be loaded into the addon's namespace

you definitely missed it... look above for the bold example ;)

Hooray wrote in Mon Aug 14, 2017 7:28 pm:Again, I think AndersG is right, and that you'd be well served by looking at Philosopher's namespace browser

that'll corrupt my FGData repository... if i can place it in my special directory for my flightgear mods, that would be so so so much better... this special directory is where i store all my custom craft and custom scenery... in other words, if i can have

Code: Select all
~/myflightgear/Nasal/Philosopher's_namespace_browser.nas


then OK but the wiki stuffings assume that folks are going to corrupt their installs by placing code like this in their FGData directory which may be system owned and not available to them as a normal user... it does this (or did) with a large number of craft, too... then they uninstall and reinstall and poof! all their custom stuff is now gone because it was removed when they uninstalled...

Hooray wrote in Mon Aug 14, 2017 7:28 pm:Looking at the problem at hand, I believe we can do better to make this more convenient, and less awkward admittedly ...

i'm absolutely positive that we can... namespace_handle and namespace_name for the current namespace would be a GoodThing<tm>... we can already specify the namespace in io.load_nasal... oh wait, the docs call that a module but i've already complained about that up-topic...

Hooray wrote in Mon Aug 14, 2017 7:28 pm:once you have found a generic solution, you may want to keep Torsten in the loop, who's explicit encouraged addon related feedback to hopefully improve the framework over time.

i definitely plan on looping him in... i forgot that he's on vacation right now but once i get this done, someone will have to commit it to FGAddon for me...

Hooray wrote in Mon Aug 14, 2017 7:28 pm:PS: Regarding your edit/last comment, I believe/assume, that <nasal> modules would still be loaded into an addon specific namespace that lives under the addon's namespace, but Torsten should know more ... Again, the namespace browser certainly has all answers, and more :D

are you saying that namespaces can have namespaces within them??? i thought i was overriding that with io.load_nasal()??? see the bold line above ;)
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby Hooray » Mon Aug 14, 2017 7:56 pm

A namespace in Nasal is just a hash - and yes, namespaces can contain other namespaces - as in, hashes may contain an arbitrary depth of nested hashes/namespaces.
Regarding Philosopher's namespace browser, I assume that it should/could also just work if pasted in the console, as long as you add the Nasal snippet to trigger it (from the binding), too.

And yeah, I missed stuff you added - sorry about that, but I don't think we need to make this so complicated.

Imagine a namespace to be a "box", and you can have nested boxes, as in having boxes in boxes in boxes in boxes :mrgreen:

like you say, explicit calls to io.load_nasal() would move stuff into a custom namespace - but as far as I remember there is implicit renaming being done, which is what AndersG was previously hinting at I believe.
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: 11100
Joined: Tue Mar 25, 2008 8:40 am

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:09 pm

based on your statement "The next step, would be to add a new line to the binding to make it dump the namespace", i added
Code: Select all
      <key n="114">
        <name>r</name>
        <desc>debug.dump(globals)</desc>
        <binding>
          <command>nasal</command>
          <script>debug.dump(globals)</script>
        </binding>
      </key>

to config.xml so i could hit the 'r' key and dump the globals... i was met with this in the console :shock: :roll:
Code: Select all
Nasal runtime error: stack overflow
  at /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 227
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 238
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/debug.nas, line 254
  called from: /input/keyboard/key[114]/binding, line 1
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby Hooray » Mon Aug 14, 2017 8:17 pm

yeah, sorry, that was to be expected actually - it's trying to dump the whole namespace with probably thousands of symbols, so that it runs out of stack space (the function calling itself recursively). That is why I suggested to look up your current namespace using caller()/closure() and pass that to debug.dump() instead.

EDIT: The following should work via the Nasal Console: http://wiki.flightgear.org/Nasal_Console

Code: Select all
var browser = nil;
var make_window = func {
   browser = getprop("/sim/version/flightgear") == "2.10.0" ? canvas.Dialog.new([400,600]) : canvas.Window.new([400,600]);
   browser.root = globals; browser.scroll = 0;
   browser.history = [];
   var my_canvas = browser.createCanvas()
                         .setColorBackground(0,0,0,0);
   var root = my_canvas.createGroup();
   # Title bar:
   var title_bar = root.createChild("group");
   title_bar.addEventListener("drag", func(e) { browser.move(e.deltaX, e.deltaY); });
   var x = 0;
   var y = 0;
   var rx = 8;
   var ry = 8;
   var w = 400;
   var h = 20;
   title_bar.createChild("path")
      .moveTo(x + w - rx, y)
      .arcSmallCWTo(rx, ry, 0, x + w, y + ry)
      .vertTo(y + h)
      .horizTo(x)
      .vertTo(y + ry)
      .arcSmallCWTo(rx, ry, 0, x + rx, y)
      .close()
      .setColorFill(0.25,0.24,0.22)
      .setStrokeLineWidth(0)
      .addEventListener("click", func {
         gui.popupTip("Back");
         browser.root = pop(browser.history);
         if (!size(browser.history)) append(browser.history, globals);
         browser.scroll = 0;
         display_root();
      });
   # Border/background
   x = 0;
   y = 20;
   w = 400;
   h = 580;
   root.createChild("path")
      .moveTo(x + w, y)
      .vertTo(y + h)
      .horizTo(x)
      .vertTo(y)
      .setColorFill(1,1,1)
      .setColor(0,0,0);
   # Red-X: close this dialog
   x = 8;
   y = 5;
   w = 10;
   h = 10;
   title_bar.createChild("path", "icon-close")
      .moveTo(x, y)
      .lineTo(x + w, y + h)
      .moveTo(x + w, y)
      .lineTo(x, y + h)
      .setColor(1,0,0)
      .setStrokeLineWidth(3)
      .addEventListener("click", func remove_window());
   # Title of this dialog
   title_bar.createChild("text", "dialog-caption")
      .setText("Nasal namespace browser")
      .setTranslation(x + w + 8, 4)
      .setAlignment("left-top")
      .setFontSize(14)
      .setFont("LiberationFonts/LiberationSans-Bold.ttf")
      .setColor(1,1,1);
   var body = root.createChild("group");
      body.addEventListener("click", func(e) {
         var y = e.clientY-20+browser.scroll;
         y /= 16;
         var clicked = browser.children[y];
         gui.popupTip("Clicked on "~clicked.id);
         clicked = clicked.value;
         if (typeof(clicked) == 'hash' or typeof(clicked) == 'vector') {
            append(browser.history, browser.root);
            browser.root = clicked;
            browser.scroll = 0;
         }
         display_root();
      });
      body.addEventListener("drag", func(e) {
         browser.scroll += e.deltaY;
         browser.scroll = browser.scroll < 0 ? 0 : browser.scroll > (size(browser.children)-8)*16 ? math.max((size(browser.children)-8)*16,0) : browser.scroll;
         display_root();
      });
      var display = func(k,variable,sep=" = ") {
         var t = typeof(variable);
         if (t == 'scalar')
            k~sep~"'"~variable~"'";
         elsif (t == 'hash' or t == 'vector')
            k~"/ (size: "~size(variable)~")";
         elsif (t == 'nil')
            k~sep~"nil";
         else k~sep~"<"~t~">";
      };
      var update_children = func() {
         browser.children = [];
         var x = 4;
         var y = 34-16;
         if (typeof(browser.root) == 'hash') {
            foreach (var k; sort(keys(browser.root), cmp)) {
               if (k == "arg") continue;
               append(browser.children, {
                  id: k, value: browser.root[k], x: x, y: (y+=16),
               });
            }
         } elsif (typeof(browser.root) == 'vector') {
            forindex (var k; browser.root) {
               append(browser.children, {
                  id: k, value: browser.root[k], x: x, y: (y+=16),
               });
            }
         }
      };
      var display_root = func() {
         update_children();
         body.removeAllChildren();
         foreach (var child; browser.children) {
            var composite = child.y-browser.scroll;
            if (composite > 32 and composite < 575)
               body.createChild("text")
                  .setText(display(child.id,child.value))
                  .setTranslation(child.x, composite)
                  .setAlignment("left-baseline")
                  .setFontSize(14)
                  .setFont("LiberationFonts/LiberationSans-Bold.ttf")
                  .setColor(0,0,0);
         }
         body.update();
      };
   display_root();

   browser.listener = setlistener("/devices/status/keyboard/event", func(event) {
      if (!event.getNode("pressed").getValue())
         return;
      var key = event.getNode("key");
      var shift = event.getNode("modifier/shift").getValue();
      if (key.getValue() == 27 and !shift) {
         remove_window();
         key.setValue(-1);           # drop key event
      }
   });
};

var toggle_window = func {
   if (browser != nil) remove_window();
   else {
      io.load_nasal(getprop("/sim/fg-root") ~ "/Nasal/nasal_browser.nas"); #reload ourselves for testing purposes
      make_window();
   }
};
var remove_window = func {
   removelistener(browser.listener);
   browser.del();
   browser = nil;
};

toggle_window()

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

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:25 pm

Hooray wrote in Mon Aug 14, 2017 7:56 pm:And yeah, I missed stuff you added - sorry about that,

not a problem... we all do it from time to time...

Hooray wrote in Mon Aug 14, 2017 7:56 pm:but I don't think we need to make this so complicated.

i'm not making it complicated... i am, however, trying to figure out and understand something that is complicated and not easily understood...

Hooray wrote in Mon Aug 14, 2017 7:56 pm:Imagine a namespace to be a "box", and you can have nested boxes, as in having boxes in boxes in boxes in boxes :mrgreen:

yeah, i understand that... i didn't know that flightgear allowed for that, though...

Hooray wrote in Mon Aug 14, 2017 7:56 pm:like you say, explicit calls to io.load_nasal() would move stuff into a custom namespace

ok... so i do understand that, then...

Hooray wrote in Mon Aug 14, 2017 7:56 pm: - but as far as I remember there is implicit renaming being done,

my understanding from reading and reading and reading the posts about it seem to say that main.nas is loaded into __addon[?]__ and executed from there... it doesn't say that other nasal code loaded from main() via io.load_nasal() without an explicit namespace specified will also be loaded into this same namespace or into a sub-namespace... it also doesn't say that loading other nasal code via io.load_nasal() with an explicit namespace will be loaded into a sub-namespace under __addon[?]__...

Hooray wrote in Mon Aug 14, 2017 7:56 pm:which is what AndersG was previously hinting at I believe.

yeah, i know and i did try what AndersG suggested but it didn't work, either... it is back up topic... i forget, now, what the errors were IF there even were any but i can go back and try it again with the code i posted... as i recall, there weren't any errors or other output written to the console when i tried it... it just didn't work in the same manner that it hasn't been... i'll check it again, though, and post the results, again...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:27 pm

Hooray wrote in Mon Aug 14, 2017 8:17 pm:yeah, sorry, that was to be expected actually - it's trying to dump the whole namespace with probably thousands of symbols, so that it runs out of stack space (the function calling itself recursively). That is why I suggested to look up your current namespace using caller()/closure() and pass that to debug.dump() instead.

yeah, that's still klingartian to me... complicated by the lack of viable and straight forward real life examples :(

i'll try your code and report back...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby Hooray » Mon Aug 14, 2017 8:31 pm

Basically, AndersG suggested to "alias" a namespace by introducing a global namespace referencing the local (addon) one - which is what his snippet of code would accomplish under normal circumstances.
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: 11100
Joined: Tue Mar 25, 2008 8:40 am

Re: cockpit-view: from --config format to --addon format

Postby AndersG » Mon Aug 14, 2017 8:37 pm

My suggestion didn't work since I didn't remember that the nonexisting subhash to globals needed to be initialized (otherwise it is nil).

The following main.nas appears to work here (with the rest of the old XML file in config.xml):
Code: Select all
#  Model view by Melchior Franz. Modified to cockpit view by Anders Gidenstam.
#
#  Usage:
#    Add - -addon=ModelCockpitView to your FlightGear command line.
#    Once you have changed to the model or model cockpit view (using v/V) you
#    can step though the available AI/MP models with q/Q.
#    Note that the defaul view offset for the model cockpit view usually isn't
#    quite right, but you can move the view using MMB drag in mouse view mode.

var model_view_handler = {
    init : func {
        me.models = {};
        me.list = [];
        me.current = 0;
        me.active = 0;
    },
    start : func {
        me.models = {};
        var ai = props.globals.getNode("/ai/models", 1);
        foreach (var m; [props.globals]
                        ~ ai.getChildren("aircraft")
                        ~ ai.getChildren("carrier")
                        ~ ai.getChildren("multiplayer")
                        ~ ai.getChildren("tanker")
                        ~ ai.getChildren("groundvehicle"))
            me.models[m.getPath()] = m;

        me.lnr = [];
        append(me.lnr, setlistener("/ai/models/model-added", func(n) {
            var m = props.globals.getNode(n.getValue(), 1);
            me.models[m.getPath()] = m;
        }));
        append(me.lnr, setlistener("/ai/models/model-removed", func(n) {
            var m = props.globals.getNode(n.getValue(), 1);
            delete(me.models, m.getPath());
        }));
        me.active = 1;
        me.reset();
    },
    update : func {
        return 0;
    },
    stop : func {
        me.active = 0;
        foreach (var listener; me.lnr)
            removelistener(listener);
    },
    reset : func {
        me.next(me.current);
    },
    next : func(v) {
        if (!me.active or !size(me.models))
            return;
        if (v)
            me.current += v;
        else
            me.current = 0;
     
        me.list = sort(keys(me.models), cmp);
        if (me.current < 0)
            me.current = size(me.list) - 1;
        elsif (me.current >= size(me.list))
            me.current = 0;

        var s = "/sim/view[97000]/config";
        var c = me.list[me.current];
        setprop(s, "eye-lat-deg-path", c ~ "/position/latitude-deg");
        setprop(s, "eye-lon-deg-path", c ~ "/position/longitude-deg");
        setprop(s, "eye-alt-ft-path", c ~ "/position/altitude-ft");
        setprop(s, "eye-heading-deg-path", c ~ "/orientation/true-heading-deg");
        setprop(s, "eye-pitch-deg-path", c ~ "/orientation/pitch-deg");
        setprop(s, "eye-roll-deg-path", c ~ "/orientation/roll-deg");

        var n = me.models[me.list[me.current]];
        var type = n.getName();
        var name = nil;
        if (type == "") {
            if (name = getprop("/sim/multiplay/callsign"))
                 name = 'callsign "' ~ name ~ '"';
        } else {
            if ((name = n.getNode("callsign")) != nil and (name = name.getValue()))
                name = 'callsign "' ~ name ~ '"';
            elsif ((name = n.getNode("name")) != nil and (name = name.getValue()))
                name = n.getName() ~ ' "' ~ name ~ '"';
            else
                name = n.getName() ~ " " ~ n.getIndex();
        }
        var color = {};
        if (type != "multiplayer")
            color = { text: { color: { red: 0.5, green: 0.8, blue: 0.5 }}};
        if (name)
            gui.popupTip(name, 2, color);
    },
};

var main = func {
    globals["model-cockpit-view"] = {};
    globals["model-cockpit-view"].model_view_handler = model_view_handler;

    settimer(func {
        view.manager.register("Model Cockpit View",
                              model_view_handler);
    }, 1);
}


I had to put the settimer around the view.manager.register() call there since it appears the main function is called before the view module has been initialized. I'm not sure that could not be considered a bug in the AddOn framework...
Callsign: SE-AG
Aircraft (uhm...): Submarine Scout, Zeppelin NT, ZF Navy free balloon, Nordstern, Hindenburg, Short Empire flying-boat, ZNP-K, North Sea class, MTB T21 class, U.S.S. Monitor, MFI-9B, Type UB I submarine, Gokstad ship, Renault FT.
AndersG
 
Posts: 2404
Joined: Wed Nov 29, 2006 9:20 am
Location: Göteborg, Sweden
Callsign: SE-AG
OS: Debian GNU Linux

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:37 pm

replying to Hooray:

that's what i guessed it would do, too... that or copy/move it to there but...

so, also, i've run the code you posted... couple of things...

1. how to you resize canvas windows like pui windows can be??
2. there's only four items in __addon[0]__
2a. main = <func>
2b. model_cockpit_view_dir = '/home/myuser/myflightgear/Ad
2c. model_cockpit_view_init = <func>
2d. model_cockpit_view_root = '/home/myuser/myflightgear/A

2b and 2c are cut off because i cannot widen the window but they do contain what i expect them to contain... now i have to figure out how to move back to the main namespace and to move around in the thing without closing it and executing it again... /me trundles off to the wiki for a moment or three :|
Last edited by wkitty42 on Mon Aug 14, 2017 8:42 pm, edited 1 time in total.
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:42 pm

AndersG wrote in Mon Aug 14, 2017 8:37 pm:My suggestion didn't work since I didn't remember that the nonexisting subhash to globals needed to be initialized (otherwise it is nil).

that makes perfect sense!

AndersG wrote in Mon Aug 14, 2017 8:37 pm:The following main.nas appears to work here (with the rest of the old XML file in config.xml):
Code: Select all
var main = func {
    globals["model-cockpit-view"] = {};
    globals["model-cockpit-view"].model_view_handler = model_view_handler;

    settimer(func {
        view.manager.register("Model Cockpit View",
                              model_view_handler);
    }, 1);
}

i'll give this a try and see what happens...

AndersG wrote in Mon Aug 14, 2017 8:37 pm:I had to put the settimer around the view.manager.register() call there since it appears the main function is called before the view module has been initialized. I'm not sure that could not be considered a bug in the AddOn framework...

that makes sense, too... we'll see what happens in a minute... /me forgets about the wiki and goes to add these few new lines of code slightly modded for the posted code...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

Re: cockpit-view: from --config format to --addon format

Postby AndersG » Mon Aug 14, 2017 8:49 pm

You can also fetch my quick conversion from here: http://www.gidenstam.org/FlightGear/misc/ModelCockpitView-addon.tar.gz

However, it should probably be updated with a copy of the arrows from the normal Model View instead of the key binding - in particular as some aircraft uses q/Q for other things...
Callsign: SE-AG
Aircraft (uhm...): Submarine Scout, Zeppelin NT, ZF Navy free balloon, Nordstern, Hindenburg, Short Empire flying-boat, ZNP-K, North Sea class, MTB T21 class, U.S.S. Monitor, MFI-9B, Type UB I submarine, Gokstad ship, Renault FT.
AndersG
 
Posts: 2404
Joined: Wed Nov 29, 2006 9:20 am
Location: Göteborg, Sweden
Callsign: SE-AG
OS: Debian GNU Linux

Re: cockpit-view: from --config format to --addon format

Postby Hooray » Mon Aug 14, 2017 8:52 pm

AndersG wrote:I had to put the settimer around the view.manager.register() call there since it appears the main function is called before the view module has been initialized. I'm not sure that could not be considered a bug in the AddOn framework...


That sounds like the long-standing issue of Nasal's lack of dependency resolution - in this case however, that may actually help people to stuff prior to loading $FG_ROOT/Nasal
For the time being, my suggestion would be use a listener and modify the view.nas file accordingly, so that the addon dependency on view.nas will "wait" for view.nas to be finished.

We're basically running into the same problem with hard-coded subsystems that Nasal scripts would implicitly rely on, without this being formalized/encoded anywhere. Which is why I previously modified a bunch of those (locally) to use the ::bind() and ::unbind() APIs in SGSubsystemMgr to make sure that such dependencies are made more prominent, i.e. using listeners rather than tons of postPostPostInit() hacks all over the place.

With more and more work on partial and incremental subsystem re-initialization (reset/re-init), it will become increasingly relevant to encode and enforce such dependencies, so that Nasal scripts don't just stop working once a subsystem is removed/re-initializated at runtime.
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: 11100
Joined: Tue Mar 25, 2008 8:40 am

Re: cockpit-view: from --config format to --addon format

Postby wkitty42 » Mon Aug 14, 2017 8:59 pm

no luck, AndersG... here's the error...
Code: Select all
Initializing addon from /home/myuser/myflightgear/Addons/cockpit-view/main.nas in __addon[0]__
Cockpit View initializing...
  model_cockpit_view directory is /home/myuser/myflightgear/Addons/cockpit-view/
  attempting to load /home/myuser/myflightgear/Addons/cockpit-view/view_handler.nas
Nasal runtime error: undefined symbol: model_view_handler
  at /home/myuser/myflightgear/Addons/cockpit-view/main.nas, line 39
  called from: /home/myuser/myflightgear/Addons/cockpit-view/main.nas, line 51
  called from: /home/myuser/flightgear-dev/fgdata-ng/Nasal/addons.nas, line 26

and main.nas...
Code: Select all
# This file is part of the FlightGear cockpit-view addon.
#
# The cockpit-view addon is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# cockpit-view is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this addon.  If not, see <http://www.gnu.org/licenses/>.

# This is the main addon Nasal hook. It MUST contain a function
# called "main". The main function will be called upon init with
# the file system path pointing to the addon installation directory
# on your hard disk
#
# This script will live in it's own Nasal namespace that gets
# dynamically created from the global addon init script.
# It will be something like "__addon[n]__" where n is a positive integer
#

var model_cockpit_view_root = nil;
var model_cockpit_view_dir = "";
globals["model-cockpit-view"] = {};


var model_cockpit_view_init = func(root) {
    print("Cockpit View initializing...");
    model_cockpit_view_root = root;
    model_cockpit_view_dir = sprintf("%s/", model_cockpit_view_root);
    print("  model_cockpit_view directory is " ~ model_cockpit_view_dir );
    print("  attempting to load " ~ model_cockpit_view_dir ~ "view_handler.nas");
    io.load_nasal(model_cockpit_view_dir ~ "view_handler.nas","model_cockpit_view");
    # how can i determine if io.load_nasal has an error or not???
    globals["model-cockpit-view"].model_view_handler = model_view_handler;          <=== line 39
    settimer(func {
        print("  attempting to register model_view_handler");
        view.manager.register("Model Cockpit View", model_view_handler);
        print("  attempt to complete");
    }, 1);
    # this next line really should be in an if/then statement in case io.load_nasal has an error
    print("Cockpit View loaded and initialized.");
}


var main = func( root ) {
    model_cockpit_view_init(root);
} # func main()

Last edited by wkitty42 on Mon Aug 14, 2017 9:08 pm, edited 1 time in total.
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 4550
Joined: Fri Feb 20, 2015 3:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 14.04.5

PreviousNext

Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest