Enklu Documentation

Enklu Documentation

  • What's New
  • Guides
  • API Reference
  • FAQ
  • Contact

›Scripting

Guides Overview

  • Guides Overview

Web Editor Basics

  • Web Editor Interface
  • Minimum Requirements
  • Managing Your Experiences
  • Sharing Experiences with Others
  • Keyboard Shortcuts
  • Placing an Asset in the Scene
  • Lights
  • Scene Settings
  • Post-Processing
  • Web Play Mode

HoloLens

  • Hololens 2 Basics & Setup
  • Hololens (1st gen) Basics & Setup
  • Voice Commands
  • Live Preview
  • Video Recording
  • Remote Debugging
  • Occlusion

Chapters Series

  • Chapter 1: Intro to Enklu Cloud
  • Chapter 2: Thinking in 3D
  • Chapter 3: Adding Lights & Effects
  • Chapter 4: Adding Interactions

Assets

  • Import Your Own Assets
  • Public Assets
  • Exporting Unity Prefabs
  • Adding Animations from Unity
  • Scene Complexity Recommendations
  • Audio
  • PBR Pipeline

Spaces

  • Creating a Space
  • Updating a Space
  • Adding Anchors
  • Downloading the Scan

Scripting

  • Public Scripts
  • Introduction to Scripting
  • Vine Script Basics
  • Behavior Script Basics
  • Controlling Animations
  • Node SDK

Enklu Embedded

  • Installing Enklu Embedded
  • Embedding an Experience in Your Unity Scene
  • Syncing Assets From Unity

Behavior Script Basics

Enklu Cloud's scripting interface offers 2 types of scripts: Behavior scripts and Vine scripts. This article will focus on Behavior scripts. For more information on Vine scripts, please check out our Vine Script Basics article.

Behavior scripts utilize JavaScript to make things happen. They can manipulate the object graph, work with central systems, as well as send and receive messages between other scripts.

Creating a Behavior Script

Select Scripts from the library drop down menu. Then click the + button next to Scripts. This will prompt for a Name to give the new script, a Description for the new script (this is optional), and a Type for the new script. Make sure Behavior is selected from the type drop down menu. Once these fields have been populated, click the Create! button at the bottom right of the library panel. This will create a new Behavior script in the script library with the input name and description.

Name, type, and description fields to create a new script.

Attaching a Behavior Script

Create or select an asset element you wish to attach a script to. At the bottom of its inspector, you will see a box that says Drop script here. From the script library, click and drag your script to the script section of the inspector and place it on the target box.

Drag new script to script target box.

Writing and Editing Behavior Scripts

To edit a script, either select the edit icon (pencil icon) on the script thumbnail or select the script and click the edit icon in the inspector panel. This will open the script editor which can be directly type into while still viewing the canvas.

Typing into script editor.

Once you have written or made changes to a script, you will have to save it to see its effects in the canvas. To save a script, click File > Save. Saving a script will cause the canvas to reload the script.

There is an Autosave toggle in the File menu that can be turned on which will periodically save the script and update the canvas. However Autosave is not recommended for use with Behavior scripts as saving an incomplete Behavior script is likely to cause more errors and unexpected behavior than saving an incomplete Vine script.

Lifecycle Functions

Behavior scripts have 3 main phases: enter, update, and exit. These are known as the Lifecycle Functions. You may use all or none of these functions in your script.

Enter

If an enter function is defined, it will be called once and only once after all scripts have been initially executed. This is generally a safe place to manipulate elements created through other scripts, as all scripts are guaranteed to be loaded by this point.

Update

If an update function is defined, it will be called every frame. This function is guaranteed to be called after enter is called, though an enter function is not required for update to be called.

Exit

If an exit function is defined, it will be called right before the object is destroyed. This is often used to turn off systems enabled within the script or to return elements modified by the script back to their original states.

All new Behavior scripts will be created with an enter function and an exit function.

New script file showing empty enter and exit functions.

Scripts as Modules

All scripts are wrapped in functions to prevent name collision in the global scope. Because of this, all functions that should be exposed to other scripts, including any used Lifecycle Functions, should be exported on a module object passed in by the runtime.

All new Behavior scripts are created with a module object exporting its enter and exit functions.

New script file showing empty enter and exit functions.

← Vine Script BasicsControlling Animations →
  • Creating a Behavior Script
  • Attaching a Behavior Script
  • Writing and Editing Behavior Scripts
  • Lifecycle Functions
    • Enter
    • Update
    • Exit
  • Scripts as Modules
Enklu Documentation
Docs
Release NotesGuidesAPI ReferenceFAQ
Social
FacebookInstagramLinkedInTwitter
Community
GitHubSlackUser Feedback
Copyright © 2021 Enklu, Inc.