Creating a plugin
There are times when we woud like to have a certain feature only activated at a specific time. A wonderful way to achieve this is by using plugins. In this lecture, I will introduce you to the concept of creating plugins and how to get them working. We will use this to improve our logic.
Video Transcript
00:00All right, so let's start animating.
00:02So the first thing that I wanna do
00:04is I wanna be able to fetch our tweens
00:08and well, not tweens yet, our text fields,
00:10so we could actually turn a tween into them.
00:13So to do that, we're gonna have to figure out
00:14some sort of a creative way to append an action
00:19because not every time we break texts,
00:20we wanna actually append a tween to it.
00:23But one thing that we could do is create here
00:26some sort of a callback
00:28that would take back basically what we need to do
00:33is figure out a way to create some sort of a mechanism
00:37that doesn't have to always happen.
00:39It's called the plugin mechanism,
00:41which is something that you probably are familiar
00:43from the plugins that we were working with
00:44earlier of tween-lite.
00:46So what I'm gonna do, I'm just gonna start doing it
00:49and really, it's not gonna do nothing in the beginning,
00:51but then you'll get my point.
00:52So the first thing that I wanna do
00:54is I wanna create here basically a new array.
00:59And I'm gonna just, oopsie,
01:00a new array, I'm gonna call this a tween,
01:02it's gonna be an array,
01:04there's gonna be nothing in it initially.
01:06Now, the thing that I wanna do next is each loop,
01:09after each loop is complete,
01:11I'm gonna actually wanna call
01:15and add that array into my element.
01:17But really what I wanna be able to do
01:19is to dynamically do something to that element,
01:22that text field that we're creating,
01:24and maybe add something else into that array.
01:26Because for example, what we're gonna need
01:28is we're gonna need to get back,
01:29and with this function,
01:30we're gonna need to get back all the tweens.
01:32But we don't wanna automatically tween it, right?
01:34So what I'm gonna do is I'm gonna,
01:36and bear with me, this is a little bit weird,
01:38it's called a plugin architecture.
01:40I'm gonna create here a private function.
01:44And this private function is gonna,
01:45let's call this a plugin, break plugin.
01:51Break, plugin, default.
01:54Break plugin default, it accepts a text field,
01:59and it returns something.
02:01So it returns an object.
02:05And an object is because a text field is a type of object.
02:08So all, I could just literally just click
02:09on the return button and return that T,
02:12and it wouldn't throw an error
02:13because a text field is an object.
02:16So there you go.
02:16So we could break our text by default,
02:19but really in our case,
02:20we could just set our text field
02:22to set it to our text field
02:23because it is a text field, Y sheet.
02:25So that's our breaking,
02:27this is our break plugin default.
02:30So I'm just gonna go ahead and create here a new variable.
02:33I'm gonna call it F or F plug for a function plugin.
02:38It's gonna be a type of function
02:39I'm gonna put inside of it my break plugin default.
02:42Now, the reason is,
02:43is because I don't wanna reference this directly
02:45inside of our forward loop.
02:46What I'm gonna do instead of that,
02:48is I'm gonna go to my item,
02:51my A tween,
02:52and I'm gonna push into it.
02:56I'm gonna push into it the value,
02:58the returns from calling the TF plugin
03:01and sending it inside of,
03:03basically I'm sending my text field inside of T plug,
03:06which T plug is actually my breaking default,
03:09which is actually this.
03:10Now all this cumbersomeness,
03:12and if we run this really,
03:13we're not gonna see anything,
03:14we hopefully won't get also any error,
03:16but it shouldn't change anything in our application,
03:17at least not visually,
03:19everything should remain the same.
03:21And now that we see that everything remains the same,
03:23we know we're in a good spot,
03:24the next thing that we wanna do
03:26is we wanna return our A tween.
03:28Now, if you're pondering, why are we doing this?
03:33The reason is,
03:34is we wanna get an array coming back to us.
03:37But really what we wanna do,
03:38is we wanna be able to send into our element,
03:41a different plugin,
03:43a different plugin logic,
03:45basically instead of the default plugin,
03:48we wanna create a new function here,
03:50and let's say this new function
03:51is gonna be doing something of this nature.
03:53So this is gonna be break plugin tween, tween in, let's say.
03:59And then we're gonna have it return a tween light instead,
04:04and we could have your,
04:04basically create that tween light.
04:06So I'm gonna new tween light
04:08to make sure that it's imported,
04:10and now that I know that it is important,
04:12I will just do a two function to it.
04:14I'm gonna take my text field,
04:16I'm gonna set a duration of let's say one second,
04:18and I'm gonna put here,
04:20actually let's do this from,
04:22because what I'm gonna do is I'm gonna take my text field,
04:24and I'm gonna scale X to let's say 10 times,
04:29I'm gonna scale Y to let's say 10 times its original size,
04:32and that's the animation that I wanna have
04:34for my break plugin tween in.
04:37Now, what I wanna do is I wanna go back
04:39to this breakup text and send as a third parameter,
04:42this plugin function.
04:44And literally, again, I'll just add to your another,
04:46and this will be the plug,
04:48and it's gonna be a function.
04:49And by default, we're not sending anything in,
04:52so if we don't send anything in,
04:53our array would return the actual object.
04:56But if not, we wanna go to our T plug and say,
04:58hey, if I sent you already a plug on my F plug,
05:02then use this plugin.
05:03If not, then use the break in plugin default.
05:05And literally what we did with this very complicated,
05:10probably complicated,
05:11and I'm gonna go through it one more time
05:12after we see that it works,
05:14literally what we're gonna expect to see now
05:16is that all the text should start animating now
05:18all at the same time,
05:20it should start animating itself in.
05:22So if we run our application one more time
05:24and just fix whatever typo we have here,
05:27oh, we didn't return it,
05:28so let me just click on,
05:29we wanna return that tween
05:30because this is gonna return us a tween light.
05:32Okay, let's run it again.
05:35So we could see that if we run it again,
05:38we'll see that all of our tweens are running in,
05:40coming in the exact same time,
05:42and it's exactly what we hope for it to do.
05:45Now, now that we have our tweens
05:47and we have our tween lights running,
05:49we could obviously do this Oto
05:50for our secondary item here.
05:54And what I wanna do is I wanna just walk through
05:56with you one more time
05:57because it is a lot to bring in.
06:00So when we look in this,
06:01basically we're forcing to use a different plugin.
06:05When our original, if we didn't have anything in here,
06:08then by default, we would be using this function
06:11which does nothing.
06:13We would just return the same text field.
06:14And then we use that function,
06:15we call that F plug, which is the,
06:18whatever it's gonna be, it's or gonna be the plug
06:20if we send something or the default.
06:22In our case, it's gonna be the plug,
06:23the one that we're animating.
06:25And then every time we create an item,
06:28we're actually calling that function
06:29and sending in the text field we just created.
06:32So that way we kind of layered functionality inside
06:34that wasn't there before.
06:36And that's how basically you create plugins
06:38if you ever wanted to create a plugin library
06:41like tween light or tween max,
06:44or any other type of plugin library
06:45where you could add features
06:46without it needing to mandatorily be there
06:49inside of the same function.
06:51That's exactly how you do that type of structures.
06:53Now, once we did that,
06:55all those animations should automatically start
06:57as soon as they begin.
06:58So if we run this again,
06:59we should see not only our top text,
07:01but our bottom text running at the same time,
07:03all kind of happening in the exact same time.
07:06So I run it one more time,
07:07we should see all of our texts coming in
07:09exactly at the same time.
07:12Now, one more thing that I wanna do
07:14is I really wanna create a stagger effect here.
07:17And I can't really do it right now,
07:18although if I wanted to,
07:19I could create here with this eye here,
07:21add some sort of a level of saturation,
07:24but there's a better way of doing it.
07:26And we're gonna look at that in the next video
07:27when we actually get into using the timeline light again,
07:31and we can see how we could leverage the timeline light
07:34to create even cooler effect.
07:36So we're gonna see you in the next lecture
07:38when we continue and see it in action
07:40inside of our timeline light.
