Skip to content

A Note on Caching and Reuse

Mike edited this page May 6, 2015 · 2 revisions

By default, all TransformVector3Tweens are cached. These are far and away the most commonly used tweens so there is no good reason not to cache them. Caching for other tween types can be configured in the ZestKit class. There are several ZestKit.cache* bool fields that you can just flip to true and caching will occur automatically. One thing to note is that caching occurs only when you use the ZK extension methods. If you are manually creating your tweens it is up to you to pull from the cache. Fetching an item from the cache is done via the following (using FloatTween as an example): var tween = QuickCache<FloatTween>.pop()

Reusing Tweens

ZestKit lets you reuse any tweens that you create in addition to caching. You can call the setRecycleTween method to tell ZestKit if you want to mark the tween for reuse or not. If you do so, ZestKit will not recycle the tween when it is complete. It will remove it from it's internal list and you can reuse it later by calling prepareForReuse before starting it again. Here is an example to illustrate reusing a tween:

var t = transform.ZKpositionTo( Vector3.one )
	.setRecycleTween( false );

// start it up
t.start();

// some time later
t.prepareForReuse( transform.position, Vector3.zero )
	.start();

// even later, this time after the tween is done we dont need it anymore so we tell ZestKit to recycle it
t.prepareForReuse( transform.position, Vector3.one )
	.setRecycleTween( true )
	.start();

Nobody Knows Your Game Better Than You!

ZestKit has a ton of flexibility baked in but when it comes down to it you know your game best. If you are going to be doing a huge amount of tweens it is recommended to enable caching for the tween types that you will be using heavily. In addition, you can warm the caches to avoid creating any objects at runtime.

Lets look at an example. If your game will be doing a ton of Vector2 and Color tweens you can easily prepare for the load. We did some profiling and we know that there will often be 15 simultaneous tweens of each type going on. Just call the following code sometime before you start tweening. You can of course adjust the types that are cached but we will stick to Vector2 and Color for this example.

ZestKit.cacheVector2Tweens = true;
ZestKit.cacheColorTweens = true;
QuickCache<Vector2Tween>.warmCache( 15 );
QuickCache<ColorTween>.warmCache( 15 );

QuickCache is There For You

As an added benefit, you can use QuickCache to cache any objects at all. It is not limited to ZestKit classes. That being said, it is a tiny class that is limited in scope so don't go trying to stick ParticleSystems in there! It requires that the class being cached has a parameterless constructor. The API is simple so feel free to use it to cache anything at all. Example usage with a custom class is below.

public class MyClass
{
	// some properties and stuff

	// the required parameterless constructor
	public MyClass()
	{}
}

// warm the cache
QuickCache<MyClass>.warmCache( 3 );

// fetch an item from the cache (creating a new one if necessary)
var item = QuickCache<MyClass>.pop();

// all done with item so stick it back in the cache
QuickCache<MyClass>.push( item );

Clone this wiki locally