GameMaker Studio 2.3: *OOP* GML with Structs & Constructors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
heylia in this video we'll be looking at structs and constructors in game maker studio 2.3 in GML if you want to create a group of variables you can use an array so you can simply enter values into that array and they'll be numbered from 0 now when you want to read those values you have to remember the numbers or use an enum but now in GMs 2.3 this is made easier with structs to create a struct you can use these brackets then inside the struct you can create variables like this then you can stow that struct in a variable or pass it into a function or do something else now inside a struct you can create any kind of variables like a real a string a data structure and even a function and you can also store a struct inside the struct so this is basically what structs are they allow you to group variables together and then we also have constructors which are basically templates for creating new structs so we can look at both of these in this video I'll go into GMs 2.3 now in this object here I'm gonna add the create event I'm gonna create a struct here and this will be used for storing the speed of this object so inside the struct I'm gonna create an X variable and a Y variable make sure to use the colon sign here nor the equal sign now we have a simple struct here with two variables x and y these are separate from the x and y variables in the player instance we can now read variables from the struct like this here I'm simply taking the X from the my speed struck and adding it to the X from the instance now you can also modify any struck variables like bells you can also add to it or do anything else that you can do with a variable I'm gonna remove these lines now and in the my speech drug I'm gonna give values to the X and y variables then down here I'm gonna use the show debug message function I'll be printing the my speed struct to the output log I'll run the game now and go into the output log and here we see our struct with the x and y variables so this is how a strap is created not a deleted you can do this so in this case the my speed struct will be destroyed but it's a little more complicated than that see this here is a variable that stores a reference to a struct it's the struct created here so we have a reference to destruct in this variable and so the struct is alive but if this reference is lost then the strut will be destroyed the reference will be lost when you destroy the instance or if you send this variable to something else so in that case the strut will be automatically destroyed of course unless you have the reference saved into some other variable the strut will only be destroyed when all references to it are lost so now coming back to this line that lead keyword simply removes the reference from this variable we can now test this by placing another show debug message call I'm gonna print the same variable again then I'll run the game and go into the output log and here you can see that we get undefined so the variable is set to undefined and the reference to destruct is lost since it was our only reference the strut was destroyed this also means that you don't have to use the delete keyword to destroy a struct if the references are gone then the struct is gone as well of course you will have to be careful with any dynamic resources in a struct like India's list or a surface you still have to destroy these resources manually if you don't do that then there will be memory leaks now I'm gonna remove the list and the surface from the struct in this truck I need a function for adding to the X and y variables so I can create a function called add in destruct but I'm not gonna do that here instead of adding everything here in destruct we could create a template and then we could keep creating new structs from that template so that can be done read constructor functions a constructor is a function that creates a new truck so to set up our constructor I'm gonna create a new script I'll name the script vector to I'll go into the script and now there's a function called vector - I'm gonna make this a constructor by doing bears so now this function is a constructor and can be used to create new struts you can also say that this is a template or a class for structs now a vector2 struct needs x and y values so we need to set those up in this constructor they'll be taken as arguments so I'm going to enter them here and then inside the constructor we can create variables for storing the arguments so a nice trucks created from this constructor will have these x and y variables and now we can create any kind of variables in this constructor so I'm gonna create a function called add this function simply takes another vector to as the argument then the X and the y from that vector are added to the X and the y in this vector so now this constructor is ready now I'm gonna go back into the object in the create event here I'm gonna remove this and then this and now I'll create the my speed struck using the vector two constructor the X and Y will be zero so this is creating a new struct using the vector two constructor and then we are running short message on that struct so now I'll run the game and go into the output log and here we see a Las truck it has X Y and the add function so it has everything we set up in the constructor now we are gonna use the add function in Avenue truck so I'll go back in the create event and on this line I'm gonna add this here I'm running the ad function into my speech truck and in the argument I'm passing in a new vector to the X and y for that vector are hundred and ten so these will be added to the x and y into my speech truck so I'll run the game again and go into the output log and now the X and the y in disrupt are hundred and ten now here we are basically converting our struct into a string and printing it to the output log now there is a way to customize this output in the constructor I'm gonna create a two string function now in this function I'm simply gonna return a string so whenever you convert a strut into a string this function will run of course only if it exists and so what you return here will be the output so we should get a simple message that says this is a vector over DX and a by so again I'll run the game and go into the output log and now we see our customized message now another feature of structs is that you can get the name of the constructor that you use to create it we can do that with the instance off function so here I'll do another show debug message in the argument I'll use the instance off function and I'll run it on the my speed struct so the show tell us which constructor we used to create this struct so I'll run the game and go into the output log and you can see that here we get vector 2 that is the constructor that we used to create the strap so this where you can basically know the class of Istra now struts also support self this is how you can refer to a struct inside that struct self is like this in some other languages let's say you want to add the struct to a list when you create it so you could do it in the constructor like this this way a reference to the current struct will be added to the vectors list now this was only a quick example so I'm gonna remove it now something great about constructors is that they support inheritance so you can create a constructor that inherits variables and functions from some other constructor so as an example I'm going to create vector 3 which is gonna inherit from that the two so down here I'll create another function and this will be left at three now this will be three dimensional so in the arguments I'll enter X Y and C now to enable inheritance I need to enter a column sign and then the name of the parent constructor so this will be vector two and now we need to specify the arguments for the vector two constructor so they are x and y so here's what we're gonna do we can eject the X and the y from the vector 3 constructor and pass them into the vector 2 constructor so this way that parent constructor will handle the X and the y now we can just enter a constructor and create a block so of course this is the constructor for vector three here you only need to setup the Z the X and the y will be handled by the parent constructor they'll simply be set to the values that we are passing in here and also everything does in a vector two will also be in a vector three including all the functions now I can go into my object and query vector three here so first I'll remove these shorty bug method calls I'm going to create a variable called position which will be a vector three the x y&z will be 50 40 and 30 and then I'll run show debug message on the position fraud so now I run the game and go into the output log and what we see here simply says this is a vector with X&Y it doesn't mention the Z now this was happening because vector3 is still using the to string function from dr. too so that's why we only see x and y this means that we need to create a new two string function inside vector 3 and since we are doing that we should also recreate the add function so inside vector 3 I'll create an add function which accepts a vector 3 so it will simply add the x y&z and then I'll create a new tutoring function fathers are simply copied the return string from here and add the Z at the end now I'm gonna make use of the new add function I'm gonna go back to the first workspace into the create event and now on this line in the position struct I'm gonna run the add function I'll simply pass in a new vector3 so this addition will happen and then the truck will be printed to the output log I'll run the game now and go into the log and now we see a proper message with the x y&z so this is basically how inheritance works another example is that you can create a base item class and then create items that inherit from the base class now we also quickly gonna go through static variables in my video on functions I explained how static variables work static variables are basically shared across all calls of a specific function so of course you can also create them inside a constructor for the demonstration I'm first going to create a normal variable inside vector 2 this will be count and it will be set to 0 now on the next line I'm gonna increase it by 1 so the variable will be initialized at 0 and then 1 will be added to it so this means that the count will always be 1 for all vector 2 structs now I'm going to go into the to shrink function and comment out the data online I'll add a new one which simply tells us the count and now I'm gonna go back into the object I'm gonna remove all the code from this event and now I'm gonna run show debug message I'll run it on a new vector2 I'll also press control-d a few times to duplicate this line so now we should see the count variables of all these struts so I'll run the game and go into the output log and we see the counts of those new vector2 trucks they are all simply won as they should be so this is where we can make it work differently using static variables here I'm gonna make the count static so now this means that this variable will only be initialized once so it will be created at zero but only once but this line will run whenever the constructor runs so I'm gonna run the game again and see what happens I'll go into the output log and you can see that the count keeps increasing with each vector to call so on each called account is not reset to zero that's because it's static and is only created once so now we can make use of tatak to save some memory when you create a new vector2 these functions are also created again so if you have many vector to structs then these functions are going to take some more space in the memory it's usually not a big deal but you can save that memory by making the function static so I'm gonna make the add function static and also the tostring function so this way these functions will only be initialized once now finally you can also use struts to set up some global data if you have seen my functions video you'll know that you can set up global data in a script so having structs also makes this a lot easier for example you can have all your game settings inside one global struct and so you can create more structs for storing more kinds of data so that's it for this video I hope you liked that and if you want to learn more about game maker check out my other videos here and here if you want to catch my future videos make sure to subscribe here and I'll see you in the next one
Info
Channel: GameMakerStation - Matharoo
Views: 14,006
Rating: undefined out of 5
Keywords: gamemaker 2, light, classes, weight, changes, version, ide, gamemaker studio 2, constructor, code changes, new, update, gms 2, object oriented, gm2, gml programming, gamemaker studio, 2.3, 2 3, objects, gms2, structs, gamemaker, new version, upgrade, constructors, game maker 2, gml changes, gml, updates, methods, tutorial, oop, gm 2, gms, functions
Id: MKgDkhKC050
Channel Id: undefined
Length: 14min 28sec (868 seconds)
Published: Thu May 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.