Passing data in events 
Author Message
 Passing data in events

Hello all,

How can i send arbitrary information with an event? e.g. my callback
function should report the name of the node that was clicked by the
user. Is there some way to send this data along with the event? Imagine
that i have 4 box shapes in my world, each with a different name, when a
box is clicked it should dump it's name to the console. (no this is no
homework exercise, i don't want a complete example just some hints in
the right direction).

TIA, Rob



Mon, 06 Oct 2003 18:51:43 GMT  
 Passing data in events
Quote:
> How can i send arbitrary information with an event?

every event includes itself and timestamp (in unix time).
using scripts u can inlude more data by using a different event type
e.g. if u want to send a 2D location (SFVec2f) , and an ID number (SFInt32)
, send it all in one SFVec3f using a Script.

Quote:
> e.g. my callback
> function should report the name of the node that was clicked by the
> user. Is there some way to send this data along with the event? Imagine
> that i have 4 box shapes in my world, each with a different name, when a
> box is clicked it should dump it's name to the console. (no this is no
> homework exercise, i don't want a complete example just some hints in
> the right direction).

how are your boxes 'named', if it only by DEF <name> then it is of no use.
the node itself cannot find out if it is being referenced by a DEF (or by
what name it is being referenced). the name would need to be mapped to a
custom Box PROTO, e.g.
PROTO MyBox [
  field SFVec3f size 2 2 2
  field SFString name "default"     // one of name or ID, ID can usualy be
more usefull in a scripts.
  field SFInt32 ID 0
  eventIn SFTime request_info
  eventOut MFVec3f sizeID_out
  eventOut SFString name_out
]
{
  Box {
    size IS size
  }
  DEF sc Script {
    field SFVec3f size IS size
    field SFString name IS name
    field SFInt32 ID IS ID
    eventIn SFTime request_info IS request_info
    eventOut MFVec3f sizeID_out IS sizeID_out
    eventOut SFString name_out IS name_out
    url "javascript:
      function request_info(sftime,tm){
        sizeID_out = new MFVec3f(size,new SFVec3f(ID,0,0));
        name_out = name;
      }
    "
  }
Quote:
}

db


Mon, 06 Oct 2003 19:46:47 GMT  
 Passing data in events
David,

Thanks for the extensive example, but i'm still confused about how to
ROUTE the events to the script in a PROTO node. Let's say i replace the
request_info function with a simple function: say_hello (arg1, arg2) {
print('my name: ' + name); }. Then I declare a new MyBox, grouped with a
TouchSensor, e.g. Group { children [ DEF ABOX MyBox {}; DEF ASENSOR
TouchSensor {} ] }.
Then how should i ROUTE events? something like ROUTE ASENSOR.touchTime
TO ABOX.sc.say_hello ?

Forgive my ignorance, and again TIA,
Rob

Quote:

> > How can i send arbitrary information with an event?
> every event includes itself and timestamp (in unix time).
> using scripts u can inlude more data by using a different event type
> e.g. if u want to send a 2D location (SFVec2f) , and an ID number (SFInt32)
> , send it all in one SFVec3f using a Script.
> > e.g. my callback
> > function should report the name of the node that was clicked by the
> > user. Is there some way to send this data along with the event? Imagine
> > that i have 4 box shapes in my world, each with a different name, when a
> > box is clicked it should dump it's name to the console. (no this is no
> > homework exercise, i don't want a complete example just some hints in
> > the right direction).

> how are your boxes 'named', if it only by DEF <name> then it is of no use.
> the node itself cannot find out if it is being referenced by a DEF (or by
> what name it is being referenced). the name would need to be mapped to a
> custom Box PROTO, e.g.
> PROTO MyBox [
>   field SFVec3f size 2 2 2
>   field SFString name "default"     // one of name or ID, ID can usualy be
> more usefull in a scripts.
>   field SFInt32 ID 0
>   eventIn SFTime request_info
>   eventOut MFVec3f sizeID_out
>   eventOut SFString name_out
> ]
> {
>   Box {
>     size IS size
>   }
>   DEF sc Script {
>     field SFVec3f size IS size
>     field SFString name IS name
>     field SFInt32 ID IS ID
>     eventIn SFTime request_info IS request_info
>     eventOut MFVec3f sizeID_out IS sizeID_out
>     eventOut SFString name_out IS name_out
>     url "javascript:
>       function request_info(sftime,tm){
>         sizeID_out = new MFVec3f(size,new SFVec3f(ID,0,0));
>         name_out = name;
>       }
>     "
>   }
> }
> db



Mon, 06 Oct 2003 22:05:43 GMT  
 Passing data in events

Quote:
> David,

> Thanks for the extensive example, but i'm still confused about how to
> ROUTE the events to the script in a PROTO node. Let's say i replace the
> request_info function with a simple function: say_hello (arg1, arg2) {
> print('my name: ' + name); }. Then I declare a new MyBox, grouped with a
> TouchSensor, e.g. Group { children [ DEF ABOX MyBox {}; DEF ASENSOR
> TouchSensor {} ] }.
> Then how should i ROUTE events? something like ROUTE ASENSOR.touchTime
> TO ABOX.sc.say_hello ?

> Forgive my ignorance, and again TIA,
> Rob

it is only ignorance if you do not ask when u do not know.
firstly your simple Group node would not display anything...
a critical part of how a PROTO works is defined in the vrml 97
specification:

" An instantiation is created by filling in the parameters of the prototype
declaration and inserting copies of the first node (and its scene graph)
wherever the prototype instantiation occurs. For example, if the first node
in the prototype definition is a Material node, instantiations of the
prototype can be used wherever a Material node can be used." (from section
4.8.3)

to you, this means that whenever you create a MyBox instance in code (as you
did with your example), it is like inserting the first node of the PROTO
definition into the code (in this case it is just a simple Box {} node). now
depending on how much VRML you have already done u may realise that a Box {}
node itself, does not render, it must be included as the geometry field of a
Shape {} node, so your code should look something like this:
Group {
  children [
    Shape {
      appearance Appearance {
        material Material {
        }
      }
      geometry DEF abox MyBox {
      }
    },
    DEF ts TouchSensor {
    }
  ]

Quote:
}

just one thing i noticed in your code (apart from a significant lack of
indenting ;) was the use of a semicolon ';' between nodes in the child
field. i don't think this is correct vrml, most browsers will read code
without any seperator between nodes in an MFNode field, but the standard is
to use a comma ',' i think..

your next note about changing the function is not so easy. functions defined
inside the url field of a Script node are useless unless they are called
'initialize' or have the same name as an eventIn to that function (whenever
the Script node receives an event, it calls the function of that name). so
if u replaced the function with one called say_hello, then you would need to
add the line:
eventIn SFTime say_hello IS say_hello
to the Script definition, and you would need to add the line:
eventIn SFTime say_hello
to the PROTO interface definition. (you would also probably remove the
'eventIn SFTime request_info' and 'eventIn SFTime request_info IS
request_info).
the easy way would be to keep the same function name, and just change the
code inside it, then u wouldn't have to change all the eventIn definitions.
also if u changed the content of the request_info function to a simple print
statement, then the eventOuts (sizeID_out, name_out) and the ID field are
not used so you could remove there references in the PROTO and Script
interfaces.

so theres the setup, now the ROUTE. you have to think of a PROTO like a
black box, just like any other node. the only interaction you have with this
box is via the fields and events defined in the PROTO interface (the stuff
in between the square brackets after the 'PROTO <name>' section). so your
ROUTE would be as simple as:
ROUTE ts.touchTime TO abox.request_info
or
ROUTE ts.touchTime TO abox.say_hello
(depending on if u changed the function..)
so heres the total code:

#VRML V2.0 utf8

PROTO MyBox [
  field SFVec3f size 2 2 2
  field SFString name "default"
  eventIn SFTime say_hello
]
{
  Box {
    size IS size
  }
  DEF sc Script {
    field SFVec3f size IS size
    field SFString name IS name
    eventIn SFTime say_hello IS say_hello
    url "javascript:
      function request_info(sftime,tm){
        print('my name: '+name);
      }
    "
  }

Quote:
}

Group {
  children [
    Shape {
      appearance Appearance {
        material Material {
        }
      }
      geometry DEF abox MyBox {
      }
    },
    DEF ts TouchSensor {
    }
  ]
Quote:
}

ROUTE ts.touchTime TO abox.say_hello


Tue, 07 Oct 2003 08:52:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pass data between subVIS using Event structures.

2. event bindings which pass the event himself first..?

3. event generate options, virtual events and client data

4. Event case loses connection to data structure when data structure is modified

5. Passing DLL events to parent frame?

6. Passing DLL events to parent frame

7. GUI/event loop programming is Continuation Passing - paper

8. Passing Javascript/VRML events between frames

9. Info passed thru 'Event'

10. Info passed thru 'Event'

11. Event Binding and Variable Passing

12. Passing asyncronous events to Tcl

 

 
Powered by phpBB® Forum Software