Board index FlightGear Development Nasal

Set a basic property

Nasal is the scripting language of FlightGear.

Set a basic property

Postby Avionyx » Mon Oct 01, 2018 5:20 pm

Hi All,

Trying my first venture in to nasal, I'd like to set a property for the radio power to be on if 2 values are true and one false.

Could someone help please - I'd love to learn.

Code: Select all
#Radio Power Logic
var radiopower = {
   update : func {
        var batswitch = getprop("/controls/electric/directbat-switch");
        var generator = getprop("/controls/electric/engine/generator");
        var running = getprop("/engines/engine/running");
        if ((batswitch != false) and (generator != true) and (running != false) {
      setprop("/controls/electric/radiopower", "true");}}
}

Thanks in advance!
Avionyx
 
Posts: 434
Joined: Mon Jan 11, 2010 3:07 pm
Location: EGKA
Callsign: G-AVYX
Version: GIT
OS: Arch

Re: Set a basic property

Postby Necolatis » Mon Oct 01, 2018 6:02 pm

This line:

if ((batswitch != false) and (generator != true) and (running != false) {

is missing a parentess at the end.

"true" should not be in quotation marks, then nasal thinks its a string. In fact its better to use just 1 or 0 for true and false.

You probably also want an else statement. So that if the above is not the case then turn the radio off.

Code: Select all
#Radio Power Logic
var radiopower = {
   update : func {
        var batswitch = getprop("/controls/electric/directbat-switch");
        var generator = getprop("/controls/electric/engine/generator");
        var running = getprop("/engines/engine/running");
        if (batswitch == 1 and generator == 0 and running == 1) {
            setprop("/controls/electric/radiopower", 1);
        } else {
            setprop("/controls/electric/radiopower", 0);
        }
    },
}
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1941
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2019.1.2
OS: Windows 10

Re: Set a basic property

Postby Avionyx » Tue Oct 02, 2018 7:01 am

Hi Necolatis,

Thank's for such a fast reply.
I've given that a go and I'm getting no errors anymore but it's also not setting the properties even though the criteria has been met.
To keep it simple I stripped it back to 2 properties and tried that but again the same results, no errors and no properties set.

Here's the snippet I'm using and a screenshot from in sim with the Nasal Console open.


Code: Select all
#Radio Power Logic
var radiopower = {
   update : func {
        var batswitch = getprop("/controls/electric/directbat-switch");
        var generator = getprop("/controls/electric/engine/generator");
        if (batswitch == 1 and generator == 1) {
            setprop("/controls/electric/radiopower", 1);
        } else {
            setprop("/controls/electric/radiopower", 0);
        }
    },
}



[code]
(Thumbnail, click to view larger size)
Image

I really appreciate any help with this, I'd absolutely love to harness the power of Nasal.

Alex
Avionyx
 
Posts: 434
Joined: Mon Jan 11, 2010 3:07 pm
Location: EGKA
Callsign: G-AVYX
Version: GIT
OS: Arch

Re: Set a basic property

Postby Necolatis » Tue Oct 02, 2018 8:18 am

That looks good.
Might be a stupid question, but are calling this in a loop, with a timer or listener?
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1941
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2019.1.2
OS: Windows 10

Re: Set a basic property

Postby Avionyx » Tue Oct 02, 2018 8:22 am

This is probably where my lack of knowledge isn't helping.
I'd assumed it would need to be a listener but I'm not sure how to set it properly in this instance.

Let's say I wanted to (rather wastefully) set this as a standalone .nas file.
What would I need to include to get the above code operating as expected? A listener on the property I'm setting or one for each that we're using in the AND statement?

Many thanks for your help so far.

Alex
Avionyx
 
Posts: 434
Joined: Mon Jan 11, 2010 3:07 pm
Location: EGKA
Callsign: G-AVYX
Version: GIT
OS: Arch

Re: Set a basic property

Postby Necolatis » Tue Oct 02, 2018 8:37 am

Code: Select all
#Radio Power Logic
var radiopower = {
   update : func {
        var batswitch = getprop("/controls/electric/directbat-switch");
        var generator = getprop("/controls/electric/engine/generator");
        if (batswitch == 1 and generator == 1) {
            setprop("/controls/electric/radiopower", 1);
        } else {
            setprop("/controls/electric/radiopower", 0);
        }
    },
}

setlistener("/controls/electric/directbat-switch", func {radiopower.update();});
setlistener("/controls/electric/engine/generator", func {radiopower.update();});


You will also need to add the nas file to the aircraft, you know how to do that?
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1941
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2019.1.2
OS: Windows 10

Re: Set a basic property

Postby Avionyx » Tue Oct 02, 2018 8:55 am

I do , thank you!

Should this work in the nasal console? I'm getting a parse error for:
Code: Select all
var radiopower = {
Avionyx
 
Posts: 434
Joined: Mon Jan 11, 2010 3:07 pm
Location: EGKA
Callsign: G-AVYX
Version: GIT
OS: Arch

Re: Set a basic property

Postby Necolatis » Tue Oct 02, 2018 11:11 am

Oh, do a ; after the ending bracket, sorry.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore


Hangar: https://sites.google.com/site/fghangar/
User avatar
Necolatis
 
Posts: 1941
Joined: Mon Oct 29, 2012 12:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2019.1.2
OS: Windows 10

Re: Set a basic property

Postby Avionyx » Tue Oct 02, 2018 4:22 pm

Huge thanks for your help my friend.
All working and now I'm ready to tackle more Nasal.

Really appreciate the time you put into this for me.

All the best,
Alex
Avionyx
 
Posts: 434
Joined: Mon Jan 11, 2010 3:07 pm
Location: EGKA
Callsign: G-AVYX
Version: GIT
OS: Arch


Return to Nasal

Who is online

Users browsing this forum: No registered users and 0 guests

cron