Board index FlightGear Development Nasal

Begin to study nasal & canvas

Nasal is the scripting language of FlightGear.

Begin to study nasal & canvas

Postby atm0sphere » Mon Dec 18, 2017 8:00 am

Hello, my dear friends, i have problem with nasal script.
I do not have any programming skills and I'm learning from various examples in free access. I enclose a simplified version of the script.

Code: Select all
var flydatinstr = {
      new: func()
      {
        debug.dump("start ...");
        var m = { parents: [flydatinstr] };

        m.canvas = canvas.new({
          "name": "flydat",   
          "size": [596, 142],
          "view": [1366, 768],                       
          "mipmapping": 1       
        });

        # in .ac file
        m.canvas.addPlacement({"node": "screenflydat"});
        m.canvas.setColorBackground(0.8,1.2,0.2);
       
        # variables       
        m.RPM = props.globals.getNode("engines/engine/rpm");
        #... and other

        #____
        var g = m.canvas.createGroup();

        # output format
       
        m.RPMtext =
        g.createChild("text", "tt")
                .setTranslation(425, 310)     
                .setAlignment("right-center")
                .setFont("LiberationFonts/flydat.ttf")
                .setFontSize(125, 1.65)       
                .setColor(0,0,0);                           

        return m;       
      },
     
      update: func()
      {
        me.RPMtext.setText(sprintf("%i", me.RPM.getValue()));       
        settimer(func me.update(), 1);
      },
           
      # Destructor
      del: func
      {
        # ...
      },
};

# upd
       
setlistener("/nasal/canvas/loaded", func {
    var u = flydatinstr.new();
    u.update();
}, 1);



Help me, please, I puzzle my mind for a long time and I just can not understand. Re-read a lot "How to do" ...I tried various ways to add a condition so that by its results this script either worked or stopped working. As I understand it, I need to write a destructor. But I can not do it right. Help me please. Thank you. Sorry, google translation :)
atm0sphere
 
Posts: 3
Joined: Mon Dec 18, 2017 6:57 am
Location: Siberia
Version: 2016.3.1
OS: Win7.64

Re: Begin to study nasal & canvas

Postby Hooray » Fri Dec 22, 2017 8:43 am

Hi & welcome,

maybe I am missing something, but could you briefly explain what exactly you want to do ?
If in doubt, just use very short sentences when using google translate - for example, not more than 5-6 words to describe, and very simple words in your own language (that way, the google translation will be much better).

My suggestion would be that you tell us what you would like to implement, and then tell us what you expect to see happen, and what you really see happening instead.
You could also post screenshots/youtube videos or other images.

It may also be helpful to add a few comments to your code explaining what you are trying to do.

Alternatively, you could also post in your native language - this community has members from all over the world, and usually someone will show up to to help translate questions/answers.
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: Begin to study nasal & canvas

Postby atm0sphere » Mon Dec 25, 2017 11:33 am

Oh, thx for answer, it's important to me :)

Ok, its my panel without script with canvas
pic1
Image

I have variable "/instrumentation/FLYdat/servicable", when I change it from 0 to 1 - i have:
pic2
Image

Buuuut, when I change again my variable it from 1 to 0 - I can not go back to the picture 1.

I just do not know what I need to write, I need switch off canvas... If it's a destructor, then I just do not know how to write it correctly.


Code: Select all
setlistener("/instrumentation/FLYdat/servicable", func {
    if ( getprop("/instrumentation/FLYdat/servicable") )
    {     
      var u = flydatinstr.new();
      u.update();
      debug.dump("servicable 1");
     
    }else{
      var g = flydatinstr.new();
      g.del();
      debug.dump("servicable 0"); 
    } 
}, 2);


Sorry for the stupid questions, I'm learning how I can by examples))
atm0sphere
 
Posts: 3
Joined: Mon Dec 18, 2017 6:57 am
Location: Siberia
Version: 2016.3.1
OS: Win7.64

Re: Begin to study nasal & canvas

Postby Hooray » Mon Dec 25, 2017 12:22 pm

there is nothing really wrong with your code here - the main problem is that you are mixing up ideas/concepts here.

A "destructor" is a function that is intended to DESTROY an object, such as the instrument/canvas representing it.
However, the "serviceable" flag/property is usually intended to merely determine if an instrument is intended to work properly or if it failed.
As such, I would not actually invoke a destructor to DESTROY the object representing the canvas/instruments - instead, I would simply stop updating the instrument or do some actual failure simulation instead.

What you are currently doing in your code is registering a listener, and checking if the instrument is sevicable: if it is, you are creating a new instance of the update, and update that.
If it isn't serviceable, you are creating a new object that you are deleting immediately afterwards (which doesn't make any sense at all)
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: Begin to study nasal & canvas

Postby atm0sphere » Mon Dec 25, 2017 1:04 pm

Yes, yes, I noticed that I'm calling the update again. Its true..

That is, I need to add a condition to "settimer (func me.update (), 1);"?

And "some actual failure simulation" how does this work? Or can you tell me where I can see an example?
atm0sphere
 
Posts: 3
Joined: Mon Dec 18, 2017 6:57 am
Location: Siberia
Version: 2016.3.1
OS: Win7.64

Re: Begin to study nasal & canvas

Postby Hooray » Tue Dec 26, 2017 10:07 am

Honestly, just stop calling the constructor/destructor as part of the listener callback - instead, create the object at the start of the code, and destroy it at the end (use a separate function for that, or it will be called sooner or later, even though you may not want that to happen!).

For handling the "serviceable" flag, all you need is telling the update timer to stop by setting a variable that you check in your loop body, i.e. prior to calling settimer again.
Otherwise, you are making this more complicated than needed, and looking for code because you think you have the solution, whereas you should actually be re-thinking your current solution/approach instead - i.e. when a device stops working, it does not magically "disappear" (which is however equivalent to removing/destroying it).
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


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests