Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non functioning Tween when reusing a GoTween instance with modified TweenProperties #74

Open
seandanger opened this issue Jan 28, 2016 · 5 comments

Comments

@seandanger
Copy link

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.

@zapdot
Copy link

zapdot commented Jan 28, 2016

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.

@seandanger
Copy link
Author

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.

@zapdot
Copy link

zapdot commented Jan 28, 2016

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
#74 (comment).

@seandanger
Copy link
Author

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();
        }
    }
}

@zapdot
Copy link

zapdot commented Feb 2, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant