Gamemaker DND Platformer Tutorial - #16 Room Transitions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're interested in making a detailed platformer just like this one with in game maker using drag-and-drop then keep watching g'day gamers and welcome back to our drag-and-drop platformer series in the last tutorial I showed you how to add scoring to our game which further enhanced the collectable items that we added in the previous tutorial in this one we're going to look at adding room transitions which enable us to move from one room to another we can also use this kind of technique to move through doorways like this and that just opens up your game where you can actually start to have indoor areas as well so I'm moving through like this but let's have a look at how we're going to do this but the first thing I want to do is go to our existing room we actually want to enable the player to leave the room but let's adjust this let's turn on the collision layer and let's move this rock down so the player can actually get past so if I hold down P I can select existing instances and drag this down and then if we go back to the mid layer I can press s and that'll let us select something and ctrl X to cover the up then we can just move that down block here that needs an adjustment and we'll just replace that one since we are looking at our collision layer I'm just gonna put an existing block over here to block the player so ctrl-c ctrl-v and will just block the player from falling off the level on the left now how we're going to do the room movement is we're going to have a transition object here but the player will be article ID with that transition object and inside it it will hold details of where to send the player in what room to send the player to but let's create this transition block go up to resources right click on sprites and say create sprite and I'm going to call it s underscore trigger underscore transition now let's just edit I'm going to just choose a pink color and I'm going to choose around about a hundred for the Alpha let's fill that in and that's all we need for this close it down now we can go and create our object right click the Create objects and it's just o underscore trigger transition now the reason I write trigger first is when you have more than one trigger it's just easy to find them if they were all listed by the name like that now the other thing we want to do it's just untick the visible and select the sprite we don't need visible because we don't want to see this in the room let's go place this in the room just over here if I select the trigger transition hold down alt it'll let me place it and we just want that off the view but the player actually has to walk out of the room to trigger it but now we need another room for the player to move to so the existing one I'm just going to rename it press f2 and call it part underscore level one and I'm going to duplicate this so right click and duplicate and the next one is just called bill two now we want to have the player be able to move to the same height as they were from the previous room so I'm just going to modify this so that we are at the same height bring this one down [Music] just sort of clean it up so that we are at least walking into the same position as what we left now it's just some cleanup here to be done okay now we'll just take this block and remove that now you can modify your level how you like I'm just going to change it a little bit so that I know that I've walked into another room at least and just going to get rid of these things in the background here and just that collision object there at least we know now that the room is different when we walk into it but as I say you can change yours as you like so let's just grab the transition object I'm going to copy that paste it over here because we need a transition to go back to the original room right so that's pretty much the setup done but we need to do some more setup for the player so when a new room is loaded everything in the old room is destroyed unless the object is set as persistent into our game object controls many facets of our game we need that to be persistent but that means once it's created it'll exist in every room now our player also has variables that we would like to move on to the next room at the moment he's hate to pee is something we need to keep between rooms but there could be more in the future and if we were to create the player again at the start of each room we would need a way to copy all of those variables across as the old player would get destroyed and we would create a new one with the max HP but one way is to set up a dummy persistent object to do this copying but what I prefer to do is just to make the player persistent so now he will just be moved to the next room but this can create problems if we look at our first room we are creating the game object and the player object in this room this means if we move to another room and then come back to this one the game and player objects will be created again so we'll have two of them which would be pretty much a mess so with persistent objects I prefer to have an initialization room run some code to create them that way you can check if they have been created before creating them but let's add another room and this one will just be our initialization room now instead of creating a new room I actually want to make it a copy of these ones and the reason being that the camera and viewport dimensions for the game are chosen from the first room so we want to duplicate these in order to get those settings let's duplicate this one and we'll just call it our underscore knish and drag it to the top now when you drag it you might notice that the icon changes to a little light above a lot on the right and a light down the bottom if you let go when it's a light on the right it thinks you want to make a parent of this object so you don't to do that you want to cancel that you want to be sure that you drag it to the light at the top to get it at the top of the room list there now with this room we actually don't need anything in here so we definitely want to delete the player and we definitely want to delete the game object but we can pretty much delete everything else we just don't need anything here I mean it doesn't really matter if these things are here but you don't want to be confused by wondering what they are even doing so you can actually select everything here and just delete it select all the mid things delete those and the tile floor and even the enemies we just don't need anything so what we do need is an initialization object to do the creating of the player in a game so let's go create that it's going to call it o underscore and ish I'm going to drag that to the top so that's on the top of everything else I'm going to put the game up here as well now let's add a create event for this and this is where we can create the player and the O game object now what we want to look for is instance exists we want to check to see if these things have been created already so we go to a game and we say if instant exists of our game is not true then we want to create it very pro game just going to put it as relative and fine to put it on the instances layer so let's just select those copy and paste those and we'll do the same for the player and we'll just put this one on the player layer now after they've been created then we can move to the next room now this object needs to be placed in the room so let's go to the actualization room and we'll just place that just up here I'm going to place it not exactly in the corner but just up there and you'll show you the reason for that in a moment now the last thing we need to do is go to our rooms and actually take away the player in the Oh game object so select the player and delete and hold down P and select this one as well and delete that I'm going to do the same in room level two I'm down P select I don't P and select and hold again P just enables you to choose things that are on different layers so let's run this and see what happens now a game runs pretty much the same as before except you'll notice that the player fell out of a sky here and that's because we are creating the player at the position of here where the initialization of diffuse so that's not exactly where we want the player to be created instead we're going to have to have some variables that move the player to e start location for each room and also make sure he is facing the correct way so in the create event for our game let's create those I'm going to make these some global variables there are some set global and get global variables but I'm just going to use the assigned to do this because I like to type them out it just helps me remember that these are actually global variables the first one is going to be called player underscore start underscore X and then we're going to have player underscore start underscore y now we need a facing variable as well so global dot player facing rich direction so remember one will face to the right minus one will face to the left so I'll just set it to one initially now for the first level we need to work out the position that you want the player to start out so if we go back to our first flight room we can look up here and see where do we want the player to start we go to our player object we can hold down alt and place the player if you zoom in you'll notice that there's actually a small gap I'm going to hold down control and down arrow just to position them in the correct spot now the position is down here it's 30 to 65 so I'm going to delete that you don't want to place it in the room we're just doing it to make sure we get the right position and we go back here and we'll set of that 32 and 65 now we need the player to grab these global coordinates and position themselves in the right place every time they enter a room so let's go to the player and we're going to add a room start event so this will run every time this instance enters a new room so here what we can do is the players X can be designed the global player start X variable so whatever that is set it'll be assigned to the players X when the room starts now we can do the same thing with the y sign that to the why and then we can also assign the facing variable to be a global player facing so that'll position the player in the correct location at the start of each room let's test it out great so we have our player instantly starting in the right location but now we can set those global positions to new values when we collide with the transition trigger and the great thing is each instance of the trigger object can have its own coordinates so each one can be set to send us somewhere different now we can do that by utilizing the instances creation code so if we go back to our room and we look at our transition trigger if I double click on it it has a creation code and this creation code is unique to this instance of the object so this particular one has creation code if I make another one that has its own creation code but let's go in there let's set up where we want to send the player in the next room but we're going to have a variable here called target X and this variable belongs to the trigger transition and this is where we want to send the player and then we'll have a target Y and then we'll have a target facing so these will be given to the global variables when the player collides with it and then in the new room the player will be set to those global positions so in order to set these to something we need to know where we want to set the player in the next room so we're on level 1 when we go to level 2 here's where we need to work out where to send the player so if we select the player select them down there and there's push them down 1 the position is 32 2 to 5 back here we can change this to 32 and 2 to 5 now out facing is just going to be 1 because we're facing to the right now the other thing we need to know is the target room to send them to we will call that variable target room and we're just going to send them to the next level which is our underscore level two and by doing it like this we can also always have a target room and it'll enable us to go back and forth between rooms so now we set that up we need a collision with this object so back in here we go to our trigger transition let's create a collision with the player but when we collide with this instance we need to update the global variables with this instances own target variables and then move us to the next room but the player then will run its room start event and it will move itself to the correct position and orientation okay let's do that we want to set the global dot player start X to be target X and we want to set the global start why to be target why and global dot player facing to be target facing and lastly we want to move to the room called target room so there's one here that says go to room and it's very specific we want to go to target room birds test this out press play and now if we go to the room we can move to the next room and that works great now when we touch that transition object our global variables are set to the transition variables and we're moved to the next room where our position is set to those global variables now if we try to walk back we'll get an error as we need to set the creation code variables for the transition instance this transition instance we just touched it has no target X variable so we get an error let's go to abort let's go to room 2 and this one here we need to set so we need to go to the correction code and we need to set it here let's go back to the other one and just grab this creation code copy that and just paste it here now we need to change where we want to send the player so let's go back to this room and we'll work out the position that the player needs to go to so I click on player position them here and just move them down one pixel and we can see that it's 1 2 4 8 and 1 9 3 now I just noticed that houses one nine three there I think I'm sending us two two two five on this one I am so let's make it one nine three this so that we're in line with the way we're going on the new one so here it was twelve forty eight and one nine three so it must be that I've actually positioned this wrong it should be just one up yeah so let's just fix this let's move this up one actually don't even need that one there now we can just get rid of that and just have the single one and in for our tiles we'll just move these up okay that's better now just in here I think I didn't change target facing we just need to set that 2-1 and set our target room to 1 okay so that's a lot better but let's test that out again at least now we'll be aligned correctly when we walk to the new room but we could turn off our collision layer but it's fine for the moment what about two players there but you'll notice the player on top of players it's good that that happened because that means in this room I've left the player right here so you want to make sure you always delete those that was turn that collusion layer off and just press play to sit out again you right so now we move correctly to the new room but a flying zombie there so hope that's go and move him down he really should be positioned on the floor there and this room we can turn off our collisions so that works great but there's a few things that we can do to fix that transition and make it nicer we can have the player maybe pause when they enter the room it'll make the transition less jarring and you might have noticed that we get some dust happening when we start the game you'll just see just here and we also get it when we move to a new room so let's have a look at fixing those two things so first to pause the player let's create another variable called house control and that will be whether or not the player is able to move let's just go here to our variable definitions and let's add a new variable and I'll call it has control if this is set to true it means the player has the ability to move that's just a boolean and I'm not going to set it to true to start with now I'm also going to create an alarm one I'm just going to set it to 30 and we'll use the alarm to give the player their control back so we just set it to a certain time and when that time expires the code in the alarm will run let's go and create that alarm but this code will run when the alarm expires so let's just set has control back to true but you can essentially set this alarm to a value and at that time the player will not have control now without transition trigger collision now in here when we do a transition we can set a few other variables now there's an apply to block here I'm going to use and the apply to block enables us to apply code to another instance and in this case it's going to be to the other in the collision and the other is the player but let's set some variables on the player I'm going to set the has control to be false so we don't have control at the moment we're going to set the player's alarm to be just let's say 60 so one second and also going to change the players HSP and set it to zero in the VSP and set it to zero and that'll stop the player from any movement they may have already been doing now the last thing we can do is if we can ensure the code actually runs when we have control so let's drag this up and let's say if has control is equal to true then we want to run all of this now if I run this at the moment we're going to get an error because this has control is a variable that belongs to the player yet we're running this in the trigger transition so we need to say other dot has control to ensure that we're referring to the variable in the other instance that the collision is happening with but now that we've set our has control let's use it to stop the player moving when has control is set to false so if we get our scripts and get our get input this is where the player does their movement right here where we are collecting the players input let's put a check here and say that if has control is equal to true then we want to allow all this movement to be captured now while we're in here I'm actually gonna add another variable and I'm gonna add an up and set it to false and this will just be if the player is pressing up on the keys so let's take a copy of the space we'll just change that to VK up and we'll set up to be true and we can use this for transitions into doorways for example so let's press play in test set out now you probably notice that you have a small pause when the game starts and also we'll get one here when we walk through so I can't actually move just for one second and it's much nicer than just having them run because the play is still pressing right arrow or still moving and controller for example it's just a nicer way to do it now you also notice we get this dust forming when the player moves to the new area and the reason that's happening is because for a split second we get put into the air state because our gravity is not enough at the start of the game to trigger us being on the ground though there's a really simple fix for that and it's just we could just go into the create dust and let's just only create dust if we have control if if has control is equal to true and that just gives time for the gravity to apply and the player to realize they're actually on the ground okay once again let's test that out now when the game starts you'll notice that we don't get that dust anymore so that's great that's just what we're looking for and when we transition as well we shouldn't also get that happening there either right we just get a nice smooth transition now it's kind of quick and you could improve that with things like a slow fade to black transition but this tutorial has been long enough so I'm not going to go into any of those details right now now you'll also notice that if we jump on the enemy's head this coins there the enemy is dead if we go to another room and then go back those things are not persistent and that's something that we might look at addressing in the future so the great thing about this kind of transition is you can apply it anywhere so in the middle here we have a door the plane moves through so they move through in the middle of a level but then moved over to the right-hand side so here we have another door entry and the player faces a different direction this is done with some particle effects which we might look at in a later one and the same with this light is just an asset with a few particle effects it might be something I'll show in a later tutorial if enough people are interested now with the door entry if you're interested in that because the player walks on the trigger transition it doesn't actually trigger unless they push up so I've done that by having a new variable called Auto transition and set it to true so that means that this will be an automatic transition if that is always set to true and I did that by just having an expression here that says if the other has control and auto transition or other up then we go ahead and we do the transition and by doing it that way if we look at this level for example this door or a double click it go to the creation code I just set the auto transition to false but that means when it's set to false you have to push up for the transition to work now with room transitions I go to more detail in my udemy course on making a tile based platformer in game maker so if that's something you're interested in there's a coupon code in the description which gives you up to 90% off my patreon to help make this channel happen I appreciate their support and you can see these superstars listed here if you find the content useful consider supporting as this enables me to create more cool tutorials for you to learn from patrons gain access to all the source code as well as any bonus tutorials and legendary patrons get free access to my existing and future courses on udemy udemy hosts my GameMaker courses which includes my latest platform of course it's in GML which is the next step up from drag and drop and it can really help if you want to advance your GameMaker skills there's a special coupon code in the description where you can use it to get over 90% off the retail price lastly don't forget to Like and subscribe as it helps me build the channel and create more cool content that's all for now I'll talk to you in the next one [Music]
Info
Channel: Slyddar
Views: 5,176
Rating: undefined out of 5
Keywords: gamemaker, platform, platformer, drag, drop, dnd, dragndrop, drag n drop, tutorial, peter morgan, one way, oneway, fall, make games, shaun spalding, heartbeast, spalding, yoyo, gms2, gms, advanced, learn, drag and drop, collision, make game, jump on head, enemy ai, horizontal, collisions, coins, collectible, items, objects, slyddar, peter, morgan, 1.4
Id: YwwTC9w5nY4
Channel Id: undefined
Length: 27min 51sec (1671 seconds)
Published: Sun Apr 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.