Now let's go over the homework. There are many ways to have accomplished are two problems. The first problem is positioning our game object somewhere on the screen and the second problem was making sure that as we adjusted our window size our game objects stayed at a particular position. For that problem I decided to create properties. That way we could use variable names amongst different levels of scope in our script file which would just basically be our functions on top of that I went ahead and created two functions functions setup Sprite and function setup game window.
In this case I created two separate functions to explain better what each block of code does one obviously sets up our sprite within hight class variables and I then went ahead and did the same thing for our game width and height. I'll talk a little more about that later and then I went ahead and created different functions for the different positions. Top center bottom center left right and middle. Each one being about two lines of code or rather exactly two lines of code for each function. On top of that. Notice how I basically positioned or rather I grouped my functions that handle position together.
And they also grouped my functions that handled setup together. So just a little bit of script organization. Not necessary however something to keep in mind and if you're a beginner and you do want to group your functions together based on how similar the tasks are along with if a function requires using another function you'd like to group those together as well. However something to keep in mind but not necessarily important if you are starting in your game programming journey. Now I have two functions set up game windows set up Sprite and the reason I separated was for a little bit of optimization.
Obviously we only need to setup our sprite variables once because our sprite image doesn't change size during runtime whereas our game window during runtime will change size and so we do need to calculate for its different height and weight. If we want to make sure that we position our sprite game object in a particular position this is exactly how we would write our code. However with game objects in particular we do want to avoid running this in a loop for two things one the player should really not have control over sizing their window besides either full screen or windowed and this is especially true for mobile game programming.
Obviously your users should not have the ability to edit and change their screen size on their cell phones. And so even though for this particular assignment I wanted you to get the hang of creating functions and their uses. In reality we would not use this kind of code during game production.
Again I want to point out that this isn't the only way to write code that solves the two problems. For example entry runs every time we enter the scene tree. However since our sprite image isn't changing its size during runtime. You could. For example make the argument that setup sprite. Belongs in the ready virtual method rather than the. End tree virtual method. As a matter of fact the same could be true for. All. Three. You could go a step above that for example you could even make the argument that position meadow and setup game window shouldn't even be in either the ready virtual method or the enter tree virtual method because it is getting called in the physics process.
Virtual method and so if you're starting to look at code like that. That's a good thing because it means one you're starting to think a little deeper about how code behaves in our game lifecycle or rather our script lifecycle. That means you're heading in the direction of optimization and so that's a good thing. If you started thinking like that and if you haven't that's okay. As a beginner try not to worry too much about this. You should really be thinking about. One thing and one thing only and that is. How exactly. Can I use my current knowledge to solve
Game problems. So again I just wanted to reiterate that. There are many ways. To solve problems. And that's basically it. I'm gonna go ahead and upload this to GetUp. So please feel free to download that. Well that's all I have for you in this episode. Thank you so much for joining me. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day