This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > VRML > December 2004 > Scripting problem....





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Scripting problem....
bensmyth

2004-12-21, 7:14 am

I'm trying to develop an OO javascript solution but cannot seem to manage to
call methods from one script from the other.... Here's a simple example:

#VRML V2.0 utf8

DEF s2 Script {
eventOut SFBool c

url "javascript:
function c() {
print(3); //DOES NOT GET OUTPUT
}"
}

DEF s1 Script {
field SFNode s2 USE s2

url "javascript:
function initialize() {
print(1); //GETS OUTPUT
s2.c();
}"
}


CORTONA ERROR MESSAGE: s2.c is not a function


Any ideas?


Cheers,

Ben


psi

2004-12-21, 12:16 pm

i never actually thought of trying that, i always assumed routes were
the 'correct'/'only' way to send data between nodes.

simon.

bensmyth

2004-12-21, 12:16 pm

> i never actually thought of trying that, i always assumed routes were
> the 'correct'/'only' way to send data between nodes.


Seems to make sense to me.... That is.... assuming I can get it to work!!


Richard Kennaway

2004-12-21, 12:16 pm

In article <cq8u3t$1q35$1@soapbox.cs.bham.ac.uk> bensmyth,
noreply@test.com writes:
>I'm trying to develop an OO javascript solution but cannot seem to manage to
>call methods from one script from the other.... Here's a simple example:
>

[snipped]
>
>CORTONA ERROR MESSAGE: s2.c is not a function
>
>
>Any ideas?


Scripts can't call each other's functions. Functions are only in scope
within the script they occur in.

Instead of
s2.c();
try
s2.c = TRUE;
and also add the following field to s1:
directOutput TRUE
You need that in order to be able to assign directly to an exposedField
or eventIn of a node. Speaking of which, in s2, c needs to be declared
as an eventIn, not an eventOut. As written, the function c() has nothing
to do with the eventOut c. If c is an eventIn, the function c() will be
called whenever an event is received on c. c() should also take two
arguments, the value of the event and the timestamp of the event.

--
Richard Kennaway, jrk@cmp.uea.ac.uk, http://www.cmp.uea.ac.uk/~jrk/
School of Computing Sciences,
University of East Anglia, Norwich NR4 7TJ, U.K.
bensmyth

2004-12-21, 7:18 pm

> Scripts can't call each other's functions. Functions are only in scope
> within the script they occur in.
>
> Instead of
> s2.c();
> try
> s2.c = TRUE;


Unfortunately that doesn't really achieve what I want.....

If you consider the following script, I would like to have three files:
main.js, Item.js and ItemArray.js (it should be obvious what code goes
where)...

Based on what you have said... is that possible?

function initialize() {
var i1 = new Item(1,2,3);
var i2 = new Item(4,5,6);
var i3 = new Item(7,8,9);

var itemArray = new ItemArray();
itemArray.addItem(i1);
itemArray.addItem(i2);
itemArray.addItem(i3);

print(i3.x);
print(itemArray.getItem().x);
}


function Item(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}


function ItemArray() {
this.items = Array(5);

this.addItem = addItem;
this.getItem = getItem;
}

function addItem(item) {
this.items.push(item);
}

function getItem() {
return this.items.pop();
}



Regards,

Ben


psi

2004-12-22, 12:16 pm

i think the problem here is conceptual, you're thinking of the js files
as objects, in vrml its the Nodes that are your objects, the js file is
like a container for the methods.

i've seen various articles lamenting the naming of javascript, (
marketing men trying to jump on a bandwagon.) javascript is
fundamentally different to java.
see;

http://www.mozilla.org/js/

simon.

bensmyth

2004-12-22, 7:19 pm

> i think the problem here is conceptual, you're thinking of the js files
> as objects, in vrml its the Nodes that are your objects, the js file is
> like a container for the methods.
>
> i've seen various articles lamenting the naming of javascript, (
> marketing men trying to jump on a bandwagon.) javascript is
> fundamentally different to java.
> see;


Javascript can however be used to create objects.....

http://wp.netscape.com/eng/mozilla/...cript/model.htm



Example:

function MyObject() {
this.value = 1;

this.getValue = getValue;
this.setValue = setValue;
}

function getValue() { return this.value; }

function setValue(v) { this.value = v; }

function initialize() {
var myObject = new MyObject();

var i;

i = myObject.getValue(); //i == 1

myObject.setValue(5);

i = myObject.getValue(); //i == 5
}


Regards,

Ben


Joerg Scheurich aka MUFTI

2004-12-22, 7:19 pm

> i've seen various articles lamenting the naming of javascript, (
> marketing men trying to jump on a bandwagon.) javascript is
> fundamentally different to java.


Therefore the name of the language you are refering to is named
"ECMAscript" in the VRML97 and X3D standard 8-)

http://www.web3d.org/x3d/specificat...Ref.html#Script

| Browsers are required to support the ECMAScript language and may ....

As VRML was young, SGI/cosmoplayer used the term "vrmlscript" instead....

so long
MUFTI
--
.... bearbeiten sie die Mehrkanallinien von Kommunikationen, auf,
welchem Sie registriert werden
aus einer Werbemail, Stichwort: rfc 2387 ?
Richard Kennaway

2004-12-22, 7:19 pm

In article <cq9jq1$1qnr$1@soapbox.cs.bham.ac.uk> bensmyth,
noreply@test.com writes:
>Unfortunately that doesn't really achieve what I want.....
>
>If you consider the following script, I would like to have three files:
>main.js, Item.js and ItemArray.js (it should be obvious what code goes
>where)...
>
>Based on what you have said... is that possible?


You can put your JavaScript in external files and use the URL to a
JavaScript file as the url field of a Script node, but you can only have
one JavaScript file per Script node, and the contents of that file will
be the only JavaScript code accessible to that Script node. (The url
field is actually a list of URLs, but they are alternatives -- the first
one that exists is the only one that will be loaded.)

So the answer is no, functions defined in different JavaScript files
cannot call each other. You would have to put all of that code into a
single file. You can have multiple Script nodes all loading the same
JavaScript file, but they still can't call each others' functions. Each
gets its own separate copy.

--
Richard Kennaway, jrk@cmp.uea.ac.uk, http://www.cmp.uea.ac.uk/~jrk/
School of Computing Sciences,
University of East Anglia, Norwich NR4 7TJ, U.K.
Braden McDaniel

2004-12-22, 11:15 pm

On Wed, 2004-12-22 at 17:07 +0000, Joerg Scheurich aka MUFTI wrote:
>
> Therefore the name of the language you are refering to is named
> "ECMAscript" in the VRML97 and X3D standard 8-)
>
> http://www.web3d.org/x3d/specificat...Ref.html#Script
>
> | Browsers are required to support the ECMAScript language and may ....
>
> As VRML was young, SGI/cosmoplayer used the term "vrmlscript" instead....


VRMLScript isn't just another synonym; it is a different language.

VRMLScript is not an ECMAScript superset. Rather, it is a severely
limited subset. JavaScript and JScript are both ECMAScript supersets.

--
Braden McDaniel e-mail: <braden@endoframe.com>
<http://endoframe.com> Jabber: <braden@jabber.org>

Richard Kennaway

2004-12-23, 12:17 pm

In article <cq8u3t$1q35$1@soapbox.cs.bham.ac.uk> bensmyth,
noreply@test.com writes:
>I'm trying to develop an OO javascript solution but cannot seem to manage to
>call methods from one script from the other.... Here's a simple example:
>

[snipped]
>
>CORTONA ERROR MESSAGE: s2.c is not a function
>
>
>Any ideas?


Scripts can't call each other's functions. Functions are only in scope
within the script they occur in.

Instead of
s2.c();
try
s2.c = TRUE;
and also add the following field to s1:
directOutput TRUE
You need that in order to be able to assign directly to an exposedField
or eventIn of a node. Speaking of which, in s2, c needs to be declared
as an eventIn, not an eventOut. As written, the function c() has nothing
to do with the eventOut c. If c is an eventIn, the function c() will be
called whenever an event is received on c. c() should also take two
arguments, the value of the event and the timestamp of the event.

--
Richard Kennaway, jrk@cmp.uea.ac.uk, http://www.cmp.uea.ac.uk/~jrk/
School of Computing Sciences,
University of East Anglia, Norwich NR4 7TJ, U.K.
bensmyth

2004-12-23, 12:17 pm

> Scripts can't call each other's functions. Functions are only in scope
> within the script they occur in.
>
> Instead of
> s2.c();
> try
> s2.c = TRUE;


Unfortunately that doesn't really achieve what I want.....

If you consider the following script, I would like to have three files:
main.js, Item.js and ItemArray.js (it should be obvious what code goes
where)...

Based on what you have said... is that possible?

function initialize() {
var i1 = new Item(1,2,3);
var i2 = new Item(4,5,6);
var i3 = new Item(7,8,9);

var itemArray = new ItemArray();
itemArray.addItem(i1);
itemArray.addItem(i2);
itemArray.addItem(i3);

print(i3.x);
print(itemArray.getItem().x);
}


function Item(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}


function ItemArray() {
this.items = Array(5);

this.addItem = addItem;
this.getItem = getItem;
}

function addItem(item) {
this.items.push(item);
}

function getItem() {
return this.items.pop();
}



Regards,

Ben


bensmyth

2004-12-25, 12:14 pm

> i think the problem here is conceptual, you're thinking of the js files
> as objects, in vrml its the Nodes that are your objects, the js file is
> like a container for the methods.
>
> i've seen various articles lamenting the naming of javascript, (
> marketing men trying to jump on a bandwagon.) javascript is
> fundamentally different to java.
> see;


Javascript can however be used to create objects.....

http://wp.netscape.com/eng/mozilla/...cript/model.htm



Example:

function MyObject() {
this.value = 1;

this.getValue = getValue;
this.setValue = setValue;
}

function getValue() { return this.value; }

function setValue(v) { this.value = v; }

function initialize() {
var myObject = new MyObject();

var i;

i = myObject.getValue(); //i == 1

myObject.setValue(5);

i = myObject.getValue(); //i == 5
}


Regards,

Ben


Joerg Scheurich aka MUFTI

2004-12-25, 12:14 pm

> i've seen various articles lamenting the naming of javascript, (
> marketing men trying to jump on a bandwagon.) javascript is
> fundamentally different to java.


Therefore the name of the language you are refering to is named
"ECMAscript" in the VRML97 and X3D standard 8-)

http://www.web3d.org/x3d/specificat...Ref.html#Script

| Browsers are required to support the ECMAScript language and may ....

As VRML was young, SGI/cosmoplayer used the term "vrmlscript" instead....

so long
MUFTI
--
.... bearbeiten sie die Mehrkanallinien von Kommunikationen, auf,
welchem Sie registriert werden
aus einer Werbemail, Stichwort: rfc 2387 ?
Richard Kennaway

2004-12-25, 12:14 pm

In article <cq9jq1$1qnr$1@soapbox.cs.bham.ac.uk> bensmyth,
noreply@test.com writes:
>Unfortunately that doesn't really achieve what I want.....
>
>If you consider the following script, I would like to have three files:
>main.js, Item.js and ItemArray.js (it should be obvious what code goes
>where)...
>
>Based on what you have said... is that possible?


You can put your JavaScript in external files and use the URL to a
JavaScript file as the url field of a Script node, but you can only have
one JavaScript file per Script node, and the contents of that file will
be the only JavaScript code accessible to that Script node. (The url
field is actually a list of URLs, but they are alternatives -- the first
one that exists is the only one that will be loaded.)

So the answer is no, functions defined in different JavaScript files
cannot call each other. You would have to put all of that code into a
single file. You can have multiple Script nodes all loading the same
JavaScript file, but they still can't call each others' functions. Each
gets its own separate copy.

--
Richard Kennaway, jrk@cmp.uea.ac.uk, http://www.cmp.uea.ac.uk/~jrk/
School of Computing Sciences,
University of East Anglia, Norwich NR4 7TJ, U.K.
Braden McDaniel

2004-12-28, 7:17 pm

On Wed, 2004-12-22 at 17:07 +0000, Joerg Scheurich aka MUFTI wrote:
>
> Therefore the name of the language you are refering to is named
> "ECMAscript" in the VRML97 and X3D standard 8-)
>
> http://www.web3d.org/x3d/specificat...Ref.html#Script
>
> | Browsers are required to support the ECMAScript language and may ....
>
> As VRML was young, SGI/cosmoplayer used the term "vrmlscript" instead....


VRMLScript isn't just another synonym; it is a different language.

VRMLScript is not an ECMAScript superset. Rather, it is a severely
limited subset. JavaScript and JScript are both ECMAScript supersets.

--
Braden McDaniel e-mail: <braden@endoframe.com>
<http://endoframe.com> Jabber: <braden@jabber.org>

Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews