Creating new instances

When you start an Urbi server, an object of each class registered with UStart is created with the same name as the class. New instances can be created from Urbi using the new command. For each instance created in Urbi, a corresponding instance of the Java object is created. You can get the arguments passed to the constructor by defining and binding a method named init with the appropriate number of arguments.

For example let's add an Urbi constructor to our Adder class. We rewrite it as follow:

public class Adder extends UObjectJava // must extends UObjectJava
{
    /// Declare a variable v that will be accessible in Urbi
    private UVar v = new UVar ();

    /// Constructor
    public Adder (String s) {
    	super (s);

    	try
    	{
	    UBindFunction (this, "init");
    	}
    	catch (Exception e)
    	{
	    System.out.println (e);
    	}
    }

    /// The init function is the constructor in Urbi. Here it takes
    /// one argument that we use to initialise the 'v' variable.
    /// The init function must return an int of value 0
    /// if all went OK.
    public int init (UValue v_init) {

    	try
    	{
	    /// Bind the variable v to Urbi
	    UBindVar (v, "v");

	    /// Initialise our UVar v to the value given in the
	    /// constructor
	    v.set(v_init.getDouble ());

	    /// Bind the function add to Urbi
	    UBindFunction (this, "add");

    	}
    	catch (Exception e)
    	{
	    System.out.println (e);
    	}

	return 0;
    }

    /// The binded function can take no other type than UValue as
    /// parameters.
    public double add (UValue rhs) {
    	/// Convert the UValue to a double
    	double drhs = rhs.getDouble ();
    	/// Return the value of our UVar v (converted to double)
    	/// plus the value of the argument of the function.
    	return v.getDouble () + drhs;
    }
}

Now 'v' and 'add' are binded only when instance of the Adder object are constructed. We have added an 'init' constructor with one parameter that we use to initialise the value of v. You can run this UObject and test it in Urbi to see the difference with the previous example. Here is what it gives:

[00006775:start] *** **********************************************************
[00006775:start] *** URBI Language specif 1.0 - Copyright (C) 2005-2008 Gostai SAS
[00006775:start] *** URBI Kernel version 1.5 rev. 785a811
[00006775:start] ***
[00006775:start] ***   URBI Engine version 1.5 rev. 1cf8ce9
[00006775:start] ***      (C) 2006-2007 Gostai SAS
[00006775:start] ***
[00006775:start] *** URBI comes with ABSOLUTELY NO WARRANTY;
[00006775:start] *** This software can be used under certain conditions;
[00006775:start] *** see LICENSE file for details.
[00006775:start] ***
[00006775:start] *** See http://www.urbiforge.com for news and updates.
[00006775:start] *** **********************************************************
[00006775:ident] *** ID: U135538712
Adder,
[00014070] OBJ [load:1]
Adder.v, /// v is not binded yet
[00023204] !!! 2.1-7: Unknown identifier: Adder.v
[00023204] !!! 2.1-7: EXPR evaluation failed
Adder.add (42), /// add is not binded yet
[00028323] !!! 3.1-14: Error with function eval: Adder.add [nb param=1]
[00028323] !!! 3.1-14: EXPR evaluation failed
adder_instance = new Adder (13);
adder_instance, /// 'v' and 'add' are binded by the constructor, and v is initialised
[00066827] OBJ [v:13,load:1]
adder_instance.add (10);
[00081980] 23
adder_instance_2 = new Adder (43);
adder_instance_2,
[00110515] OBJ [v:43,load:1]
adder_instance,
[00113377] OBJ [v:13,load:1]