1
0
Fork 0

add initNode() function that initializes (if necessary) and returns a property.

If the property has a valid value already, then the given value is ignored.

  var x = props.initNode("/foo", 10);
  var y = props.initNode("/bar", 1, "BOOL");
This commit is contained in:
mfranz 2008-05-19 15:44:23 +00:00
parent 67ba396942
commit 8fe1cf807d

View file

@ -239,14 +239,33 @@ var nodeList = func {
list ~= nodeList(a[i]);
elsif(t == "func")
list ~= nodeList(a());
elsif(t == "ghost" and ghosttype(a) == ghosttype(props._globals()))
append(list, props.wrapNode(a));
elsif(t == "ghost" and ghosttype(a) == ghosttype(_globals()))
append(list, wrapNode(a));
else
die("nodeList: invalid nil property");
}
return list;
}
##
# Initializes property if it's still undefined. First argument is a property
# path or a props.Node. Second argument is the default value. The third,
# optional argument is a property type (one of "STRING", "DOUBLE", "INT", or
# "BOOL"). If it is omitted, then "DOUBLE" is used for numbers, and STRING
# for everything else. Returns the property as props.Node.
#
var initNode = func(prop, value, type = nil) {
if(!isa(prop, props.Node)) prop = props.globals.getNode(prop, 1);
if(prop.getType() != "NONE") value = prop.getValue();
if(type == nil) prop.setValue(value);
elsif(type == "DOUBLE") prop.setDoubleValue(value);
elsif(type == "INT") prop.setIntValue(value);
elsif(type == "BOOL") prop.setBoolValue(value);
elsif(type == "STRING") prop.setValue("" ~ value);
else die("initNode(): unsupported type '" ~ type ~ "'");
return prop;
}
##
# Evaluates a <condition> property branch according to the rules
# set out in $FG_ROOT/Docs/README.conditions. Undefined conditions