GameMaker Studio 2.3: FUNCTIONS! (+ Method Scope & Static Variables)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
area in this video we be learning about creating functions in game maker studio 2.3 before 2.3 each function was a script so if you wanted ten functions you had to create ten scripts now that is no longer the case functions now look like this and can be placed anywhere in the code so I'll go into GMS 2.3 which as of this video is in beta from the asset browser I'll create a new script I'll name this shoot if you go into the script you'll see a function with the name shoot so now you can fill in the contents of your function here so for example I'll add this here we are creating a bullet instance we are storing its ID in this local variable and then we are returning that ID so we are creating a simple script like we always said but now it's in a function now the function name here doesn't have to be the same as the script name so you can name your script something and you function something else and then you can simply call the function by doing this you can also get the ID of the function by simply entering the function name without the parentheses now back to our function if you want the function to take arguments you can do so here so you can simply enter your arguments inside the parentheses and then you can use those arguments anywhere inside the function so you can see how easy it is to setup and use arguments now instead of entering argument 0 and argument 1 we can simply enter the names directly now another new feature for the new scripts is that you can enter more than one functions inside one script so below the shoot function I can simply enter as many functions as I want this means that you can now grow similar functions inside one script file now here we are simply entering functions inside the script and the functions are created when the game starts so what if instead of functions we created other stuff like variables so in the script here I'm going to create a global variable this will be the player name so this global variable will also be created when the game starts any code you put in a script that's nor any function will run when the game starts so you could now create another script for setting up data at the start of the game I'll simply name this in it now in the script you can remove the function and use it only to initialize data you can also set up any global arrays structs data structures and even call functions so I'm gonna call the show debug message function all primes a couple messages and the second one will have the name of the player so when I run the game these messages should show up in the output log and here you can see that they do show up so anything you put in a script outside of a function runs when the game starts now you might remember doing this before 2.3 using the GML pragma function with the global command but now scripts do that by default I'll also go into the previous script and remove the global variable from here now you can also create functions inside objects so I'll go into objects and open the player object now in the object I'll add the create event and now in this event I'm going to create a function so this function will be get name by the way there are two ways of creating a function the first as well and the second a stairs so this way you can assign the function to a local variable a global variable or something else I'm gonna use the first one for now inside the function I'll simply return the name of the object so we first got the object index from the instance and then we got the name from that object you can now use this function anywhere in this object so I'll print the name to the output log using show debug message I'll also print another message before this and now I'll run the game I'll go into the output log and here we see the name of the player object which is all player so the function is working correctly now something special about instance functions is that they are bound to these stars that they are created in so this function is bound to the player and belongs to it so this means that this function will always run in the player unless bound to something else so no matter where you call this specific function it belongs to the player and will always run inside it now to demonstrate this concept I'll go into Oh enemy now in the object alert the create event now in the event I want to get the get name function from the player so for that I'll do this here I'm cutting the getname function itself from the player that's why I don't have any parentheses at the end if I did that would call the function and get the name of the player so we're only getting the function itself now I'm gonna try to use this function to get the name of the enemy object so I'll print the same messages here that I did in the player now I'll start the game and go into the output log here under enemy name we still see or player so we ran get name in the enemy and still God or player that's because as I said before the function is bound to the player so to run it in an enemy we need to bind the function to the enemy stance so we can do that with the method function this creates a copy of a function bound to a new instance so we can use this to create a copy of the get name function and bind it to the enemy so I'll come here and remove this part instead I'll add this now I'm using the method function to create a copy of the get name function from the player now here we pass in the ID of the instance where the copied function will be bound and we are passing in the ID of the current enemy stance so now here we got a copy of the player's cat named function bound to the current enemy strands and when we call it here it will actually run in the enemy so to destroy it I'll run the game and then go into the output log and now under enemy name we actually see Oh enemy so the method function did its trick so through this example we learn two things one instance functions are bound to the instances they create a ten and two you can use method to copy a function and bind it to another instance they function inside a script and that function will be global and a global function will not be bound to any particular instance and so a global function will run in the scope of the instance that's calling it so as an example inside the script I'll create a function this will be global get name and inside the function it will do the same thing it will get the name of the object so now this function is not bound to any instances so you can call it anywhere and get the correct answer now we be learning about static variables static variables can be created inside functions and they are only initialized once so as an example I'll create another function inside the script let's say this is a function for drawing a box and let's say the width of that box has to be random so to get a random width for the box I'm gonna do this I'm getting a random integer number between 0 and 22 and then for now I'm simply gonna return the box width now you can see that whenever I call this function a random number will be selected for the Box width so it will be random on each call to test this I'll go into the player object here I'll add the step event in the event alcohol show debug message I'll be printing the return value of the draw box function so we should get the width of the box which will be printed to the output log so I'll run the game and go into the output log here we simply see random values being printed every step so whenever we call the function a random value is selected for the box width but I only want one random value so this is where we can make use of static variables now I'll go back to the script and then come down to the draw box function now instead of making this a simple local variable we can make this a static variable this way the initialization for this variable will only be done once so no matter how many times you run this function this will only run once so I'll run the game again and go into the output log and now we see one single value being printed every step so a random value is selected once and then it's used in all the function calls now this is particularly useful if you wanna get the width of a sprite or any other property from an asset so this way this function only runs once and saves you some performance another useful feature is that you can pass a function as an argument into another function so this can be useful for callbacks and other stuff so that's it for this video in the next part we will be talking about structs and constructors until then check out my other videos in this playlist also do take a look at my crafting udemy course make sure to subscribe to catch my future videos and I'll see you in the next one
Info
Channel: GameMakerStation - Matharoo
Views: 10,691
Rating: undefined out of 5
Keywords: 2 3, gms2, gml, code changes, new version, version, gamemaker studio, gamemaker, upgrade, 2.3, callback, static, gm2, gms, gamemaker 2, game maker 2, scripts, gamemaker studio 2, new, gml changes, ide, functions, methods, changes, updates, tutorial, gm 2, arguments, gms 2, update, scope, variables
Id: BH4kX57w8aM
Channel Id: undefined
Length: 10min 9sec (609 seconds)
Published: Tue Apr 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.