Non functioning Tween when reusing a GoTween instance with modified TweenProperties
I'm saving instances of tweens that need to be reused often, with minor TweenProperty changes. Here's what my code looks like:
// declaration as a field of my class
private GoTween _myTween;
// setup
var config = new GoTweenConfig().setEaseType(GoEaseType.CircOut).startPaused();
_myTween = new GoTween(_myTarget, 1.0f, config);
_myTween.autoRemoveOnComplete = false;
Go.addTween(_myTween);
// use
int newValue = Random(0, 100); // same tween, but a different value each time
_myTween.clearTweenProperties(); // get rid of old properties from last use
_myTween.addTweenProperty(new IntTweenProperty("MyIntProperty", newValue));
_myTween.goToAndPlay(0.0f); // play from the start
This works the first time the tween is used, but after that, the value set by the tween is always 0 instead of newValue.
From debugging, it looks like the newly added TweenProperty is never getting its prepareForUse() method called, which normally happens in GoTween's onInit(). I think either prepareForUse() should be called on new properties added via addTweenProperty() if the GoTween's _didInit is true, or there should be exposed a Reset() method to set the _didInit field back to false on the GoTween, so it will properly call prepareForUse() on all TweenProperties.
I'll experiment with these fixes and submit a pull request once I have a proper fix, but it would be nice to hear from an author with more knowledge of the code as to what the best solution is.
is Random() a function of yours, or is this pseudocode? If it's the latter, can you paste something closer to what you're actually using?
I'll take a peek at the source myself, and see if there's anything else I might be able to suggest.
Pseudocode. In actual use, I'm tweening the value of a health bar for a game. Each time the tween happens, I'm changing the IntTweenProperty's endValue param to the new health value, which could just as easily be a random int between 0 and 100.
Is there a reason why you're not creating a new tween, rather than trying to edit the property?
- Michael
On Thu, Jan 28, 2016 at 8:39 AM, seandanger [email protected] wrote:
Pseudocode. In actual use, I'm tweening the value of a health bar for a game. Each time the tween happens, I'm changing the IntTweenProperty's endValue param to the new health value, which could just as easily be a random int between 0 and 100.
— Reply to this email directly or view it on GitHub https://github.com/prime31/GoKit/issues/74#issuecomment-176189151.
I'm aiming to keep the Tween in memory (as a field in this particular class) and reuse it over and over again instead of allocating a new one each time. My original goal was to re-use the property too, but the IntTweenProperty class has no way of changing the target endValue, so I was forced to create a new property to tween with.
As an aside, I've extended IntTweenProperty and added the ability to change the endValue, so I'm not using the above code any longer, but I think the issue still stands that anyone wanting to add properties to an already-run tween will find them non-functional.
Here's that class in case anyone is interested, note that prepareForUse() is called each time the value is changed since GoTween won't call it after the first run.
public class IntAdjustableTweenProperty : IntTweenProperty
{
public IntAdjustableTweenProperty(string propertyName, int endValue, bool isRelative = false) : base(propertyName, endValue, isRelative)
{
}
public int EndValue
{
get { return _originalEndValue; }
set
{
_originalEndValue = value;
prepareForUse();
}
}
}
So I spent a bit more time looking into this, and thinking about it.
Right now we have a smattering of properties that actually have resetWithNewEndValue() that @prime31 put in back in 2013. Perhaps if our AbstractTweenProperty also utilized generics, we could easily add the method to the AbstractTweenProperty class, and be done with it. It'd take a bit of work to do, so I'll leave this ticket open as a potential improvement to attack in the future, or as a challenge for anyone looking to dive in and help out. :)
One more thing to consider, doing something like this doesn't necessarily reset the startValue of the tween, so while doing something like this may allow you to keep a tween in memory and edit the property targets willy-nilly, you're not guaranteed to have a great looking result depending on when you do reset the desired end value to something else.