GameMaker Studio 2: Action RPG Tutorial (Part 19: Room Transitions)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to episode 19 today we're gonna be starting to cover this moving from one room to the other as you can see we're starting it off really quite simple we've got this zone that you can see behind behind the game I've placed in our village our first room and when we step into it it takes the player to another room that I've created and if we step back it brings us back to that room notably informing the game where the player should spawn when we move from one room to the next because if I restart the game you see this is where I spawn this is where the player is placed in the game room but the way we're going to set this up is when we collide with the zone it will tell us not only what room we need to go to but where exactly the play needs to be stood and what direction they need to be facing when they get there one little bug fix from previous episodes before we get started is you might notice just watching this footage you might assume in a bit but these are fragments as the camera moves you'll noticed a sometimes seem a bit out of sync with everything else they seem to like G around a little bit and that's because you might recall all of our entities and a player etc draw at a rounded position to prevent any kind of weird subpixels stuff okay and we forgot to do that with our fragments okay so if you want to fix that this wasn't evident before because we had a room that was too small to really see the camera moving but it's a lot clearer and now so if you want to go ahead and fix that you can just come into a fragments draw event and just wrap the X&Y part of this in a floor statement okay simple as that and that will synchronize the the movements of these fragments with the camera and everything else on screen okay so off-camera I've gone ahead and made a new room for the game this is called our River very similar inside of a bank exactly the same setup as on our village using the exact same layer structure just another child of our parent built in the same way it's just a bit bigger okay its 320 by six for either strep resenting it kind of just another leg in our characters journey so be in the village will get a quest from this guy to go and get rescuers hat from a cave will journey up this river maybe fight some slimes along the way we're gonna have a boulder in front of the the cave entrance they have to blow up and get inside get inside the cave got the guys hat back you win okay that's the that's the general flow of our game and that's what this room is now I've got off camera because you know I didn't really need to see me just setting up all the tiles and stuff again you can design whatever levels you want I mean this isn't really perfect haven't really even account for the fact you can just kind of walk off the side of the room here I'm not here to do your level design for you I'm just here to show you how to make stuff and so I've made this room look the whole point is you're gonna want a second room for this tutorial of some kind make sure your player is in it somewhere I've just put them down here but it doesn't actually matter where you place them exactly in the game world so the first thing we want to do for this is make some global variables okay so I'm going to come into Oh game our persistent object that exists from the very start of the game right through the end of the game which is generally our go-to place in this particular project for making new global variables we've got and I'll create event initialize and Global's right this is where we make them the reason we want some global variables for this is we want to create some variables that are going to persist from room to room that can tell us where the player is supposed to spawn right when we come from one room to another based on you know whatever triggered that transition where should we put the player and what direction should they face okay so we're going to need for new global variables and then arrive global dot target room which can just equal minus one for now global don't target X also - one global target Y and global dart target direction which could just equal zero okay with those Global's in place we're gonna make a new object done so that's getting a bit claustrophobic let's make new workspace close all but this just give us up some space and create a new object and I'm gonna call this object Oh room exit this object is going to function as a trigger area that we can place in our game though when the play collides with it it will send us from one room to another and inform our global variables our targets target Y and so on where the player should spawn when we enter that room okay in order for that to work we need to make some variable definitions so that we can alter those properties the target x target 1 and so on on a per instance basis when we place instances of this object okay so in variable definitions I'm gonna add a target X target Y and target room those are the only ones we need you might remember we also have target direction but we're just gonna base that on whatever direction the player happens to be facing when we walk into this trigger okay you could add that as one on here if you want or two as well and be like if a target direction is set you know if it's not undefined or whatever and then use that instead I will I'll leave that decision up to you there's a way to sort of enhance the versatility and usefulness of these triggers if you want to that can be your own homework but we're just going to stick with modifying these three using the direction of the player okay now in order for this object to function as a trigger area it needs to have an area to be collided with right and for that we kind of need to give it a sprite that it can use as a collision mask so I'm going to go to sprites we're going to make a new sprite and just call it s trigger I'm going to quickly add it together and image that's just going to be in like a blue square with some transparency scram of Blues at the AL for a bit lower just so that we can see through it when we place it in the world and that will do top lap bearish in absolutely fine for this and let's come back to our Oh room exit set the sprite to be s trigger okay now we're gonna add the step event what would also work for this is the you know the collision event like a collision with the player I prefer in these kind of situations to use the step event it's really kind of the same thing because we're just going to check to see if we're colliding with the player in the step event and by using the step event we just maintain just that a little bit more flexibility and control if we want to kind of change how things will work rather than just an event that you know triggers once when when the player touches the instance so in this step event we're going to check to see if the player is collided with this instance and if it has we're going to cause a room transition alright the first thing we want to do is check to see if the player even exists we're gonna do if instance exists a player always a healthy thing to do before we start you know referring to an instances variables and so on is check to see if an instance of that thing even exists just a handy little safety check assuming it does let's get that other bracket in there let's not forget that assuming it does then we want to check if also position meeting Oh player dot X Oh player dot y IE d close bracket close bracket so I don't know if I've already been through how if shortcutting works which is on by default gaming studio - if if I write an if statement that has two conditions in it with an and like this it checks the conditions in order right and send the way the logic of an and works is it requires both conditions to be true we know if the first condition is false it doesn't matter what the second condition is right because we needed them both to be true and so what gaming it does what many languages do is if if this does turn out to be false it won't bother even checking this ok which is what allows this to function as a handy check to make sure that the player is there before we refer to variables that might otherwise not exist okay so we won't do this check unless there is definitely a player all right so that's part of why we do it like that so if there is a player and the position of you know it's the players X and the players Y is in a position that collides with this particular instance ok ID being the ID of our whatever room exit is running this code so if that's true then we know we need to change rooms right so then all we have to do is replace what's Ivar currently in our Global's with whatever is in this particular instance so global dart target room is going to equal target room the local variable global target X is going to equal our local target X and the same for target Y and then last of all global dot target Direction is going to equal a player dot direction remember we've already confirmed that a player does exist ok now later this will be where we start calling the code to or calling a function that will run a fancy room transition we're going to do like a little slide transition just just to show you how it works but for now we're keeping things simple and we're just going to snap straight from one room to the next a song a new room go to target just to be safe to make sure we don't call all this a bunch of times which is important when it comes to when we're doing our transitions and stuff like that because you know this will be running every frame that we're actually sitting on the player we're gonna do instance destroy doesn't really matter for the time being because when we do even go to like this and still get destroyed anyway but as I said eventually this is going to be replaced with you know a function like do I do a room transition right which will take more than one frame to complete so we want to destroy the instance at the end of this to make sure that this room transition stuff isn't triggering every frame that the player is colliding with it all right so that already actually sets us up to move from one room to the next so let's go ahead and just put these into the room where we want them so in our village I'm just going to put one let's put a grid on and GUI instances layer yeah and just plunk this at the edge here just resize it to be whatever we want just sort off the edge of the screen there we'll bring it on a bit as well as you so it's really nice and clear move this signpost out of the way you might want to prefer so it's like you have to actually walk off the screen young I don't leave all that sort of stuff to you but just for the clarity in the tutorial we're just gonna bring it right here and double-click on it let's go to variables and we can set up a target X target Y entire get room just click the little pencils next to use to be able to edit these values and the target room is going to be our River now for finding the exact X&Y that you want to use you want to go to that room and the easiest way is to just place your player just because it doesn't know where your player is in any one of these dreams because they're always gonna come into here in via like a room exit right I mean just can move them around and just use them as a dummy to place them exactly where you you know you think the player should actually appear when they they move from one room to the next just then double-click them and just see what the X&Y is in that and then you can put it back wherever it was you know you can just move them around like that so twenty four five to eight appears to be a good place for this one so let's come back and put twenty four five to eight there okay so now we're gonna do the same thing in art River so in the river map that's now we can just mutant leave this player out the way and put a room exit instance onto the layer drag it out and wherever we want it to be double click it bring up the variables ok I'm close this down so it's not in our way so the target room as we know will be our village and then we can use the same method we used for the other room to find out our target X and target Y okay so I'm going to pop back over to our village and on the instances layer drag in a player and just stick him anywhere like I didn't want to move this one cuz that's where we actually want to like spawn this off for a stream of the game so I'm just gonna drag in another one temporarily just to find an x and y position I'm not putting him importantly on the trigger error itself because you'll if you end up you know if you teleport onto the trigger area you'll just you'll get stuck in an infinite loop doing that right which we will show you how to avoid when we come to doing the fancier transitions for now we're just keeping things simple so just just don't use a coordinated such as the each trigger itself right now okay so for now all we have x - 9 6 + y 1 1 2 okay so if I pop back over to our river edit these tog X is 2 9 6 and target Y is 1 1 2 all right and don't forget of course once you've done that to get rid of this temporary player we don't want multiple player objects in the game at once alright if I run the game now I just want to show you I mean obviously we haven't quite finished setting this up and we need to make this invisible as well we just have this big Billy trigger area but the room go to part will work alright so the most basic element of this as I walk in air and it's brought through into the next room there's a couple of problems right our camera starts way up like near the top of the room and we're not actually being positioned about this coordinates to Lisa okay and if I come back in it's here I'm here where I would originally start and that's the that's the issue we're trying to solve right that's why just a simple room go to doesn't really work here and we need these global variables now since we've set up those global variables fibery makes it we know that when we come into a room where we want the player to stand and what direction we want another face we just have to actually apply it so I'm going to go into a player and go into the create event just the you know all the stuff that happens when a player object is actually created and at the bottom here what I'm gonna do is if those global variables have been set to something we will set our X&Y appropriately so I'm going to do if global dot target X does not equal minus 1 because that's our default setting and it's also a setting we will never actually want to use for target X right so we know if it is minus 1 we have we just haven't set anything to it or there's no reason we would set minus 1 it's not okay and I think it's reasonable as well to therefore assume that if target X is minus 1 then we probably haven't also set target Y right it's I can't think of a reason why we would only ever set one of them so as long as target X is minus 1 then we can assume target Y is minus 1 as well and then we can just leave the player wherever they are naturally spawning right because we can assume this is just the first room and we haven't come here from any kind of transition and this helps us as well if we ever want to do that in future after a cutscene or something like that we can just set global target X to be minus 1 or target Y to me minus 1 reset those values and then the player will just spawn wherever the instance happens to be whoever it was placed in the room editor right but if it's not minus 1 then we know that we have a position that we actually want to be in okay so I'm going to set X to equal global target X y equals global target y and last of all Direction equals toggle dot target Direction let's get rid of that random line of whitespace there and the other thing was the camera right so if I come into Oh camera what I'm gonna do in here since the camera is persistent its create event isn't gonna run every single time we move from room to room okay it gets made once and start the game and we don't actually run this create event like multiple times okay so we can't use the Korea event and we actually kind of don't want to anyway because then we we want the player to have moved first so that the camera can catch up to wherever the player happens to be I mean I suppose in theory we could just use those same global variables but it's quite handy just being on follow the play and so what I'm gonna do in our camera is we're gonna add an event I don't know if we've used in this yet which is in the other section an event called room starts actually I think we have I think we've used it in an O game for let yeah layer management stuff yeah so room start just as a reminder runs at the start of the style room notably after all the instances in the room have been created and therefore have triggered their create events okay so the players create event has gone off we've set ourselves to our appropriate position so now we can just position the camera wherever the player happens to actually be assuming there is one so my description here is going to be to update two players new position and we're gonna do if instance exists a player X equal Oh player X y equals Oh player dot Y you mean you might want to replace this with the follow variable if that's what it was called yeah the the follow variable since near the camera is kind of dynamic and controlling multiple different things which might give you some more flexibility with things like cut seams and so on like this is just a quick solution because we're generally assuming that the camera is always going to want to follow the player whenever we move from room to room but I don't know maybe saying this to follow our necks and checking if that instance exists and so on is is worth doing as well but bear in mind that if if follow contained a player's ID like the specific our instance idea of O player from a particular room than that instance ID will not necessarily be the same when you move to another room okay because it'll be a new instance of O player that you're working with so for now for simplicity we're just using a player that okay so with that done I can now run this and we can see the improvements that we've made to this run-through here you can see we spawn in the correct place facing the correct direction and the camera is already in the right place last thing here let's just go to room exit and Mark this not visible okay that's kind of important and there we go that's that's that's more the sort of desired effect all right okay and and just a show it's keeping the direction as well like if I kind of weakly come in from above here you can see a thing I maintain the down direction they're only possible because there's my kind of rubbish not very well for three level design here but just just a demonstrate that it doesn't work in that way so that is moving from room to room as I said we're going to put in a fancier transition here but this honestly has kind of a charm to it in and of itself when I first made this code I wrote the transition in straightaway and like it was a little sliding transition that sort of you know slides to blank and then slides but fades back in just to show you the new room but honestly this sort of snapping thing has kind of a charm it feels instant Dainius feels like that you know that this screen really is physically right next to the next screen so I don't know maybe you might like this kind of transition and just kind of leave it like that I'll leave that decision up to you but next episode we will be covering how to make this a bit fancier okay one other thing - of course note is there's gonna be a lack of persistence when you move between these rooms if I cut these once and I go into here and I come back the plans aren't cut anymore alright that's pretty standard as games go right you know having things like that respawn in between room transition keeping everything utterly persistent between rooms is gonna make a headache for you and a lot of work so I think it's generally better to pick and choose your battles when it comes to that kind of thing and decide exactly which things are important for you to keep persistent and you know in which lair episode we'll deal with the systems and things like quests and NPC dialogue and things like that and carrying objects between rooms and that sort of thing but it's something I think personally is best handled on a case-by-case basis and I would resist the urge to go I don't even know where the setting is this how infrequently I use it yes down here so I would avoid the temptation if I were you to go in and take this persistent box on our Vic now would I mean that works on the moment like if I just take our village and I come in here do that boo-boo God don't know what's happened that way okay that's done all sorts of weird stuff and this is why I would avoid it I'm not entirely sure what's got out there it's moot I think it's moved that object somewhere else and I don't even know what happened but I mean that's a perfectly good demonstration like it's kind of a blank box what's meant by a blank box is that you are you can only really ever understand what it does at a very surface high level and how exactly it functions under the hood is hidden from you and especially in terms of happens when and in what order and all you can really know about it is whatever the documentation tells you about it which is obviously going to be somewhat limited okay so it would really avoid this option my honestly thought it would work there but I've not tested it I've not tried it it's something I tend to avoid ruin persistence in theory in theory I don't know what exactly went wrong but in theory you can do that and it will like keep things between rooms but as you can see as I've accidentally kind of demonstrated it can lead to a lot of problems so I would avoid the temptation to try and use this and I would wait until basically just you know deal with persistence on a case-by-case basis on the things you want to remain persistent is possible to get all these plants and stuff like staying cut if you really wanted them to but as I said take your bowels because the more things like you'd want to apply that tears are more complicated it's gonna get all right okay I'll catch you guys next time thanks for watching see ya thank you of course and as always to my amazing patreon supporters without whom I couldn't be doing any of this work a huge show in particular and in no particular order to the following cool kids boughs of the dog zen own a robert churches row van Dahlen Safiya flame daca dawned ago max m Bertie T relentless Rex duat doobie Jason dark rider zero three one eight james Sagan's James L Anderson hair eunjin roopinder Reni Dan Scott Matthews leo Tyler Hubble maria celeste de oliveira frailing your unpaid ur cabbage pants Gilberto Cisneros Figgy Marc Burgas John Harwood sack : goose Cal Franklin Troy Mara Alex Schenkel Wilfried Olin Dara Carter green Justin a dega Julian Paul and Kaiser ho thank you all ever so much and thank you of course for watching catch you all next time
Info
Channel: Shaun Spalding
Views: 17,904
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2
Id: hJTC9xSSYFg
Channel Id: undefined
Length: 26min 25sec (1585 seconds)
Published: Fri May 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.