Repeated DelayedCallbackEvent
I'd like to make a SceneObject enable then disable at a specified interval. My callback triggers the first time but I'd like to wait another interval and run again. Could someone suggest how to do this? I'd prefer not to roll-my-own inside of update events, for instance, but instead wait another callback interval.
//@input float delayTime = 1.0
var parentObject = script.getSceneObject()
parentObject.enabled = true;
function onDelay( eventData )
{
parentObject.enabled=!parentObject.enabled;
delayedEvent.reset(script.delayTime);
}
var delayedEvent = script.createEvent("DelayedCallbackEvent");
delayedEvent.bind( onDelay );
delayedEvent.reset(script.delayTime);
print("bound")
Thank you.
Hi Robert,
Your script is really close to working, the only problem is that when the script disables its SceneObject the first time, the delayed event stops running (since the SceneObject isn't enabled). That's why the event doesn't fire again to reenable the object.
You can fix this by moving the delayed event to a separate SceneObject from the one getting disabled. Here are two ways to do this:
Option 1: Move your Script to a different object
For this method you'll have to move your script to a different object, then hook up your original object to the "targetObject" input in the script. This works because the object holding the script never gets disabled, so the delayed event never gets interrupted.
Option 2: Create a new object at runtime to execute your event
This method works by dynamically creating a new SceneObject, adding a new ScriptComponent to it, and binding your DelayedCallbackEvent to it instead of the current script. This works because the DelayedCallbackEvent runs on its own SceneObject and won't be interrupted by this SceneObject being disabled.
Let me know if you have any more questions!
Worked like a charm. Option 2 is a bit more complex but I like it better. Thank you.