Hi, welcome to this new video. Now that we've understood what a tuple is, we're going to start using it in an example. We're going to learn about a concept that I find very powerful in programming and it has helped me really start organizing my code and my software in many interesting ways and keep things organized. That's the notion of a state machine. We're going to start with a very simple state machines, because state machines can get quite complex over time. But we're going to do the most simple implementation of a state machine. But let's first understand what a state machine is. A state machine is a computational model used to describe all the possible states and transitions between those states that a particular object or system might be in. What does that mean? It means that, let's say we have a switch. It could be in an on state or it could be in an off state. Those are the two only states in which the switch can actually be. We might want to identify what are the conditions that take us if we are in the on state, that would take us into an off state and the other way around as well. Obviously, we could have more than two states, we could have infinite number of states. But what's interesting here is that the states are predefined and the way in which we transition from one end to the other. Those are very explicit as well. You can think of these state machines as a further way of modularizing or breaking and compartmentalizing the code that you're working on. You're going to have different areas of the code that refer to a specific state and those may be working perfectly. But maybe you have a bug that happens only in a specific state that you're in. Well, you can clearly isolate that, that is where that bug might be happening, because you have been working with this structure of state machines in mind. We're going to write a very simple state machine using tuples for describing the different states that we can be in. Let's jump into processing to see. Here we are in processing. We have our template. Let's draw a simple ellipse in the middle of the screen. Let's do def draw. We're going to do a background black. I insist just refresh the background every frame, and an ellipse that is going to be in 600, 300 so the center and of 200 pixels by 200 pixels. Let's see what we have. That's our ellipse. Let's imagine that it's a light that can be in three different colors or three different states. We can define our possible states with a tuple. Let's say, states equal RED, GREEN. Let's just use capitals, and BLUE. Now, let's imagine that this tuple represents all the possible states. The good thing about tuples being immutable is that we're not going to be changing the number of states later. Software won't be able to alter the structure so we're locked in these states as we describe them. But we also want to know in which current state are we in? We're going to create a variable and current state will be, let's use the variable, states. States 0. This represents that we're going to be in the red state. Basically, this would be a string variable that says our current state is the first entity of our three possible states. We're using the concept of the topple here to identify the possibility space or the possible states in which this system can be in and then using one of them as a current version. That's great. Now we could actually go ahead and change some condition. We could say, well, if we could change the feel, the color of the Canvas based on the state we're in. But let's try to do that as a function. Let's just create a function change state. Let's create a bit of space here to write that function. What are the conditions that we want to evaluate? We want to evaluate an if statement that says if current state is equal to zero, then something happens. Let's imagine what will happen is the fill will be red. We can copy this line once more. Basically, we're going to do three versions of that to elif. If the current state is state number 1, the fill here, we're going to do, I think we wrote green. There would be RGB, there we go. Then finally, let's do the final elif statement for the final state, that is state number 2. We have repeated this line three times or almost repeated it. Changing the if statement condition being are we in the first state, the second state, or the third state, and changing the fill that we are using in each one of those cases. That's good. But the problem that we have here is we're using a function and we're accessing something that is a global variable. Both states and current state are global variables. Variables have been described outside that function. We're going to have to add global states and current state. There we go. In many places, you will see that it's not a very good practice to use these global variables. We're going to learn further down the line in the course, how to start getting rid of this, how do you avoid using global variables and what are the best practices within programming? But for now, I think that we want to do, it's a simple implementation where we are accessing these variables that we declared here within this function. Notice that I named this function, change state. At this time, we are not changing the state at all, we're just changing the color based on the state so we're evaluating the state. How could we change the states? Maybe we want to test if this is in fact working we could put this line of code, this function, before the ellipse, just to see if we remember that the ellipse was white but if we are in fact in state 0, which is the red state, we would be, it should be red. We're facing a problem. The problem here is that we should be not assigning but checking for equality. We're asking the question in an if statement. Is this current state the first state, and there we go. It is working. If we would change that, the current state up here, if we would start with state 1, we would be green and then let's check the final one. That's great. Our function is able to identify the states, but let's not just change it every frame. Let's not call this function automatically. Let's call this function whenever we want to change this state. We are going to maybe start just to make sure that we are in fact having ready lips to start with. Our first state it's just going to be our base condition but we want to change this state now by creating a mouse function. Let's do a mouse-click function. This is one of the inbuilt functions in processing that allow us to identify once the mouse is clicked, we can call a function in this case. We're going to use this function only when we click the mouse. We're starting in a red state. If we click, we're having there, let's see what's going wrong. Well, as I mentioned before, we are actually checking for color, but we're not changing the state. We're not really getting to the point at which we're altering the states. How would we do that? Let's just pick the current state. What would be the next state to zero would be one? If we are in state zero, we would transition to state 1 and if we're in state 1, we're going to do this for each one of the different states. We're going to transition to state 2 and if we're in state 2, we're going to transition to state 0 so we look back. This is what we're identifying as a transition. On mouse click, we are moving from the current state to the next state in the list. We're moving from red to green to blue, back to red and we look around. Let's see, we have the red state, we click again. Here it's working. Yeah, it seems to be working well. It's having an issue with the input, but it seems that it's not a problem with the code, right? As you can see, we have the transitions. So this is where we're kind of starting to get into how we can actually construct a piece of software that might have a whole range of different states and different functionality. We're going to be breaking and using this structure quite a bit to not only identify the movement, sometimes of an object, but sometimes the entirety of how the software is actually operating. A very useful technique, and again, this is a very simple implementation of that. We will be seeing more advanced versions as we move along. I'll see you in the next video.