create3000 / titania

Titania X3D Editor
https://github.com/create3000/titania/wiki
GNU General Public License v3.0
39 stars 10 forks source link

script: type mismatch issue when setting MF types. #120

Closed splace closed 5 years ago

splace commented 5 years ago

cut down to min. demo from PROTO found online.

#VRML V2.0 utf8 Titania V4.5.7

Script {
    exposedField   MFInt32 val []
    url " ecmascript:

function val(i) {
 val = i
 }
"
}

// working for non-MF type.)

Script {
    exposedField   SFInt32 val 0
    url " ecmascript:

function val(i) {
 val = i
 }
"
}

console output..

#   
#   Couldn't assing value to user-defined field 'val': type of argument 1 is invalid, must be 'MFInt32'.
#      
#       ^
create3000 commented 5 years ago

This will never work. Because you define a field with name 'val' it is globally available in the Script. Then you define a function with the name val in the global scope which overrides the field value. The JavaScript engine will assign this function to the field val which results then in the error. Whether or not it will work it is not legal JavaScript. A field handler function for a exposedField always starts with 'set_'+name of the field to make a distinction between the field and the field handler function, in the example above the function must be renamed to 'set_val'.

Besides this exposedFields are not allowed as user-defined Script fields in VRML, the are first introduced in X3D 3.0.

splace commented 5 years ago

This will never work

// working for non-MF type.)

just saying.

splace commented 5 years ago

Besides this exposedFields are not allowed as user-defined Script fields in VRML, the are first introduced in X3D 3.0.

AFAIK that is incorrect;

'exposedField' --> 'inputOuput'

splace commented 5 years ago

A field handler function for a exposedField always starts with 'set_'+name of the field

i assume you mean when there is ambiguity, clearly you don't always need to do that.

but when this happens, i would think a better/clearer way would have been just to decompose the exposedField; inputOnly 'set_val' and outputOnly 'val_changed'

splace commented 5 years ago

seems this was done, for some/one browser, as a simple way to implement a forwarded exposed field, that is, have the same event out as came in.

also

the error message above is the same as when you access an array with an 'out-of-range' index (or maybe undefined index)