I've now turned props.initNode() into a props.Node method.
initNode is a rather new function that creates (if necessary)
and returns a node initialized (if necessary) to the given
value and set (if necessary) to the given type. If the node
does already have a type, then the type won't be changed.
And if the node does already have a value, then the value
won't be set. (Do that explicitly if necessary!)

This allows to override node value/type from the command
line, and initNode will respect and keep value and type:

  --prop:double:foo/bar=123   or  --prop:d:foo/bar=123



initNode() has two optional arguments: value (scalar) and
type (one of "STRING", "DOUBLE", "INT", or "BOOL"). 

  var x = props.globals.initNode("/foo/bar", 45, "INT");
  var y = x.initNode("child[0]", "test", "STRING");
  var z = x.initNode("child[1]", 1, "BOOL");

The first line will make /foo/bar an "INT" node of value 45,
the second will make /foo/bar/child[0] a "STRING" node, etc.



The type can be left away, in which case either "DOUBLE"
will be used if the value is a number, or "STRING"
otherwise:

  var a = z.initNode("whatever", 123);   # -> "DOUBLE"
  var b = z.initNode("herbert", "gaga"); # -> "STRING"
  var c = z.initNode("fluffy", "123");   # -> "STRING"



The value can be left away, too. Then 0 will be assumed.
That is, if node /foo/bar didn't exist yet, then

  var x = props.globals.initNode("/foo/bar");

is equivalent to

  var x = props.globals.getNode("/foo/bar", 1);
  x.setDoubleValue(0);

That's because we most often initialize (FDM) values
to DOUBLE 0, so this is quite convenient. initNode()
is, of course, slower than getNode(), so it should
really only be used for initialization purposes, not
in high-frequency loops.

Also don't check in a high-frequency loop whether a
particular property exists and is initialized! Just
initialize it properly, and this check is superfluous.
Once given value and type the node will not suddenly
become "NONE"/nil again.[0]

m.


[0] OK, that's a lie. One *can* turn node types into
    "NONE" again, with value nil. But just assume it
    is never done to your nodes, and if so, an error
    message should be exactly what you want.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to