Home

Welcome to Verge3D PzLib API Reference!

PzLib is a set of helper classes, functions and variables used in the code generated by the Puzzles Editor. It is also available for use in custom puzzles loaded as plugins.

PzLib is a set of low-level APIs that may be subject to change. Always check your plugins for compatibility every time a new Verge3D release is out!

API Usage

PzLib API can be used inside a custom puzzle's code function which is located in a plugin's .block file.

  1. First, all entities from PzLib API that are going to be used in a custom puzzle must be declared via the Plug.pzlib() method of a special Plug object. This can be done either by specifying them one by one or by specifying multiple names at once:

    <script>
    function code(block) {
        // like this
        Plug.pzlib('getAllObjectNames');
        Plug.pzlib('getObjectByName');
        Plug.pzlib('generateUniqueName');
    
        // or like this
        Plug.pzlib('getAllObjectNames', 'getObjectByName', 'generateUniqueName');
    
        ...
    }
    </script>
    
  2. Then the code generated by that custom puzzle will have those names available under the PzLib namespace:

    <script>
    function code(block) {
        ...
    
        const fun = Plug.provide('myFunction', function() {
            const objNames = PzLib.getAllObjectNames();
            const cubeObj = PzLib.getObjectByName('Cube');
            const uniqueName = PzLib.generateUniqueName('Cube');
        });
    
        return `${fun}();`;
    }
    </script>
    

Plugin Example

Here's an example of a plugin puzzle that utilizes PzLib API for printing some information about the object specified in the puzzle's input. The PzLib.getObjectByName function is used here to obtain the actual 3d object from its name, which is given as a string:

  • init.plug

    <category name="My Plugin" color="#cb4d00">
        <block type="logObjPosition">
            <value name="OBJECT">
                <shadow type="objectList">
                    <field name="FIELDNAME">&lt;none&gt;</field>
                </shadow>
            </value>
        </block>
    </category>
    
  • logObjPosition.block

    <template
        color="#cb4d00"
        inline="true"
        prev="true"
        next="true"
    
        tooltip="Log the world position of the given object into the browser console."
    >
        <value name="OBJECT" type="String Object3D">
            <label>log obj position</label>
        </value>
    </template>
    
    <script>
    function code(block) {
        const objName = Blockly.JavaScript.valueToCode(block, 'OBJECT',
                Blockly.JavaScript.ORDER_NONE) || `''`;
    
        Plug.pzlib('getObjectByName');
    
        const fun = Plug.provide('logObjPosition', objName => {
            const obj = PzLib.getObjectByName(objName);
            if (obj !== null) {
                console.log(obj.name, obj.getWorldPosition(new v3d.Vector3()));
            }
        });
    
        return `${fun}(${objName});`;
    }
    </script>