Last active
January 30, 2017 23:37
-
-
Save SeijiEmery/633f33ea331be04c6bad to your computer and use it in GitHub Desktop.
Hifi scripting: JS entity wrapper + timeout example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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