Board index FlightGear Development Nasal

Need help debugging my firework Nasal code  Topic is solved

Nasal is the scripting language of FlightGear.

Need help debugging my firework Nasal code

Postby TheEagle » Sun Dec 26, 2021 10:30 pm

I have this simple Nasal timer to shoot a firework up into the sky first (a translate animation on the height property), then run a particle system with a condition on the active property for the explosion:
Code: Select all
         fireworkNode = props.globals.getNode("/scenery/fireworks/firework[0]");
         heightNode = fireworkNode.getNode("height");
         activeNode = fireworkNode.getNode("active");
         
         var update = func() {
            activeNode.setDoubleValue(0);
            interpolate(heightNode, 500, 5);
            print(heightNode.getValue());
            heightNode.setDoubleValue(0);
            print(heightNode.getValue());

            print("1 ", activeNode.getValue());
            interpolate(activeNode, 1, 3);
            print("2 ", activeNode.getValue());
            activeNode.setDoubleValue(0);
            print("3 ", activeNode.getValue());
         }
         
         t = maketimer(10, update);
         t.singleShot = 0;
         t.simulatedTime = 1;
         t.start()

The code resides inside a <load> section in the firework's XML file.

This is the expected behaviour:
  1. active = 0
  2. height = 0
  3. height changes to 500 in 5 seconds
  4. height = 0
  5. active changes to 1 in 3 seconds (effectively, bool(active) is true for (almost) 3 seconds)
  6. active = 0

The height stays at 500 for ten seconds, jumps to 0 and immediately interpolates to 500 again. The active property is 1 all the time, then when height is 500 it jumps to 0 and changes linearly to 1 again where it stays until height is 500 again, where the thing begins again. I don't understand that. But the worst is - all three prints output a value of 0 for height all the time ! :shock: Please help quickly.

You can test the behaviour by pasting the code above into the Nasal console and observing the properties in /scenery/fireworks/firework - i.e. it's standalone.
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby ludomotico » Sun Dec 26, 2021 11:28 pm

interpolate does not wait for the interpolation to end. It starts a timer and returns immediately

So, after: interpolate(heightNode, 500, 5);

heightNode is still 0 (or whatever), it has not changed. You have to wait 5 seconds (but not inside your function!) for heightNode to be 500

It is the same with the other lines:

Code: Select all
 
print("1 ", activeNode.getValue());
interpolate(activeNode, 1, 3);
print("2 ", activeNode.getValue());
activeNode.setDoubleValue(0);
print("3 ", activeNode.getValue());


They will print:

1. whatever activeNode value is
2. whatever activeNode value is, and the same than step 1 because interpolate() has not changed the value yet
3. 0. Not sure how setting the value manually while an interpolation is running in the background affects the value, but it is most probably not a good idea
User avatar
ludomotico
 
Posts: 1269
Joined: Tue Apr 24, 2012 2:01 pm
Version: nightly
OS: Windows 10

Re: Need help debugging my firework Nasal code

Postby TheEagle » Mon Dec 27, 2021 12:04 am

With a separate timer for every thing that needs to be delayed it (about) works:
Code: Select all
      <load><![CDATA[
         fireworkNode = props.globals.getNode("/scenery/fireworks/firework[0]");
         heightNode = fireworkNode.getNode("height");
         activeNode = fireworkNode.getNode("active");
         riseTime = 3;
         explodeTime = 0.5;
         
         deactivateTimer = maketimer(explodeTime, func() {
            activeNode.setIntValue(0);
            heightNode.setDoubleValue(0);
         });
         deactivateTimer.singleShot = 1;
         deactivateTimer.simulatedTime = 1;
         
         heightTimer = maketimer(riseTime + .05, func() {
            activeNode.setIntValue(1);
            deactivateTimer.start();
         });
            
         heightTimer.singleShot = 1;
         heightTimer.simulatedTime = 1;
         
         var update = func() {
            interpolate(heightNode, 500, riseTime);
            heightTimer.start();
         }
         
         t = maketimer(10, update);
         t.singleShot = 0;
         t.simulatedTime = 1;
         t.start()
      ]]></load>
      <unload><![CDATA[
         t.stop();
         deactivateTimer.stop();
         heightTimer.stop();
      ]]></unload>

Thank you !
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby TheEagle » Mon Dec 27, 2021 1:42 am

Is it possible to get a property node from a params section inside the Nasal script ? I tried like this:
Code: Select all
   <params>
      <root>/scenery/fireworks/firework[0]</root>
      <height>/scenery/fireworks/firework[0]</height>
      <active>/scenery/fireworks/firework[0]</active>
   </params>
   <nasal>
      <load><![CDATA[
         fireworkNode = props.globals.getNode("../../params/root");

etc.

but that gave me an error "attempt to move past root with '..' node".
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby erik » Mon Dec 27, 2021 9:09 am

If this is your -set.xml file then <params> will be in the root of the property tree.

e.g.
fireworkNode = props.globals.getNode("/params/root");

Erik
Current: Parachutist, Paraglider, Pterosaur, Pilatus PC-9M and variants, ERCO Ercoupe, Fokker Dr.1, Fokker 50, Fokker 100
Less active: Cessna T-37, T-38, Santa Claus. Previous: General Dynamics F-16. Worked on: Wright Flyer
erik
 
Posts: 2245
Joined: Thu Nov 01, 2007 2:41 pm

Re: Need help debugging my firework Nasal code

Postby wkitty42 » Mon Dec 27, 2021 2:34 pm

TheEagle wrote in Mon Dec 27, 2021 1:42 am:but that gave me an error "attempt to move past root with '..' node".

this is one reason why relative pathing is or can be problematic... i learned a long time ago that, while it is longer to type, it is generally safer to use full paths when possible... that was back in my DOS batch file days when one 1500+ line .bat file controlled my BBS (and FTN mailer and tosser) operations...
"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: 9148
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04

Re: Need help debugging my firework Nasal code

Postby TheEagle » Tue Dec 28, 2021 2:26 am

Sorry erik, you got me wrong a bit … I was trying to get a property from the params section of a 3D model from inside it's <nasal><load> section.

Another question - if I load an XML model A from another XML model B, will A's <load> Nasal code get executed ? It doesn't seem to for me …
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby TheEagle » Tue Dec 28, 2021 2:36 am

@wkitty: you are right, but sadly you can't use absolute paths when aliasing an animation's property to one from the params section ! :wink:
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby jsb » Thu Dec 30, 2021 11:07 am

TheEagle wrote in Tue Dec 28, 2021 2:36 am:@wkitty: you are right, but sadly you can't use absolute paths when aliasing an animation's property to one from the params section ! :wink:

Yes you can
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
  <path>landing.ac</path>
  <params>
    <power-source>systems/DC/outputs/landing-lights</power-source>
    <x-m/>
    <y-m/>
    <z-m/>
  </params>
  <animation>
    <type>select</type>
    <condition>
      <greater-than>
        <property alias="/params/power-source"/>
        <value>22</value>
      </greater-than>
    </condition>
  </animation>
  <light>
    <name>Landing light wing</name>
    <type>spot</type>
    <position>
      <x alias="/params/x-m"/>
      <y alias="/params/y-m"/>
      <z alias="/params/z-m"/>
    </position>
...
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: Need help debugging my firework Nasal code

Postby jsb » Thu Dec 30, 2021 11:31 am

You should try something like this to see if you can access the parameters:
Code: Select all
    <nasal>
        <open><![CDATA[
            var root = cmdarg();
            debug.dump(root.getChildren());
        ]]>
        </open>
  </nasal>

If the dump shows something plausible, try root.getChild etc
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: Need help debugging my firework Nasal code  

Postby TheEagle » Thu Dec 30, 2021 9:13 pm

@jsb that won't work. In normal scenery models you can't use cmdarg - it always raises an error because it's not supported (scenery models don't have their own property tree). In a submodel included with <model><path>… this could maybe work, but the Nasal code of a submodel doesn't seem to get executed. So, actually the whole approach is senseless as getting a <params> property makes only sense in the submodel, where the Nasal code isn't executed. So I'll probably just simply make an AI scenario for next year. Thank you for trying to help.
Cessna 210 (Wiki)
My other aircraft: my wiki profile !
Other: FGTools (GitHub)
World tour: View on SkyVector
Please consider donating $1 / €1 to help me finance a new camera !
User avatar
TheEagle
 
Posts: 3413
Joined: Sat May 01, 2021 3:27 pm
Location: France
Pronouns: You, he
Callsign: F-EAGLE
IRC name: none
Version: Git next
OS: Ubuntu Studio 22.04

Re: Need help debugging my firework Nasal code

Postby erik » Fri Dec 31, 2021 9:11 am

It just occurred to me you should be able to set some aliases in Nasal itself:
Code: Select all
var root = props.globals.getNode("/sim/fireworks/root");
root.alias(props.globals.getNode("/scenery/fireworks/firework[0]");

Erik
Current: Parachutist, Paraglider, Pterosaur, Pilatus PC-9M and variants, ERCO Ercoupe, Fokker Dr.1, Fokker 50, Fokker 100
Less active: Cessna T-37, T-38, Santa Claus. Previous: General Dynamics F-16. Worked on: Wright Flyer
erik
 
Posts: 2245
Joined: Thu Nov 01, 2007 2:41 pm

Re: Need help debugging my firework Nasal code

Postby jsb » Fri Dec 31, 2021 3:27 pm

TheEagle wrote in Thu Dec 30, 2021 9:13 pm:@jsb that won't work. In normal scenery models you can't use cmdarg - it always raises an error because it's not supported (scenery models don't have their own property tree). In a submodel included with <model><path>… this could maybe work, but the Nasal code of a submodel doesn't seem to get executed. So, actually the whole approach is senseless as getting a <params> property makes only sense in the submodel, where the Nasal code isn't executed. So I'll probably just simply make an AI scenario for next year. Thank you for trying to help.


Sorry, I obviously missed that you are working on scenery stuff. The XML sample was taken from an aircraft where the lights are in submodels.
I like the idea of having fireworks in FG :)
happy new year
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: Need help debugging my firework Nasal code

Postby portreekid » Sat Jan 01, 2022 10:21 am

I have a POC for dialogs, but didn't need it. Scenery elements could be enabled in an similar fashion

https://sourceforge.net/u/portree_kid/f ... 10857afda/
portreekid
 
Posts: 651
Joined: Tue Jan 14, 2014 4:36 pm
Location: Leipzig
Callsign: PORTREE
Version: 2020.2.1
OS: Windows 10


Return to Nasal

Who is online

Users browsing this forum: No registered users and 4 guests