V8 Javascript API

User Types

[Commands]

Description

Command modules can also register user types with RealityServer by exporting a user_types Object. The keys of this object give the user type names and values should be Command.Argument objects, although only the type field is used.

In the V8 integration user types are most useful for passing arguments in. There is currently no specific mechanism for creating user types to return, the user should simply create an object with the same properties and return that.

‎// Contrived example command to show use of user data types.
const Camera = require('Camera');
module.exports = {
    command: {
        name: 'set_camera_data',
        description: 'Sets some arbitrary camera data.',
        groups: ['javascript', 'camera'],
        arguments: {
            camera_name: {
                description: 'Name of the camera to set.',
                type: 'String'
            },
            data: {
                description: 'Data to set on camera',
                type: 'camera_data'
            }
        },
        returns: {
            type: 'camera_response_data'
        },

        execute: function({camera_name,data}) {
            const camera = new Camera(camera_name);
            if (!camera.exists) {
                throw new RS.Error(RS.Error.COMMAND_INVALID_PARAMS, 'Camera does not exist');
            }
            
            camera.orthographic = data.orthographic;
            camera.aperture = data.aperture;
            camera.focal = data.focal;

            // Return user type by simply creating an object with the same properties
            return {
                orthographic: camera.orthographic,
                aperture: camera.aperture,
                focal: camera.focal,
                fov: camera.orthographic ? 0 : RS.Math.degrees(Math.atan2(camera.aperture/2,camera.focal)) * 2
            }
        }
    },
    user_types: {
        camera_data: {
            orthographic: {
                type: "Boolean"
            },
            aperture: {
                type: "Float32"
            },
            focal: {
                type: "Float32"
            }
        },
        camera_response_data: {
            orthographic: {
                type: "Boolean"
            },
            aperture: {
                type: "Float32"
            },
            focal: {
                type: "Float32"
            },
            fov: {
                type: "Float32"
            }
        }
    }
};