Board index FlightGear Development Nasal

A technical Nasal question

Nasal is the scripting language of FlightGear.

A technical Nasal question

Postby Thorsten » Tue Mar 09, 2010 4:20 pm

This may be stupid, but:

I would like to rotate an AI object from Nasal. For this purpose, the xml-file of the model gets the tag

Code: Select all
<animation>
 <type>rotate</type>
 <property>orientation/rotation-deg</property>
 <factor>1.0</factor>
 <axis>
  <x>1</x>
  <y>0</y>
  <z>0</z>
 </axis>
</animation>


I then can control the rotation just fine from Nasal by

Code: Select all
setprop("ai/models/aircraft[0]/orientation/rotation-deg",x);


where x is my rotation angle. Now, when I have multiple instances of the AI object,

Code: Select all
var modelNode = props.globals.getNode("ai/models", 1);
var aircraftNode = modelNode.getChildren("aircraft");
   
foreach (var e; aircraftNode) {
   var e_rot = e.getNode("orientation/rotation-deg");
             e_rot.setValue(x);}


is the more convenient way of doing it. But there is a problem, and after some experimentation I think I know what it is: orientation/rotation-deg as declared in the animation tag is only initialized once the object becomes visible. However, unlike setprop, (node).setValue does not simply create the node if there is nothing previously, but returns an error and crashes the Nasal script. So if the Nasal script happens to run while some instances of the AI models are not (yet) visible, the Nasal script exits.

I'm now looking for a clean solution to this - like declaring an extra property in the AI-scenario or the model xml wrapper file - but it doesn't seem to be straightforward (or I may just be too dumb). I can think of a few dirty workarounds (like using setprop a hundred times to initialize all nodes), but I'd rather avoid that.

Any help appreciated!
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: A technical Nasal question

Postby AndersG » Tue Mar 09, 2010 4:54 pm

Actually it is the getNode(path) method call that returns nil if the property node doesn't exist. To make getNode create the property if it doesn't exist call it like this: getNode(path, 1).

getNode is defined and documented in data/props.nas but the usage is almost certainly documented on the wiki as well.

/Anders
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: 2524
Joined: Wed Nov 29, 2006 10:20 am
Location: Göteborg, Sweden
Callsign: SE-AG
OS: Debian GNU Linux

Re: A technical Nasal question

Postby Thorsten » Wed Mar 10, 2010 9:46 am

Thanks - I appreciate the help!

I have read the documentation before posting here, however my problem is that it is clearly written for someone with a background different from mine - I understand perhaps half of it, so I have to learn Nasal by doing and occasionally asking questions :?
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: A technical Nasal question

Postby Thorsten » Sat Mar 13, 2010 12:44 pm

Not exactly Nasal, but too small to justify a new thread:

Is there a property node from which I can get the current view coordinates (i.e. not the plane coordinates, for e.g. tower view plane and view coordinates are rather different) and view direction vector, and if so, where is it?

Thanks in advance!
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: A technical Nasal question

Postby Hooray » Wed Apr 21, 2010 5:08 pm

Thorsten wrote:Is there a property node from which I can get the current view coordinates (i.e. not the plane coordinates, for e.g. tower view plane and view coordinates are rather different) and view direction vector, and if so, where is it?


Well, there is only the stuff that is in /sim/current-view which is based on the view configurations found in /sim/view[0] ... /sim/view[n] (configs based on http://wiki.flightgear.org/index.php/Ho ... FlightGear ).

But it should not be all that difficult to also expose additional internal data for a given view.
After all, all of this is already being calculated it just isn't yet exposed to the property tree, see $FG_SRC/Main/viewmgr.cxx for details.
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


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest