Attaching scripts to Entities


#1

There are two major features that have been merged:

  1. Entity Events - these various events will fire when you interact with an entity. These events will fire as “global” signals that you can hook in any global script using the regular Entities.eventName.connect() feature, or you can use the new entity scripts feature discussed next to have the signal fire for your single entity.

  2. Entity Scripts - entities have a “script” - property. That property can be a URL to a JS file, or can be the actual JS text. The format of the script should be a JavaScript class prototype function. There are several examples scripts added to examples/entityScripts to demonstrate various use cases.

The follow events exist for entities:

  • mousePressOnEntity(entityItemID, mouseEvent) - sent when you press the mouse button down and the cursor is pointing at an entity

  • mouseMoveOnEntity(entityItemID, mouseEvent) - sent when you move the mouse and the cursor is pointing at an entity

  • mouseReleaseOnEntity(entityItemID, mouseEvent) - sent when you release the mouse button down and the cursor is pointing at an entity

  • clickDownOnEntity(entityItemID, mouseEvent) - sent when you being clicking the mouse button while the cursor is pointing to an entity

  • holdingClickOnEntity(entityItemID, mouseEvent) - sent when you continue to hold the mouse button down after previously clicking the mouse button while the cursor is pointing to an entity. Note: the entity that you initially clicked on will continue to get this event EVEN IF you are no longer pointing at that entity

  • clickReleaseOnEntity(entityItemID, mouseEvent) - sent when you release the mouse button after previously clicking the mouse button while the cursor is pointing to an entity. Note: the entity that you initially clicked on will get this event EVEN IF you are no longer pointing at that entity

  • hoverEnterEntity(entityItemID, mouseEvent) - sent when move the mouse cursor such that it begins to point at an entity

  • hoverOverEntity(entityItemID, mouseEvent) - sent when move the mouse cursor such that it continues to be pointed at an entity that you previously started pointing to

  • hoverLeaveEntity(entityItemID, mouseEvent) - sent when move the mouse cursor such that it no longer points at an entity that you previously started pointing to

  • enterEntity(entityItemID) - sent when you avatar enters the bounds of an entity

  • leaveEntity(entityItemID) - sent when you avatar leaves the bounds of an entity

  • Sample scripts - We added sample scripts that can be attached to entities that demonstrate the following features:

  • changeColorOnHover.js - changes the color of an entity as you hover over it, changes the color back when you are no longer hovering over the entity

  • crazylegsOnClick.js - if you click on the entity, your avatars legs will do the crazy legs animation for as long as you hold your mouse down

  • playSoundOnClick.js - click the entity a sound will play

  • playSoundOnEnterOrLeave.js - a sound will play when you enter the bounds or leave the bounds of an entity

  • teleportOnClick.js - click the entity and your avatar will be teleported to the entity


#2

I am playing with the scripts for entities, the only sample script I can find on the running scripts list is changeColorOnHover.js

Where would I find the other sample scripts please?


#3

Hi @Adrian, check this out https://github.com/highfidelity/hifi/tree/d3372537c5f979fdbfc1ce57f97613f7bc8c16dd/examples/entityScripts