Tween : I'm having 'self' doubt.....

Forgive the pun, but looking for a bit of a ‘sanity check’ on using Tweens with Classes…

So, as a ‘stripped down’ example - we can represent ‘position’ using vec2 within a simple class to hold the x,y coords:

self.pos = vec2(0,0)

and then tween by referencing directly the x index as a target:

subject = self.pos
target =  {x = WIDTH}
tweenID = tween(5, subject, target)

All fine and dandy!

However, how would this work with a single variable and what should ‘target’ in this instance be?

self.rotate = 0
subject = self.rotate
target = ??????
tweenID = tween(5, subject, target)

I guess the issue is that self.rotate is not ‘userdata’ (ie vec2 etc) and hence I can’t seem to work out the correct syntax or a ‘simple’ way of representing easily what a ‘target’ should be.

As a hack, this works fine if a treat pos and rotate as a vec3 (using the z index as the rotate) - but there must be an easier way of tweening a single (non user-data) variable within a class?


Tween is designed to work with properties on any tables, including classes.

Try this:

tweenID = tween(5, self, {rotate = 90})

Numbers in Lua are immutable so you can’t use them as the subject of a tween. Instead you use the class as the subject and the properties of the class as the target.

Nice one John, this worked a treat! Sanity has been restored! :slight_smile:

P.s. Might be useful to add this sort of usage of tweens (via classes) to the docs - I’m sure others will find this useful as well :wink: