Board index FlightGear Development Nasal

function call from sub hash

Nasal is the scripting language of FlightGear.

function call from sub hash

Postby Autowings » Thu Jan 02, 2020 5:00 pm

Hi Together and a Happy New Year,

the new decade has just begun and I'm facing a hash/class problem which I can not solve on my own:

For example, I create a class with several sub hashes "first", "second" and "third":

Code: Select all
var Cfoo_class =
{   new: func()
   {   var m = { parents: [Cfoo_class] };
      return m;
   },
   
   first:
   {   one: func()
      {   .... # do whatever one has to do ...
      },
      
      foo_one: func()
      {   .... # even more stuff ...
      },
   },
   
   second:
   {   two: func()
      {   .... # do whatever two has to do ...
      },
      
      foo_two: func()
      {   .... # even more stuff ...
      },
   },
   
   third:
   {   three: func()
      {   me.first.one();               # DOES NOT WORK !!! Error message: "No such member "first"
         parents[0].first.one();       # DOES NOT WORK !!! Error message: "Undefined symbol "parents"
         me.parents[0].first.one();       # DOES NOT WORK !!! Error message: "No such member "parents"
      },
   },
};


When I try to access a method/function that is located in a different sub hash, Nasal complains about member cannot be found.
What do I have to do to call functions from a different sub hash?

Regards,
Autowings
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 9:09 am

Re: function call from sub hash

Postby Hooray » Fri Jan 03, 2020 12:42 pm

It's always a good idea to tell us what you are trying to do. Also, the wiki has some pretty good articles covering how Nasal hashes and OOP via hashes works. For instance, are you familiar with the differences between composition and inheritance ? I'd suggest to take a look at the wiki, and then review your code (posting code that is self-contained so that people can try it via the Nasal console is also a good idea).

http://wiki.flightgear.org/Howto:Start_ ... s_in_Nasal
http://wiki.flightgear.org/Object_orien ... g_in_Nasal
http://wiki.flightgear.org/Object_Orien ... with_Nasal
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: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: function call from sub hash

Postby Autowings » Sat Feb 15, 2020 5:35 pm

Hello again,

I'm sorry that I had to be offline for some weeks. As well I'm sorry about the circumstance that the example I posted was unfortunately not transparent enough.

So, here is another example which I try to keep simple. It should just illustrate the problem that members (variables and functions or whatever) can not be accessed from inside a hash except by a global path:

Code: Select all
var myhash =
{   aileron:   0.52,

   elevator:   0.03,
   
   mixer:      aileron * 0.2 + elevator * 0.15,            #1
   mixer:      me.aileron * 0.2 + me.elevator * 0.15,         #2
   mixer:      myhash.aileron * 0.2 + myhash.elevator * 0.15,   #3
   
};


The problem I encounter is that neither assignment #1 nor assignment #2 for mixer will work, because aileron and elevator (although they both are members of the hash) are not known in this context, and also me does not know these members.
Assignment #3 does work instead, but it is a global path and will also fail as soon the script file is loaded into the namespace context of an aircraft (myplane, for example), where the path should be extended to
Code: Select all
    mixer: myplane.myhash.aileron * 0.2 + myplane.myhash.elevator * 0.15,


So I just want to know if there is a way to access member variables and functions from inside a hash (not a class instance) without specifying a global path.

Thank you in advance
Autowings
Autowings
 
Posts: 11
Joined: Thu Oct 31, 2019 9:09 am

Re: function call from sub hash

Postby jsb » Mon Feb 24, 2020 1:34 pm

Hi,

#1 does not work because there is no implicit resolution for the hash members in Nasal
#2 the "me" reference is only available if you call a function in a hash ; see also http://wiki.flightgear.org/Object_orien ... g_in_Nasal

You could add a function to your hash and call
Code: Select all
var myhash = {
...
    getMixer: func { return me.aileron * 0.2 + me.elevator * 0.15; },
};

print(myhash.getMixer());


hth
Henning
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux


Return to Nasal

Who is online

Users browsing this forum: No registered users and 5 guests