[GMS2.3] Structs and Constructors : OOP in Gamemaker Studio 2.3 | FunBox tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody it's blaze from funbox here and with the release of gms 2.3.0 i figured i would ask the communities that i'm a part of what kind of tutorial videos they wanted me to create naturally some of them are also subscribers to this channel and watch my videos and so i got a few questions asking me about the state of the term based combat tutorial series that i had announced at the start of the year to keep things simple i have an update for you guys at the end of this video so if you are curious about it and you want to find out more as to what's happened to my little tutorial series wait until the end watch this video straight through to the end okay moving on one of the suggestions that some of the people made that stood out to me was actually structs and constructors these are new to gms 2.3 and so i figured i'd cover them in a fairly beginner friendly video if you've worked with structs and constructors in other programming languages that's great you've got a head start which is always an advantage but keep in mind that these are a little bit different to how you may have written them before all right let's take a look at what we are working with we have two objects and one script that's all we're going to be working with today let's take a look at our controller first since we're only going to be looking at it once and all it does is in the create event it just randomizes everything let me center this for you guys so you can see easier and when we press enter it creates an instance of o character at some random point in the room so let's close this off and now let's take a look at our o character for o character we have a create event where we set the my name and my health don't worry about this array for now that's that's part of my explanation but basically the mind name chooses a random name and sets the health at a random number we also have a draw event where it draws its own sprite and draws that health and name text right next to it when we press space it calls the function add health let's take a look at that script now okay here it's very basic all it does is it just adds health to the character and that only happens when we press space let's try playing this demo of ours and let's see how it looks alright here we are in game i'll press enter creates a character and when i press space it increases the health by 1. alright so that's basically what it looks like let's create a few more characters and let's all increase their health okay so we know that that works and so we can close this out alright let's get into the main bulk of this tutorial in past versions of game maker studio you may have created your objects whether it's a character or a weapon or a car or whatever objects you have in your game you may have created them like this top version that we have here where we name the variable in this case my name and my health and then we put whatever code we want to assign it that works that's fine or you may have used an array where we basically do the same thing this is okay too they both work and they both have their uses they definitely have their uses the difference between these two methods and say using a struct is that we can basically put all of this information inside of a struct as well as functions that are limited to only being used with a particular structure or struct for short let's take a look at creating our first struct now but before we get into that we can just comment these two lines out since we don't need them anymore all right to name a struct what you're going to need is the struct name itself i'm going to call mindstat we also need the assignment operator or the equal sign and for the absolute minimum we need an open and closed curly brace and that's it there's your struct right there the cool thing is even though this is an empty struct you can add things as you need them which is very convenient something you can't really do using this method that we have up the top here and it is very difficult even though we have a way to do it now in 2.3 to add that to an array that you have here all right let's keep going and we can actually prove that this stat exists in our object by using a debug call all right let's test this out what we're looking for is something to come up here in the output window down the bottom all right let's press enter and see what happens okay great we've spawned a character in and here on the side we have an empty open and closed brace okay great it works we know what it looks like or what it should look like and that's enough for me let's close this out and let's keep writing our struct out let's fill in the information for our name and health generally i would tell people not to copy and paste code even if it is something that they've already written but in this case to keep this video short or as short as possible i'm just going to do it for now okay i've cleaned that up a bit and you can see that we have some errors inside of our struct here and that is because when we are setting up things like our variables inside of a struct i'm just going to stick to calling the variables for now we actually don't use the equal sign instead of that inside of a struct we use a colon and when we have multiple items here like name and health we don't use a semicolon at the end we just use a regular comma okay good that's looking good we have no errors let's try playing our game now let's press enter to spawn a character in and we can see here that we've spawned in a han character with 7hp but the struct down here says boba with 10 hp so what's going on here let's close this out and what's actually happening is in the draw event it's drawing my name and my health these two variables are not the same as these two down here even though we know that code is read from top to bottom now if you're working with the knowledge that you've learned from game maker studio in the past then strictly speaking by logic you should just comment this out and by running the game it should work before i press enter i can guarantee you that we are going to get an error and there we go we can see it here now it says here that the variable o character dot my name is not set before reading it what does that mean exactly now the thing here is that although we have the variables my name and my health inside of our o character it actually belongs to a separate object can you guess the name of that object if you guessed that the name of the object was stat then you're absolutely correct basically what a struct is is its own object it's a what we would call a lightweight object to be able to access the my name and my health variables you would have to use dot notation let's try doing that now it's very simple all you need is the name of the struct followed by a dot that's it let's do the same thing for my health okay it's all done let's save that out now and let's try running our game okay here we are and when i press enter there we go we can see that we've spawned an evader character with seven hp and down here in the output window it's the same thing okay let's close this out since we're done with it now let's keep going like i said earlier we can actually have a function inside of a struct and what we're going to do is we're going to actually move this add health into the struct here so that we can use this instead so let's try doing that now to create a function inside of a struct it's basically the same thing as what we have up here and that is simply by writing the name first followed by the colon the keyword function brackets for any parameters that you might need and then at least an open and closed curly brace don't forget your comma there at the end alright so there's what we have here now the reason why it's come up yellow here the text is because we actually already have a function called add health outside of it so even if it's outside of the script it still wants to be able to know where it is okay so what we're going to do is simply we're just going to comment that out and we are going to write inside here a way to change this my health directly and to do that it's really simple to do that all we need to do is start off with the word self the keyword self in this case ensures that whatever you put after this dot belongs to the same object and the same instance and it's just a safety precaution but definitely something that you should use in this case we want to change our my health variable and so let's do that okay there we go we have no errors now the only thing left to do here is to actually change here we don't have dot notation for our add health function call and so let's do that now okay really simple really quick change let's try playing our game and see if that works okay here we are we're in game let's press enter to spawn a character in actually let's spawn in a few characters okay now let's try changing their health there we go we can see that everything changes relative if you notice from the start of this video that we had quite a few instances and when i press space they all changed their health to be the same level that's a big problem for us but you can see in this case whenever i press space everything changes relative to what they were in a previous value let's close this out and the reason why it changes relative is because we're actually using this self keyword here it's very useful to know this before we move on to constructors which is the last part of this video i want to quickly cover an alternative way that you can access these variables inside of a struct without using dot notation to read a variable inside of a struct we can use this function here for those of you who have worked with structs in other programming languages this here is your getter basically what it does is it reads whatever value you put inside here be aware that you need to have a struct name and the name of the variable itself before you can use it the other one is naturally going to be set and basically it's the same as get accept rather than reading it it writes whatever you put in here structured very similar to the get the only addition here is that you need the struct of course you need the name that you're going to set and also the value that you want to set it to seeing as i want to keep this video short and beginner friendly but it's good to know that they exist and that they are there okay the last part here is of course constructors basically a constructor is what creates a struct however unlike writing up a struct we need to create our constructor outside of the object that we're going to make it in so let's go over to our character script and let's write our constructor function let me center this for you guys so you can see it a little easier and let's write this out so to write a function all you need is the function keyword first the function name i'm going to call mine stat open and close brackets and the keyword constructor this tells game maker studio 2 that this is indeed a constructor and not just a random function and don't forget your open and close curly braces okay it basically works to create one of these at the very start now the reason why it's yellow text here and not blue text is because we actually already have this stat over here so basically what we're going to do is we'll we'll just comment this out okay so that's done we don't need that anymore we don't have to worry about it now to get this constructor inside of our game object what we need to do is we actually need to call it and we need to store it inside of a variable so in this case since our constructor is called stat i need a new variable name i'll call mine unoriginally i guess stats we need the assignment operator or equal sign and we need to use the new keyword this is all a part of using constructors instructs here in game maker studio as well as other programming languages and basically what it means is that it needs a new version or a new instance of a struct in this case we need a new stat so this works now we see that we have no uh references to this and we can debug it by taking this line of code here however we have a bit of a problem here we are calling for the stat function or the stat constructor rather than the variable name itself so let's fix that okay so we're expecting it to print out something now however before we actually test that out we need to comment this out otherwise we will get an error and we don't want that so let's quickly do that and now let's try testing our game remember all we're looking for is some sort of output in the output window some sort of text let's try spawning our character in now and we can see just like before with the structs we have an open and closed curly brace fairly simple it's basically the same functionality as a struct i'll get to the difference in just a moment let's try filling this out with all the same information that we had in our struct from before so basically again it's a copy paste unlike structs for constructors when we create variables here we actually need to go back and we need to use the equal sign as well as the semicolon at the end of the code we need to get rid of all the commas here and change them as appropriate all right so now it works and everything looks to be in working order okay that's good now that everything's set up let's minimize this and now let's go over back to the draw event uncomment this line and instead of stat we're going to change this to stats and we're also going to change this to stats okay that's all set up and again over here when we press space we're going to change that to stats as well let's now test our code and see if everything runs smoothly and without fail alright here we are in the game window let's spawn a couple of characters in no problem so far now let's try changing their health great okay so looks like everything works out just fine so now we get to this last part here what's the difference between using a struct and a constructor one of the advantages and one of the biggest advantages of using a constructor is we can actually set some parameters here so let's say for example we only want to be able to create i don't know luke we can take this code and i'm just going to comment this out now and let's comment this out as well we can put some parameters in here to influence what will come up for my name and my health so for example we can write this and of course we need to use them in our constructor somewhere okay that's good it works it looks fine but of course we need to add some extra parameters inside here so let's say we only want to create a loop and we want to only give him 10 help always this works but it's basically the same as what we had up here however keep in mind one advantage is that we still have access to this add health function let me show you what i mean here when we have it set to just luke and 10 hp when i spawn a character in it's always going to be loop and when i press space it will always increase at the same rate so how do we end up creating multiple different characters within a single button press with a little extra work and yes i'm about to ad lib this into this video but i might as well do it since i said it what we can do is we can create an initialize function like so so you might be wondering what is going on here it's basically going to be the same as up here that might be true but let's try deleting this code here going over to here and let's try erasing this all right since this is all ad-lib this is unscripted i have no idea what's going to happen next but basically what we're going to do now is we're going to create a random spawner so let's go into controller here and when we press enter instead of creating a single instance let's store it in a temporary variable here a local variable i'm going to store mine into character here and then i'm going to run a width statement let's add some random element to this and let's do this okay so basically what i'm gonna do is create a random enemy spawner so what we're going to do is when we spawn a character we are going to roll a random six-sided die and if the number is less than let's say 4 character dot stats because remember we are storing our constructor our struct inside of this variable called stats dot initialize and we are going to spawn now dot initializes this function all the way up here yes it's getting very messy now but just bear with me for a second we have dot initialize and obviously we want to set the name and the health okay so that works or it should work let's try putting an else statement in okay so we have some sort of random monster generator here i guess and instead of just spawning one random character why don't we do this as a loop so basically what this code is going to do is it's going to spawn five characters at random and depending on the number that we get if it's four if it's less than four so zero one two or three is going to spawn a wookie and if it's anything far or higher then it will spawn a storm trooper so i've written out all this code but before we try to run it we actually need to make a quick fix up here in the character constructor we need to change these two variables because right now it's going to throw us an error because it doesn't really know what to do with underscore name or underscore health so let's change these to zero for now all right let's save that out and let's try running our game okay so here we are in our empty room let's try to spawn let's see how many characters spawn when we press enter okay there we go we have we have one two three wookies and two stormtroopers there we go that makes that's that's interesting there let's try pressing space now and we can see that when we press space the health increases relative to each other what we've actually created here is a very basic factory here it's a factory game object and i'm not going to go into it too much because i think i've done enough ad libbing but basically a factory is a way for you to be able to spawn multiple game objects into your scene at a very low cost so imagine having to initialize a wookie and a storm trooper every single time you had to spawn it you can skip all of that extra work by creating a constructor calling it into a particular objects create event and then having that set up basically automatically it makes things a lot easier and when it comes to runtime events it's a lot faster much easier than having to set things up manually so i'm just going to leave it here for now i think i've done enough extra ad lib for this particular video i gotta admit it's great to be back making videos and if you guys like this video then i would very much appreciate a like if you guys want to see more videos in the future hopefully i can get them out once a week then please subscribe and turn on your notifications if you guys have any suggestions then you can always leave them in the comment section below of course i do have a couple of other videos that people had requested from those community groups and so i'll get to any suggestions i have here after i'm done with those ones if you have any questions you kind of want to get extra clarification then of course you can always ask them here but that's it for me guys thank you so much for watching and i hope to see you in the next one bye bye what's up guys it's blaze from fun box here and if you're watching this part of the video then you're interested in the turn-based combat tutorial series that i had planned or i at least announced it at the start this year in short what's happened is moving it from past gms2 versions to 2.3 has actually broken a lot of the code fortunately for us though i've managed to fix most of it however there's still a lot that needs to be fixed specifically and if i run this game specifically what needs to happen is when i click to attack i can't actually find my buttons that will actually allow me to attack the enemies but i'm working to get that fixed right now another thing that has broken and we can't actually see it here because it is that broken is the animation control so for some reason i'm not entirely sure what's happened but playing animations in previous versions has basically broken so i have to recreate or i have to rewrite the animation control code everything else however works and this is more or less what it's going to look like visually i know it's not the most impressive looking thing but i just wanted to have something that's prototype ready that you guys can definitely build off of so that's basically where we're at in terms of this tutorial series or where we are in terms of me fixing it i hope that uh you guys are patient with that if you are still interested in it i'm almost there we're almost ready to go so thank you very much for your patience if you have been waiting for that and if not if you've already lost interest then well that's that's entirely my fault i know i've been working on it uh as much as i can but life gets in the way anyway guys thank you so much for watching if you guys haven't already subscribed then hopefully today with today's video i have earned your subscription if you guys have any suggestions or some comments then feel free to leave them in the comment section below that's it for me guys thank you so much for watching and i hope to see you in the next video bye bye
Info
Channel: FunBox
Views: 4,125
Rating: undefined out of 5
Keywords: BlaizeArtz, SixT4Pixels, Blaize'nSix, Blaize, Nic, Yosshi, game maker studio 2, game development, sixt4pixels, gamemaker 2.3, gamemaker studio 2.3, gamemaker studio 2.3 structs, gamemaker studio 2.3 tutorial, gamemaker studio 2.3 beta, OOP, object oriented programming, factory programming pattern, beginner programming tutorial, game maker studio beginner tutorial, game maker studio 2 beginner tutorial, gamemakerstudio oop, gamakerstudio 2 oop, gms oop, gms2 oop
Id: oTke_unJKsc
Channel Id: undefined
Length: 24min 33sec (1473 seconds)
Published: Sat Aug 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.