this is going to be a detailed tutorial on how to use updaters within madam so firstly uh we're going to import some stuff from madam and we're also going to import from random this just for our last animation here right now i'm just going to start off with a little kind of primer as to what we've done before so i'm going to call it old one here where without a box we'd add some text and we'd add an update to the text if we wanted to stay in the center of the box as the box
is moving around right so we've got the box with its stuff we've got the text and then we add the updater keep it in the center we add it and when we animate the box around and move it around and all sorts of things it'll stay within the center okay but notice if i scale the box the scaling of the text is not going to be considered it's only going to stay within the center so if i made it too small then the text would be like outside of the box right i can clear updaters
and then i can play that box around moving again and you'll see that there is no updater applied there so we've already seen something like that similarly we have got old two we're in everything else staying the same right so we get our box we got the text except here we've got always redraw instead of add updater same thing again right you'll notice the same thing the scaling not much changes there uh and when i clear the updaters everything will play out nicely right so we've seen this before if we want to add an updater
you can add an updater if you want to clear it you can clear it my go to is always redraw because i kind of like to just do everything at once in one line of code but you know each other so here's on to our new section here right where instead of just typing out all that stuff within the scene if i know what i want it to look like and i want these things to move around and stay in sync like if i want to create a model of something then i'll create that as
a function so i'm just going to i'm just calling this get helper function and it's going to input a color if i want to change the color of this stuff now you'll see everything's a vector group and that's what i return like i would if i'm trying to create some stuff i've got the box i've got the text now notice you don't need to add updaters when you're creating a function because this get helper function will always keep the text within the center of the box as you move this this thing around this this whole
result around everything will just move in sync right it'll move as one object rather than the box moving so you know that's a nice little option that you could do there now you'll notice if you code it like this it'll render way faster like noticeably faster okay so i get my stuff which is my helper function the color is blue because that's really just the fill color of the uh of the box right create it animate it so i can scale it i can make it bigger and you'll notice that the text also gets bigger
as well because everything and stuff which is the box and the text is being changed and if i just wanted to move the box right notice i've got stuff and i'm accessing the first element of it so within stuff or within stuff it is the vector group of this here so if i wanted to move around the text i'd access it by stuff and the first element of it rather than the zeroth element because the box is the zeroth element and the text would be the first element so you can totally do that that's just
leveraging the fact that we're using python to code everything out here and that's how python will interpret your functions and how you've added everything together it's just arrays okay and now we're on to something which is relatively new this is the more detailed part which is you can add an updater function and apply that to animations if you want things to change like instantaneously right like 60 frames per second if you're rendering at high quality and you'll notice that within three blue one brown videos he does this quite frequently because this is how you get
things to move around really really nicely so firstly i i just want to have a coin flipping heads or tails and to give it that effect that it is flipping around so firstly i need to get a coin okay it's a vector group of stuff but the coin is dependent on a 50 50 event so i access by random uniform and that's why at the very top of the code i had import random and this is just going to access a random number between zero and one uniform is uniformly distributed right you know there's it
there's no bias there's no it's not like it's going to pick something from the center more likely like it would with a normal distribution it's uniformly distributed where if it's less than 0.5 so 50 of the time give me a circle set it to be red and put a t in the middle of that as if like it's a tails otherwise make it a blue circle and give me a heads and then return that result okay you'll see i've added the text oh i've actually i've done that twice i don't need to do that i
only need to do that once result dot add c and text okay so that's just going to give me the coin that's not animating anything yet this is my animator function here okay this is my update animator similar thing it takes in a coin so when you create a coin throw that in and it'll animate it now what this is going to do it's going to pick a random number again between 0 and 1 because 50 50 event result is our vector group if it is less than 0.5 give me that otherwise give me that
so it's the same as before give me that heads or tails but then you'll notice here what i've got is p and k for shifting the coin up and left and it's just by some tiny tiny tiny little nudge now let me explain what's going on here what this is going to do is it's going to consider our coin being flipped and as we well let's have a look at how this how this comes out we add the coin right it's always redraw get coin update from funk go coin and animate the coin for two
seconds now what this is going to do every single frame per second so this is going to happen 60 times per one second if you're rendering at 60 frames per second what it's going to do is it's going to pick a random number for this one frame depending on what the random number is will tell us if we've got a heads or tails and then we're going to shift the coin up or down or left or right by the tiniest nudge and then we're going to do that again and again and again and again and
again 60 times per one second and i want this to have a runtime of two seconds so it's going to give the illusion of like about 120 flips of the coin now if you can wrap your head around updater functions you can like the really the creativity is endless because you can make things move around and animate on the screen and you can you can do all sorts of cool stuff if you can come up with your updater function