Difference between revisions of "SHIP:Sail:getChild"

From Serious Documentation
Jump to: navigation, search
(Prototype)
(Prototype)
Line 7: Line 7:
 
== Prototype ==
 
== Prototype ==
  
<code>{{DataType|Node}} node = getChild({{DataType|Node}} parentNode, {{DataType|Integer}} index);</code>
+
<code>{{DataType|Node}} getChild({{DataType|Node}} parentNode, {{DataType|Integer}} index);</code>
  
 
=== Parameters/Return Value ===
 
=== Parameters/Return Value ===

Revision as of 10:41, 5 December 2012

SHIP Sail Reference Home

getChild

Gets a child node object from within an enclosing parent node, by index.

Prototype

Node getChild(Node parentNode, Integer index);

Parameters/Return Value

Parameter Data Type Description
parentNode Node The node to look in for children
index Integer The index of the child desired in the parent
Return Node nth node within the parentNode's children

Detailed Description

The getChild(parent, index) function returns the direct child at index within the parent.

Nodes in SHIP may have 0 or more direct child nodes. For example, a group in the resources area may have N images within the group. These children, if present, are implicitly numbered 0..(N-1) where N is the total number of children of any type within the parent node.

The index is always taken "modulus the count of children within the parent", preventing inadvertent out-of-bounds accesses. For example, a reference to index 5 when the parent only has 4 children will return 5%4 => 1. Negative indices are handled also with a modulus. Therefore, as expected, index -1 returns the last child.

If the specified parent node has no children, the parent object itself is returned by the function.

Examples