GameMaker Studio 2: Action RPG Tutorial (Episode 21: Lifting things)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to episode 21 in this episode we're going to begin and begin as the key word the process of making objects liftable okay or entities liftable rather so we're going to bring in a pot you know like a classic Zelda styled pot is an entity much like our grass nice lying person or NPCs that you know you can hit and that little white flash appears and all that jazz but also that you can pick up and this will be a cool property that will be able to basically assign to any entity that we want to be pick up a ball right the effect will not look perfect by the end of this episode as there's quite a lot of groundwork foundational stuff that we need to do in order to get this going because there's a few assistance that we're going to need in place that are going to be useful for a lot of different things that we're going to do the key thing being that we need a way to make a arbitrary animation play that is you know we need to be able to tell our game that we want the player to play some kind of arbitrary animation and then maybe do something when that animation is finished in this case like playing like the lift animation right and so like a cow flight leans down to pick something up and like has it above their head so rather than just writing a state for each individual thing that can do that there's going to be a few times what we might want the player to just perform a specific animation we're gonna write something a bit more modular little iolair to just do an arbitrary animation and then put ball on the thing so it will need to set that stuff up and there's a few new functions that we're going to need to create in order to get that going but it will be very useful for us later down the road before we get into that though I'm just going to do a handful of things now just to get this a bit ready for later on we're going to bring in our pot sprite the sprite for the player for lifting things up and and just add a couple variables here and there okay just the standard stuff we tend to do just to set things up here a reminder as well I think I've said this before and but don't worry if it's sometimes the order in which I do things feels odd like you wouldn't have thought to go to the creative and add variables the things before you even start writing them obviously i've script this in the hope of it being clear for you guys what you need to do like the steps you need to follow this obviously is not necessarily the natural way you would code this sometimes you would write something and then go back to add in your variables to the create event and so on later okay I just want to point out again to not worry about that kind of thing alright so let's get started first of all in sprites I'm gonna make a couple of new sprites the first one from my RPG assets this s pot that's just a little little Zelda style pot I drew we've got some fragments for this later as well that we can probably sell in the next episode when we come to like throwing the object we're just going to deal with lifting today so this is the pot which is just can recall despot and we're gonna put the origin in the bottom center I did that manually but you can just assign it there as well bottom center okay then the next sprite we want to bring in is s player lift which is in here somewhere as player lift I think I'll go on s player lift one that's ignore that alright if you're using my assets you just want s player lifts trip for bring that one in s player lifts and you can see it's just a single frame animation with the character just sort of Leaning down and we're just going to reduce the frame rate to from 15 to 6 just so that it hangs on whichever frame it wants to play just a little bit longer and then we just sort of have like a brief moment where the character is in this frame and then comes back to just being an idle stick it's just a 1 frame animation we're just kind of holding that frame for a little length of time ok so speed 6 and the origin for this according to my script is 11 31 yeah that looks about right about there okay so they go from like standing like this to sort of Leaning down like that alright okay just a couple more things to do here let's go to objects and go - and manages our Oh game object in the create event I'm gonna add a new global which is global dot I lifted I standing for instance in this case and we're gonna have it equal no one and this global is just going to contain the instance ID of whatever entity we happen to be carrying okay of how player is lifting something above their heads in a Zelda style way okay I'm handy to have is global just because then anything and the rest of the game can access it and sometimes that's necessary right we need to maybe an NPC needs to know what you're holding and so on right then let's pop over to the player object on the create event for player and we're gonna add in here just another variable somewhere in the big list animation and script to equal negative one and we're not gonna use this in this particular episode but it's going to be a useful thing for is when we execute an arbitrary animation for the player to be able to say when this animation finishes carrying out a certain script actually not really necessary for the lifting stuff but when later on we're planning to add a bow into the game that you can shoot and that's essentially going to use this system where we just play an arbitrary animation which is the player shooting a bow in whatever direction and at the end of the bow shooting animation we're going to fire an arrow right and that's where that will come in and be useful there so it could be useful for all kinds of different things you want to do okay so that's clear up a little here that's close down that room editor make a new workspace just so we've got plenty space so I'm gonna make a new state for the player okay cuz what a fairly familiar now with Howell player States work we're just going to make a new function in player States called player state ax okay actors in view to to perform an act to carry out an animation and this is just going to be our you know perform an arbitrary animation state where we don't want to be able to move and roll an attack and all those different things we just want to stand still and perform an animation of some kind okay and so the first thing in here is to just update the sprite going to whatever animation we're currently playing I'm a su that when we came into this state we set our animation accordingly it could be anything okay so I'm just going to call a player anime sprite our function for controlling our animation and then you will remember that this function that we go to it works out if we've just ended the animation okay and this is yet another time for this particular variable to shine this if we know we've ended the animation we can call the animation end script if we have one and also send us back to the free state which is presumably where we came from right so if animation and state equals player state free just send straight back there and animation end equals false just you know so just to get that a set right away if animation end script does not equal minus 1 then I'm going to execute whatever is in that script so script executes animation end script if by chance you are worried having seen the stuff about 2.3 I don't know if there's anyone in the audience for these videos who is isn't necessarily thinking about this but if there is let me know if this was useful um you're worried about script execute because you know scripts are becoming separate from functions later and you're thinking well is this this whole method of doing script X is still going to work in 2.3 onwards yes it will okay script executes will change to be kind of weirdly named in a way but you can still use it in 2.3 onwards I've tried this in the beta I got confirmation from yo-yo that this remains a thing and you can use it to call functions that you've put in a variable okay just in this exact way like we do with state equals the name of a function you can put names of functions in two variables just like that and execute them with script execute even though it's called script execute right works just the same so don't worry too much about that all right and I'm assuming here we don't have any arguments that were going to want to pass to the animation in script that might be something you want to build in for extra flexibility if you want / it's more work than we need to do for our game so animation in script that's fine once we've executed that animation in script we want to set the animation in script of the player back to - one important thing you do this because otherwise maybe the next time you execute a player state act that actually doesn't have an animation end script it might still find that you have one that was set from the last time you called it and it might call it so make sure you reset it when you're done animation in script equals negative one and that's the lot there so that's our act state for the player okay state they can be in where they're just playing their animation and when it finishes they go back to the free state it will play an animation exactly once and if they happen to have had an animation n script set it will execute that script okay now this isn't all we need we also need a function to actually carry this out send us to this state with the appropriate sprite and with the appropriate animation n script and so on okay so I'm going to make a new function we'll just make it in player general called player act out animation as you could also just call it player out right for sure over just I like to be verbose if it could make me extra clear I prefer to write the longer thing if I can so we'll write the description for this one because it's going to be a function we actually like call through the game with arguments so desk player act out animation sprites comma and script there are two arguments our sprite or soup right apparently a sprite and odd and script okay and for a vague description as well just so we remember what this is actually for us we've got a few started get a few similarly named scripts about the place good thing there start commenting these are in a bit more detail carry out an animation and optionally carry out a script when the animation ends so state is going to equal player state sprite index is going to equal argument yeah argument 0 that's all yeah ah that's right that we pass through then what we want to do is check to see if our argument count is more than 1 and we passed an end script through as well and if we have set it so if argument count greater than one animation and script equals argument 1 ok we can reasonably assume that the player is the one calling this script as well which is all I'm just setting the variables okay so then local frame needs to equal 0 remember it's important to do that when we change to a new animation for the first time image index is also going to equal 0 and then and then that's almost everything but the last thing we want to do is call player animate sprite just once at the end here other way if you don't do this you might find you just have a little flicker at the start where it like because it faces to the the right bite of hope this was our image indexed to 0 and we just want to calculate that direction right away so we can do that just by calling this once at the start here just a quick way to make sure we are facing the right direction immediately so we don't have much of a way to test it right now but we actually have everything we need there to perform an arbitrary animation okay I wish I'd have built something like I'm spinning around animation or something we could just sort of test that with can I think of anything real quick actually ok here's one way we could maybe try it build quick let's open o player if I just super quickly I just don't copy in the code this is just demonstrate something just quickly add the key pry selective and if I do player I act how animation and the sprite would be s player attack slash for example and we have an end script that we can call it would do anything interesting drop items hex Y and then the item could be o fragment what did we call our other fragment no Frank plant that's the Oh Frank plant just as a quick test of this so what they should do is make us just play the slash animation and drop some fragments when I press the left arrow key right what it does Luzi and it takes our Direction into account as well which I mean it looks like I'm just doing the attack but you can tell up not because we're dropping leaves everywhere right so there we have it we have everything we need to just play an animation once and also if we want to execute a script happy and hold that animation okay so I'm gonna delete that as I said I didn't really want you to write that in so there just delete that event if for some reason you did put it in now that we have that foundation in place we can go ahead and actually make our entities liftable okay so I'm gonna make uh yet another new function okay we're going a new function called debates liftable okay and it's going to be a script that we tie to an entity as its activate script and if it's something we want to be able to pick up and throw or whatever right it's going to have one argument and its argument will be the ID so we just pass along the ID of the instance that we are lifting up if globe be good and the reason we do that sorry is because these scripts tend to be carried out by the player rather than the entity itself so we need to pass along the ID of the entity as an argument lowball dot I lifted global for which object we have lifted equals no one so assuming that that's nobody we're able to carry anything we can lift this up okay so player act out animation s player lift with no no script to execute at the end of it alright I couldn't do it all here global dot I lifted equals argument zero setting that ID to be the thing that's that's lifted up and then with that so with global dot I lifted lifted equal true and persistent equals true okay the reason we make it persistent is so that you know we were able to carry an object for from room to room all right pretty pretty useful then we just turn that back off when you drop it or whatever okay which we'll come to in the next part and that's that whole script so let's pop over first of all to P entities create event this is P entity our hair and for all entities in the game and add another thing in the create event here just lifted equals 0 is we wanted to track whether or not they're being lifted and that's not something we want to set at any point that's why it's in here and knowing variable definitions then we're gonna go to the end step and suddenly Sean remembers why this is the end step yes this is specifically the end step because in this part we're going to have the entity follow the player around if they are being carried and since we're doing that we want to have a very up-to-date coordinates for the player so that it doesn't like lag one frame behind if it executes before the player does you know that kind of thing so that's why this is the end step if you didn't make it the end step just it's very easy enough to just right click and hit change event and just swap it to that okay so in here what we're going to do is inside this if not global game pause bit all the stuff we do assuming the game isn't paused and after we set the depth there we're going to write if lifted and instance exists oh we're all right and if that's true then we're going to check to see if a player dot sprite index does not equal s player lifts because if it does it means we're still in the process of lifting it up and we don't want it to just teleport to the top of our head while we're still lifting it and this is why I'm saying it actually makes a bit more sense if you just use that end script thing we set up to make it so execute after the animation is finished rather than at the start of the animation and would have actually made more sense but hey this is just how I coded it when at the time all right we don't always think of every improvement while we're writing code okay so if you want to do that probably a little optimization you can make that and then you can skip writing this whole if statement because it won't matter but for now we need to put this in so assuming we are out of the lift animation at the end step with the entity we want to set X to equal a player dot X Y to equal a player dot Y and Zed to equal 13 okay you might remember Zed is like our fake height variable so we move this to wherever the player is and then we lift it up off the you know ground in quotation marks to a specific height you might want to make this a macro I'm happy with it just as a magic number I know to come here if I want to edit it only in one place up to you if you want to do that and then the last thing that you hear is right depth equals Oh player dot depth minus one alright so we it shows where the same depth as where of the player is because it's being carried and I like to thinking there's one complete unit in terms of their depth in the scene but we also kind of want to in front of the player so it sort of shows up on top of that like hands or whatever right in the next part we're gonna bring in a sprite so that when you are carrying something you're like you're the character has his arms in the air and it's obviously holding it for now it's just gonna flow above their head yes that's why we're gonna do that anyway and that's everything there and then we just need to actually bring our pot into the game and set it up to be area ball all right so I'm gonna make a new object called Oh hot and set it sprite to be s pot and set its parent to be P entity which means it inherits all of that lovely code and this is why I love the empty system so much when we go to variable definitions and we can just set this up so entity activate script is going to be and they lift the ball and our entity activate arguments are going to still be an array so make sure you still have those square brackets there but it's going to be ID and then I guess at the bottom here with entity hit script we can set that to what was it for like a signpost again entity hits solid right so at the bottom here will just write entity hit solid then let's go ahead and put that into the game and see how it works so if I just come to our village and put a couple of pots down we should be able to sort of pick those up the pyro over here press space we pick it up and it just sort of floats above our head and is now just carried around by the player and we can leave the room come into here and we're still carrying it so there you go there's picking up objects moving them from room to room and has to say this it doesn't look like great right now because we just will roll while we've got it it just sort of floats above our head we can't throw it we can't put it down it's just sort of there forever but that's half of the journey all right this episode's long enough and in the next part we'll start sort of finishing this off and being able to throw it changing our animation while we're carrying it and so on ok thanks for watching guys hope you enjoyed this part and I'll catch you all next time 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 as a noun a Robert church's row van Dahlen safiya flame daca donned ago max n Bertie T relentless Rex duat doobie Jason Dark Riders zero three one eight Jane's Sagan's James L Anderson hair eunjin roopinder Ren Dan Scott Matthews leo Tyler Hubble maria celeste oliveira frailing your unpaid ur cabbage pants Gilberto Cicero's piggy mark Burgas john harwood sack : goose cow Franklin Troy Mara Alex Schenkel Wilfredo land era Carter green just Anna daga 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: 9,620
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: foi6Bb9IYy4
Channel Id: undefined
Length: 22min 19sec (1339 seconds)
Published: Fri Jun 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.