Making Scripting Easier: Entities Simplified [WIP]


#1

So I’ve been working on a script that should make scripting and rezing entities / overlays easier.

For now it is just Entities, but Ill get to Overlays as well eventually.

Heres the jist:

Wrapper for Entities and Overlays

For High Fidelity Metaverse’s JS-Scripting Language
to work alike to JS OOP.

Note that this is still work in progress.

Allows for JS Chain notation over the Default High Fidelity settings.

In Laymans, This means that you can create and control an entity using just a JS Object with out calling “Entities”.

Entity Manipulation

var e = new Entity("uuid")

Would bind a High Fidelity Entity into the JS variable e.
e.properties would contain everything Hifi’s Entities.getEntityProperties
would return for the uuid from the last sync.

Now e.properties does not currently stay uptodate automatically, its only
last time a sync was called.

By Default, calling the uuid constructor of Entity will call sync.

To sync the entity instance later on in the code, we call

e.sync()

this makes sure that the entity instance is up-to-date to the one in the world.

Note that sync, filter, and all *Entity functions will always return the instance of the JS Object,
so you can do some chaining, if you so wish:

Forexample:

var anewVar = new Entity("uuid")
anewVar.filter(["position"])
       .editProperties({position: Vec3.sum({x:0,z:0,y:1},anewVar.properties.position)})
       .updateEntity()

The above would first find uuid and set it to anewVar.
it would then be connected to the instance.

Calling filter(["position"]) sets what we will update to just "position"
edit the position one unit up, and then update it in world. the instance is
then set to the anewVar

Entity creation

Now, most of the time one isn’t looking for an existing entity or overlay
that already exists. You actually are creating them.

Expanding on this, you may want to create a custom object that’s not in the world

var e = new Entity({properties})
e.addEntity()

You could even use this in a single line:

Forexample, rezing 100 purple spheres in one line., Jump and theyll go all over the place

for(var i = 0 ; i < 100; i++) new Entity({type:"Sphere", position:Vec3.sum(MyAvatar.position, {x:0,y:1,z:0}), name:"CubeToDelete"+Math.random()*10000, color:{red:110,blue:255,green:4},dynamic: true, lifetime: 45}).addEntity()

In this, We create a new Entity bound to entity then we add this instance of
entity to the world via entity.addEntity()

Basic Entity Interaction

Basic Interactions for an entity can be bound with a single callback line

e.setInteractionStart(function(instance,event){ print("Interaction")})

This binds both hand controllers AND mouse to a single callback.
The event being an MouseEvent.
For hand controllers, only Left click will register, and the Button will be “hand”

Instance is the Wrapper Entity, that allows you to manipulate the object.

For Example Creating multiple that hop on click.

var hopper = function(instance, event){
  instance.filter(["velocity"])
          .editProperties({velocity: {x:0,z:0,y:15}})
          .updateEntity();
}
var defaults = {type:"Box", position:MyAvatar.position, name:"Hopper",lifespan:60, color:{red:0,blue:0,green:255},dynamic: true, gravity: {x:0,y:-2,z:0}}

for(var i = 0 ; i < 10; i++) new Entity(defaults).addEntity().setInteractionStart(hopper)

Similarly, interactions can be bound to setInteractionHold and setInteractionStop

Note that binding the interactions through this,
applies only to whom ever the script has run, if you want to do it for an entity script, use.

(function(){
    var obj = new Entity()
    var hopper = function(instance, event){
      instance.filter(["velocity"])
              .editProperties({velocity: {x:0,z:0,y:15}})
              .updateEntity();
    }
    return obj
  })

When setInteraction is called, both the Mouse
AND hand controller interactions are bound to the object.

to clear all interactions, simply call,

e.clearInteractions()

Ill work on this a bit more to also get Overlays working, but feel free to give this a shot :slight_smile:


Scripting Getting Started 2017
Q How democratic do you want highfidelity to be?