Skip to main content
Skip table of contents

Writing OKO Scripts - The Basics

There are certain globals that are accessible to any script, at any point in the script’s lifetime:

OKO.Log

This allows you to print messages to the developer console (great for debugging scripts!)

TheEntitySystem

The interface to the Connected Spaces Platform (CSP) Entity System that manages the state for all your Entities within the space. From here, you can access other Entities and Avatars.

ThisEntity

The entity that owns the script that is being executed. From here, you can get and set properties for the entity, grab components that belong to the entity and mutate their state too.

TheEntitySystem

Responsible for retrieving sets of entities, either space objects, or other users.

getEntities() - Gets all Entities within the space.
getAvatars() - Gets all Entities that represent a user.
getEntityByName() - Get an entity by its name.
getEntityByID() - Get an entity by its id.

Entity

An entity defines one particular collection of components in the space, along with some other basic attributes, such as its world transform and who owns it.

position, rotation and scale - The properties defining the world-space transform of the entity. These can be read from and written to in script.

getComponents() - Gets all the components that belong to the entity.

subscribeToMessage('message','callbackFunctionName') - Subscribes the entity to a named message, along with the named script function to call when the message is received. Note that the message could come from any entity in the space.

postMessage('message') - Invokes a named message, which will be transmitted to all entities in the space.

Entity Events

There are certain kinds of messages provided out of the box and you can write scripts to respond to them via entity.subscribeToMessage(). For example:
ThisEntity.subscribeToMessage("entityClick", "onClick");

entityClick - Invoked when an entity's component has been clicked, the params object contains a id (entity id) and cid (component id).

entityTick - Tick is our update loop, this message is invoked once every frame for every entity in the space. The params object contains the delta time.

entitiesLoaded - Invoked when all entities have been loaded.

custom - Remember you can define your own events!

Logging

Sometimes, it can be helpful to understand what's going on with your scripts via logging.

OKO.Log('message') - Logs messages to the browser's console.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.