DYNAMIC Doors & Switches w/Variable Definitions - GameMaker Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and now I'm gonna open up this bridge and I'm gonna copy this ID I'm gonna open up this bridge start go to variables under bridge and I'm gonna edit it and I'm going to paste this in so if I go ahead and hit play you'll see that what's gonna happen is this bridge start is gonna look for that bridge and and it's going to recreate as many bridge body pieces as it can through it and it's gonna connect to that end but all I had to do is drag this start piece in this end piece specify their connection and the code ran all of this for me hello Joe welcome to another game maker tutorial in today's video we're going to be going over how to use variable definitions to create dynamic and reusable code and how we can use this to create doors and switches just this topic alone is a really large topic and there's so much that we can showcase with this but I'm gonna give one example in this video and hopefully this will be enough to kind of get your mind stirring and starting to have you think of all the different ways that you can implement this in your game so the first thing I want to show off is what are variable definitions so here I have a couple objects a door and button and this is what we're gonna implement in this video is a door button switch system you've probably never noticed this variable definitions button here at the bottom or if you've noticed that you haven't clicked on it I know I was like that for years and I didn't learn about variable definitions until a few months ago it really is just a fantastic feature that you should start implementing as soon as possible in your game so if we click on variable definitions you'll notice that we get this panel here and that looks relatively vague and empty and if you see this without any sort of background information you're not really gonna know what to do with this if we go back to our room though if we click on any instance so here we have the player for example if I go ahead and double click on this object we start to see a bunch of information tied to this instance here in their room so we have some sort of instance ID we have the object index that it is we can click on this and we can edit the object right in the room and start working with the code we can also change the draw color here if we go ahead and start we can start yeah we can't change the image blends we can set the starting rotation the starting flip so there's a lot of different stuff we can do here if we want an object to start off in a room in a very specific manner and so if we click on this variable button here you'll notice we get a screen very similar to the one that we saw in our last window this kind of screen here and I'll actually go ahead and open up the obj player so we can see exactly which one I have implemented here so in this system I've actually used this variable definitions feature to allow me to implement local co-op in this game so for example I'll go ahead and show what it looks like if I go ahead and drop another obj player in this room right here next to them if we go ahead and hit play we'll see what happens I will be able to control both of the characters with the keyboard as though they are just two of player ones for example so if I hit left or right and I jump I get control total control over both of these characters here and this is not necessarily what I want I want one player to control one character and another player to control the other character so what I've done is I've created this variable definition called PID which just represents player ID I've set it default to zero and you can define the type of it so this is an integer value but for now I just say PID is equal to zero cool and now what can I do with that well if I go back to this object in the room and let's say this is player two I can open up variables when I hit this little pen here which just says I want to customize this and I'm gonna just toggle this up to one so now player ID on this character is equal to zero and player ID on this character is equal to one well the way that I've implemented my code is now it will see that those are two different player IDs so if I move this guy with the keyboard left and right and I can move this guy with the arrow keys now there's a lot of code running in the background to make it so that these two characters operate independently but the only thing that I had to do when I wanted to implement this feature was to just come in here and change this PID variable definition okay so here is a room from totem and you'll see that we have a couple enemies in this room so if I double-click on any of these enemies we see our instance properties window and in this if I click on variables you can see that we have a variable called state and this is set to a resource which is tied to a script called scr graver sit so that means when i run this game this graver or this enemy will start in its sitting position now let's say i don't want him to start in a sitting state i can go ahead and edit this come over here and i'm gonna say graver pace so now when i run this game this graver will be pacing around and not just sitting down i can then go over to this object here open it up variables let's say I wanted to start sitting but I want to start with his back to the that where the player is going to come from the player is going to come over here so I want his back to the player that way the player has a chance to sneak up on him so this is just another example of the really really awesome things that you can do with variable definitions but in this video we're gonna showcase how you can set up a door and a switch using variable definitions without having to write a lot of code and doing it all inside of our variable definitions I'm going to just quickly drag in a door and a button that I've created now there's no code in these objects so far so they are just empty objects with some basic sprites and I just created them for the sake of this demo but we're gonna drop these in here we're gonna write some code and I'm gonna show you how we can have multiple doors and multiple buttons and all we have to do is change a variable definition in this room editor okay so let's go ahead and drop one more button so what we're gonna do is we're going to say that this button it needs to be tied to this door and this button needs to be tied to this door actually let's just make it simple let's just set them apart from each other I'm not going to setup collisions or anything I'm just gonna demonstrate that we can use it to open it okay so let's go ahead and open up our door objects and I'm just going to create a setup a create event and I'm gonna just define a few properties to start with okay so here I just set up a couple properties I just set image speed and image index to zero and then a variable called open that's equal to false and then I just say my image index is equal to open so if it's open then this will be true which will be a of one which means our image index will be one but if open is false then that will be equal to zero which means our image index will be equal to zero okay great so that's our door I'm gonna go ahead and open up our button and we're gonna say create and we're gonna say image speed all right and we're going to set this up just the same way as we did with the door but instead we're gonna come in our step event and we're going to say if place meeting X Y obj player we're just gonna say pressed is equal to if we're meeting this player so if we are touching the player then we're gonna say we're pressed and if we're not touching the player then pressed will be equal to false and then we also want to say image index equals pressed okay great so if I run this I should be able to walk over the button the button will go up and down this will be toggling a variable called pressed and then what we're going to want to do is we're gonna want to tie the door to that variable there okay so I'm touching the button and it presses great the doors don't do anything like I said we're not going to set of collisions in this but you can see that the buttons are reactive to the player running over them okay great so now what we want to do is we're going to create a variable and we're gonna say door equals undefined and this is going to represent the door that the button will be tied to so if I actually changed or to just obj door and now what we're gonna say is we're gonna say if pressed or let's actually just make this real simple we're gonna say if door is not equal to undefined so we're gonna make sure that we have a value assigned to our door and instance exists so again these are just good practices making sure that our code doesn't crash at any point we're gonna make sure we have a door defined and then that object exists we're gonna say door dot open is equal to our pressed value so let's go ahead and run this again and as we run over this okay so as you can see we're getting some weird results and this is because when we run over this button man nothing's happening but when we run over this button our doors are opening so it actually looks like our doors both of our doors are tied to this button and no door is tied to this button over here so how do we avoid this from happening well this is where we're going to use variable definitions instead of defining our door object in our create event we actually want to define it in our variable definitions so I'm going to go ahead and open up variable definitions and we want to make sure that we remove this from the create event because if it's in our variable definitions we don't want it in our create event so that's why I commented it out so we want to open up our variable definitions we're gonna say add and we're gonna call this variable the same thing that we called it before we're going to say door so now we have an option of choosing what type of variable this is is it a real integer string boolean expression resource list color we are going to actually specify this to be a real value and this is going to be defaulted to undefined I don't know if this will let us set it as undefined let's run it okay so we set it to undefined and if we notice our buttons are not working and if we go back to our room editor and we double click on this button and we go to our variables you can see that door is set to undefined by default okay so how do we tie these things together well this is very very simple and when I show you it's gonna blow your mind or you guys ready we're gonna open up our door here we're gonna take this instance ID we're gonna copy it we're gonna hit edit and we're gonna paste it right here now we're gonna save it and I'm gonna hit run one more time so if you remember this button before was not working on either of the doors and this button was tied to both of the doors but now if we click on this this is tied to this door and this one is not doing anything so if I close this one again and I double click on this button can I double click on this door and I copy this and I come into here I say variable definitions let's edit it let's paste that instance ID let's run it again now when I step over this button this door opens when I step over this button this door opens so that's it that's all you have to do this is such a simple implementation but it's kind of hidden in this variable definition functionality that a lot of people don't even know exists now I do want to specify a couple things this instance ID that is generated here is different for every object so if let's remember what this is so this is 220 d-57 70 okay so I'm gonna delete this object I'm gonna come in here I'm gonna drag another door and if we notice the ID is now different so this ID is generated randomly every time we create a new instance so if I go ahead and try to run it again we're gonna see that the button is not going to work on this door because the IDS are not the same so I goes this one switch works out I guess we accidentally deleted that button so either way you guys kind of get the point this is not gonna work because the IDS don't match up you'll see even the button has a new ID now so one thing that we have to keep in mind is that if we ever delete an object we have to go through and update the instance ID for those connections otherwise it's not gonna work I do want to show one more example and something that I've implemented in this project so I'm gonna go ahead and show that real quick let's drag our player over here and so let's I actually created a piece of code for a physics-based bridge so I'm gonna go ahead and create this so the way that I've done this is to implement the same functionality is I've designed a bridge start and a bridge end so I'm gonna go ahead and put this bridge end over here and now I'm going to open up this bridge and I'm gonna copy this ID I'm gonna open up this bridge start go to variables under bridge and I'm gonna edit it and I'm going to paste this in so if I go ahead and hit play you'll see that what's gonna happen is this bridge start is gonna look for that bridge and and it's gonna recreate as many bridge body pieces as it can through it and it's gonna connect to that end now obviously there's still a little bit of overlap here because I haven't flushed out all the bugs with it but all I had to do is drag this start piece in this end piece specify their connection and the code ran all of this for me and I don't have to go through and place each of these pieces individually or specify their connection especially for an example this where all of these pieces are interconnected using physics joints so I don't want to have to specify that with any sort of code I want that to be all dynamic and done automatically for me so by using variable definitions we are able to do this very easily and very simply anyways guys that's it for this video I hope you found it instructional I hope you found it helpful let me know how you implement this into your game and if you have any feedback please leave it in the comments hit that thumbs up button subscribe and I will be back with another video very soon [Music]
Info
Channel: GentooGames
Views: 1,338
Rating: undefined out of 5
Keywords: doors, door, switch, switches, button, buttons, variable, definitions, definition, gamemaker, how, to, make, create, setup, program, code, in, studio, start, starting, variables, vars, var, lever, levers, connect, connecting, two, objects, object, connected, sync, hook, up, hookup, shaun, spalding, heartbeast, heart, beast, friendly, cosmonaut, friendlycosmonaut, shaunspalding, pixelated, pope, pixelatedpope, development, gamedev, indiedev, pixel, art, gms, gml
Id: GA7QlbkB9ek
Channel Id: undefined
Length: 13min 56sec (836 seconds)
Published: Tue Sep 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.