Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active January 30, 2017 23:37
Show Gist options
  • Save SeijiEmery/633f33ea331be04c6bad to your computer and use it in GitHub Desktop.
Save SeijiEmery/633f33ea331be04c6bad to your computer and use it in GitHub Desktop.
Hifi scripting: JS entity wrapper + timeout example
var ENTITY_TIMEOUT = 30; // tell entity server to delete us after 30 secs
var TIMEOUT_INTERVAL = 20; // refresh every 20 secs
function createEntity (properties) {
properties.lifetime = ENTITY_TIMEOUT;
var entityId = Entity.addEntity(properties);
var timeout = TIMEOUT_INTERVAL;
return {
id: entityId,
edit: function (properties) { Entities.editEntity(entityId, properties); },
delete: function () { Entities.deleteEntity(entityId); },
getProperties: function () { Entities.getEntityProperties(entityId); },
keepAlive: function (dt) { // call this every update
if ((timeout -= dt) < 0.0) {
timeout = TIMEOUT_INTERVAL;
Entities.editEntity(entityId, {
lifetime: Entities.getEntityProperties(entityId).age + ENTITY_TIMEOUT
});
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment