Untitled

📰Getting Started | **Updates |** 📝 Guides | 🔢 API | ❓FAQ

Guides Overview

Enklu comes with a simple scene editing API. This allows scripts running in the scene to make changes to the scene. This should not be used for multiplayer, but is a handy way to automate scene editing.

const editp = require('edit');

Connection

First, make sure the edit api is connected to the Enklu platform. This is generally done after checking the isConnected flag. The edit object also fires events when the isConnected value changes, i.e. as the device connects and disconnects from the authoring server.

if (edit.isConnected) {
    start();
}else {
    edit.connect(function(err) {
if (err) {
            log.error("Oh no! I couldn't connect : " + err);
        }else {
            start();
        }
    });
}

Then, add a listener for connection changes.

edit.on('connectionchange',function (isConnected) {
if (!isConnected) {
        log.warn('We were just disconnected!');
    }
});

Transactions

The scene can be edited through the use of transactions (txns). A transaction is a list of actions on a scene that are applied atomically. This means that if any of the actions fail, all of the actions are rolled back.

First, create a transaction.

var txn = edit.txns.create();

Next, add actions to the transaction.

txn
    .update(element.id, "bool", "visible", true)
    .update(element.id, "vec3", "position", vec3(0, 1, 0));

Finally, make a request to apply the transaction.

// fire and forget (useful for frequent updates)
edit.txns.request(txn);

// callback
edit.txns.requestCallback(txn,function(err) {
if (err) {
        log.error('There was an error : ' + err);
    }
});

You may also duplicate an element. This requires creating a unique id for the element ahead of time. This is so that the created element may be referenced with other actions within the same transaction.

var id = edit.txns.generateId(); // generates a unique id for the new element
var txn = edit.txns
    .create()
    // include the element this new element should be a child of, the element to duplicate, and the id of the new element
    .duplicate(parentElement.id, templateElement.id, id)

    // now we can reference the created element before it's even created
    .update(id, "bool", "visible", false);

Finally, delete elements with delete.

var txn = edit.txns
    .create()
    .delete(id);

Module Properties

isConnected <bool>

Module Methods

connect(callback)

Connect to the authoring server.

txns.create()

Creates a transaction.

txns.generateId()

Generates a new guid.

txns.request(txn)

Sends a transation to the authoring server.

txns.requestCallback(txn, callback)

Sends a transaction to the authoring server, with a callback.

ElementTxn Class

Instance Methods

update(elementId, schemaType, key, value)

Applies a new value to a key in an element's schema.

duplicate(parentId, templateId, newId)

Copies an element.

create(parentId, elementType, elementId)

Creates a new element that is the child of the provided parent element.

delete(elementId)

Deletes an element

Element Types

Next: Score (Preview)

Sidebar Table of Contents


Untitled