Auto Dispose a Script

37 views
Skip to first unread message

robin....@redspace.com

unread,
May 20, 2015, 3:13:23 PM5/20/15
to fla...@googlegroups.com
I'm running into an issue where scripts are acting weird. Current I'm doing something like this.

public function foo(value)
{
var script:Script = new Script();
script.run(new Sequence([
new Delay(value),
new CallFunction(function(){
//do something
script.owner.dispose();
})
]));
owner.addChild(new Entity().add(script));
}

But I don't believe that the script is being fully disposed of. I'm aware that script.run returns a disposable, but I can't find a way for it to fire once a script has completed aside from making the disposable global which isn't an option.

If there is a way to create a delay in calling a function without a script that could also solve my problem.

Any help here would be much appreciated.

Thanks,
Robin

Mark Knol

unread,
May 20, 2015, 4:31:00 PM5/20/15
to fla...@googlegroups.com, robin....@redspace.com
What you do is correct (I also dispose the script like that, otherwise it keeps existing), however you can also create a component that simple counts up/down untill it passes a value. Make sure you take the deltaTime in account from the onUpdate function.

For example, I created this AutoDisposer component. It disposes it's owner entity after a given delay. You can even provide a function that should be called before it gets disposed. I have some variations of this component for different purposes (repeating things, cooldowns).

You can create simple components like this yourself; script/actions don't always fit. 

Enjoy!

Mark Knol

unread,
May 21, 2015, 8:18:37 AM5/21/15
to fla...@googlegroups.com, robin....@redspace.com
By the way, to create global delays, I always use snippet. Since it makes use of the flambe update loop, so its more in sync and controllable compared to haxe.Timer

public static inline function delay(onComplete:Void->Void, delay:Float = 0) :Disposable
{
return System.root.addChild(new Entity()
.add(new AutoDisposer(delay, onComplete)));
}

robin....@redspace.com

unread,
May 21, 2015, 1:19:38 PM5/21/15
to fla...@googlegroups.com, robin....@redspace.com
That's good to know about this being a correct way to dispose a script. I do however like the auto disposing component solution better so I think I'll go with that.

Thanks for your help.
Reply all
Reply to author
Forward
0 new messages