WHERE

From HEWIKI
Jump to: navigation, search
This command identifies a node's available classes. For the branching command, see WHEN.

WHERE <NodeRef>
  IS <relation>
    (commands)
  .
  IS <relation>
    (commands)
  .
  DEFAULT
    (commands)
  .
.

WHERE <NodeRef> IS <relation>
  (commands)
.

Arguments

<NodeRef> 
A node reference variable to the node that is being checked.
<relation> 
IS KINDOF <classname>  
Will match if either the base or glommed classes of NodeRef, or any of the classes that those classes inherit from, are the same as <classname>.
IS EXACTLY <classname>  
Will match if either the base or glommed classes of <NodeRef> match <classname>. Will not match on inherited classes.
IS CLASS OF <target_node> 
Not currently implemented -- Will match if either the base or glommed classes of NodeRef match any of the base or glommed classes of target_node

Overview

When dealing with a particular node-reference variable, it is not possible for HeroScript to access any of the fields on that variable until its class has first been declared. A node's class can be declared at the time that it is defined (n as NodeRef of class saladbar), or the WHERE command can be used to declare what classes that the node has available.

Node Classes

There are different ways that a class can be associated with a node.

It is also important to note that the above categories are not mutually exclusive. It would be possible, for example, for a class to be a parent class and a glommed class at the same time. Please see the bottom of this page for an example.

Syntax

The command is in a multi-clause format, and as many of the different clauses available can be mixed and matched as are needed:

WHERE <NodeRef>
  IS EXACTLY <class>
    (commands)
  .
  IS EXACTLY <class>
    (commands)
  .
  IS KINDOF <class>
    (commands)
  .
  DEFAULT
    (commands)
  .
.

HeroScript will check <NodeRef> against each of the clauses in turn, until it finds the first one that evaluates as TRUE, in which case that clause's code is executed, and all other clauses are skipped. If none of the clauses evaluate as TRUE, then the code in the optional DEFAULT clause is executed.

Each of the IS clauses must be closed with a period. The entire WHERE structure must also be closed with its own period.

HSL also provides for an in-lined syntax that is commonly useful:

WHERE <NodeRef> IS <relation>
  (commands)
.


Examples

where n
  is exactly dagger
    println(n.knifeFactor)
  .
  is kindof weapon
    println(n.attackRating)
  .
  default
    println("hello there")
  .
.


To modify a field on a sword node, first it must have its class declared:

sword as NodeRef             // sword is declared as a NodeRef, but not which class it is yet
where sword
  is exactly Flaming         // now we know its class, and can adjust a field
    sword.burnrate = 3
  .
  is kindof Glowing
    (commands)
  .
  is exactly Wet
    (commands)
  .
  default
    (commands)
  .
.

n as Noderef
where n is kindof GUIlabel
  n.text = "hi there"    // The "text" field could not be set until the
                         // class of n had been defined with the WHERE
.

It is also worth noting that a WHERE statement needs to be used to declare a node's class, even if it was already seemingly declared with a GlomClass() function (though this may change in the future):

n as NodeRef                 
GlomClass(n, "MyClass")        // Add the class "myClass" to n
where n is kindof MyClass      // If the node that n points to has a "myClass" type...
  n.value = 200                //  do stuff
.


For the next example, make the following assumptions:


You could then check carrot_node by checking for its base class, its parent class(es), or whether or not it matches the class of other nodes.

where carrot_node 
  is exactly vegetable
    // This would be TRUE   
  .
  is exactly Saladbar
    // FALSE
  .
  is kindof Saladbar
    // TRUE
  .
  is kindof Fruit
    // FALSE (though both fruit and vegetable  have the same parent, saladbar, a vegetable is not a kind of fruit)
  .
  is classof celery_node
    // TRUE
  .
  is classof utensils_node
    // FALSE
  .
  default
  .
.

Note: If the above example were actual code, it would only proceed to the first check that returned TRUE.

Lastly, here's an interesting example of CLI commands that shows how a node could have the same class attached in two different ways:

: CCD BaseClass, data;                         // Create a class, BaseClass
: CCD StartingNodeClass, data; BaseClass       // Create the class StartingNodeClass, which inherits from : BaseClass
: CNFC StartingNodeClass                       // Create a node (call it ID #3) from StartingNodeClass
: MNAC 3, BaseClass                            // Add BaseClass to the node

In the above case, the node ends up BaseClass for 2 reasons

Both of these conditions would affect the result of a WHERE statement. The node would always return a TRUE for a KINDOF check, because of the inheritance. But it would only return TRUE on IS EXACTLY when the class had also been glommed!

where 3
  is kindof BaseClass 
    // TRUE
  .
  is exactly BaseClass
    // Only true after the MNAC statement
  .
.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox