Love2D Tutorial Ep13: A SceneManager

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's make a bit of an optimization to our sprite class if you will notice here in main we have these animations walk swim punch and idle and I don't really like having to declare them like this basically I would like a way to add pretty much as many animations as I want to add so what I want to do is have something that looks like the following so we'll still call it add animation we'll add an S to it basically what it will do is it will take a table we'll make it take a table and the table will contain the key that we want say for walk wants to do idle so we'll just say idle equals idle and love 2d is smart enough to know that this is going to be our identity which is our which will be a string value or basically it's the key and then this will actually refer to our animation right up here so that way we can do the following we can say walk equals walk swim equals swim and punch equals punch so that's what I want I want to be able to do that and you see how that removes basically all of these lines right here so I'm gonna go ahead and save that and if I were to run it of course it would give me an error because we haven't made that modification yet so in order to do that let's go over to the sprite class and let's say where's our add animation let's make that add animations and let's take a table called animations so we'll rename this two animations and we don't have to but I'm gonna go ahead and search the type of animations you know I'm kind of doing this I don't know in different places and I guess in reality you probably want to do it in more places but I'm doing it in some I'm doing these asserts and just some what I think are key places where one could get messed up or stuck so you know take it for what you want put them in or not it's just kind of a help to for someone to understand how how they need to use the library like for me if I come back to it and I've forgotten let's the animations parameter must be a table simple enough so what we want to do is we want to say for key come a value in pairs of animations and we want to do [Music] want to do something like this but it's going to be self-taught animations of key equals that value and now if we run it we should be great so we can come up here we can change him to swim and he swims we can make a punch afterwards so that's something that's been bugging me I just wanted to I just wanted to fix that all right by now you may be saying let's make a game of some sort okay why don't we try to do that one thing though that we need is a lot of times games will have certain you'd like to call them states like there would be a menu state there might be the actual gameplay state there might be a game over state all sorts of different states let's create some sort of manager to manage these and let's call them scenes because I like that name yeah sounds like a great name let's come in here to lib and let's say it's called a scene manager and we will pop into one of these others and grab the class construct here to include that module and we'll say local I'll call it SM for short scene manager it equals class and we're going to do our standard derive animals a scene manager and will return it and that scene manager the sing manager is gonna say less we need to function call of course the new function on our good friend the new function and our what what should we do here should we have let's do this let's say that we [Music] are going to tell the scene manager what folder our scenes are in let's create a let's also create let's go ahead while we're at it and create a scene folder and that actually needs to come out here and I want it to be called scenes instead of seeing and let me let's just block out what what we want in here let's create a new file let's say just call it let's call it start Dahlia okay now what do we want in a scene we want a function and let's call it gosh what should we call it let's call it let's call it just load and so this function it's called when the scene is loaded let's let's think about this and this will be this is gonna be a local function so we'll have some sort of I'm thinking we'll have some sort of module so we'll just say local I'm just going to call it s and we'll set that equal to an empty table then we'll return s so basically we're kind of making a template here and really let's do that that's that's a good point let's actually make it a class so that way we can just derive from it so we're gonna make a class and we're gonna call that class we're gonna call it a scene because that's what it's gonna be it's gonna be a sling and we'll derive no it's sorry its class derive and we're gonna do rive it's gonna be a called a scene and what we're gonna do is we're gonna say new so yeah I'm not sure if we'll use the new function or not let's keep on going here just just just for fun and actually I want to move this I want to rename it because this is gonna be called scene and we're gonna put this in the Lib directory okay all right so we're gonna grab this because we're gonna create a couple of other functions we're gonna save scene update so we want it to we want every scene to have an update function we want every scene to have a draw function in addition when we exit a scene from our scene manager we want to say exit okay and we'll have a function called enter and I think let's just let's go with that for now how about that so what should the same manager do then so when we when we new up a scene manager the scene manager should probably take a table of scenes that we know are gonna be in there and let's say we should be able to add a scene a specific scene if we want to - the scene manager and then in the converse we should be able to remove a scene from the scene manager of course so we want to new up the thing and we want to let's call it we want to be able to switch scenes with the same manager so next thing we just call it next scene and with that in mind our scenes need to have be aware of the scene manager so when you new up a scene we need a we need to know where who is our scene manager so let's do this so every scene will have in its itself it will have a scene manager all right yeah I like that so that way we can we can each scene can control that and switch to other scenes as it as it needs to of course this also requires the scenes to be aware of the scenes you know of the other scenes but that's fine I don't I don't see any problem with that let's also do a function let's say you list scenes and that'll give us all the scenes that the same manager know about knows about so let's let's just label these gives a list of all the scenes scene names let's say scene names the scene manager knows about that's Marty nice of a singing manage you to do that I okay so this switches scenes switches let's say switches from the current scene to the next scene and what we'll have in here is will say we'll have for one thing will have self dot current scene and for now that'll be nil and what we'll do when we switch to the to these scenes we'll say if self dot current scene is not equal to nil then we're gonna call a function on that scene and we're gonna call we're gonna call the exit function on that scene actually so let's do self dot current scene dot exit or let's do colon colon that gives it it that gives it its self let's also just for we this might be an interesting thing to be able to go and ask for the previous scene you and I would say that these are actually going to be let me show you so the scenes when we come in here and we create a sing manager that's going to be we'll say this if scenes first of all you can create a scene manager without specifying any scenes at first if you so desire why do we allow that flexibility because we're just nice people that's why really nice ok but if scenes is not equal to nil in other words if scenes are actually specified then we're gonna do something otherwise so well yeah otherwise self dot scenes actually having to do otherwise let's let's go ahead and put that up here so there's no else but if the scenes variable is not nil then let's also assert that the type of scenes is equal to table if it's not then we'll say per parameter scenes must be a table come on people don't you know and if it is then we'll say four and it's really just gonna be a list it what we're gonna do is it's going to be a list you I think it's gonna be a list of strings let's just wear let's see I equals one to the size of scenes all right so what we want to do and that's another thing the scene that's also when we knew up a scene [Music] what we want is we want the scene to have well I guess we I'm thinking about names and maybe the scene name will actually be the file the name of the file because what we're gonna do here is we'll say local M equals require scenes of I kind of like that and that what would be nice be able to specify the directory that the scenes are located in so let's add that what's that scene directory just like that I'm going to do a run real quick shouldn't be any differences here let's go to main and I want to just include this real quick so let's say local scene manager is require Lib scene manager the code okay load the code and let's new up a scene manager so just we're going to test some things equal scene manager and then we'll give it the directory scenes let's see what happens alright now let's make let's put a scene sort of a of a skeleton scene in here and we'll it's call it let's call it main menu and what we'll do is we'll come and grab this main menu and we also know we don't actually don't need that we need we need seen and that's gonna be require lib not seen and what we're gonna do in here is we're gonna make our own little little us we're gonna make our own scene and we'll call this main menu local main menu equals scene derive main menu I could spell it right main menu there we go okay let's return the main menu so let's go back in here and let's say remember we said this is needs to be a table so we want you to add to your scene management the main menu and what do we have module main menu not found of course because we need to include this same directory in here so let's do this if not basically if the scene director is nil then scene directory let's just set it equal to an empty string that way we can concatenate it here we'll require Sene directory and concatenated with our with our scenes but we'll do a dot like that and we're back to main menu dots or three local scenes he manager attempted takes a local scene oh we're in mainmenu sorry all right oh yes of course seen know that it is seen we're returning and we're not returning seen that's the problem sorry about that load it up okay we're still swimming that's good that's great it's wonderful now what we should do is now the question becomes should we allow the scene manager itself to new up one of these scenes well I think we have enough control there when we do a new perhaps we don't construct anything so we just basically leave this default new for each scene and then on our enter code that is when we actually construct and our items within that scene and whatnot so the enter would only be called one time when that scene was first loaded so our module here and then what we would do is we would say you we could add the self dot scenes and we'll add that key which would be there's our string representation so we'll add that key to the list and that's going to equal two em it's just gonna equal to em and then we're gonna give it this because remember this is the same manager let's see if that's okay that's looking good now the scene manager is also gonna need and we're just gonna grab them from here and rename that scene manager is also gonna need an update and an enter I mean not enter an update and a draw because what's going to happen is we're gonna we're basically going to do those on the current scene so we'll say if self dot current scene then self dot current scene dot update our : update et and we really don't need this nil here do we we don't know we don't and then let's do the same here for the draw so that'll be current scene girl just like that and let's go ahead and hook those in and main so same manager scene manager update and we'll need to actually do a a local for that variable which we have him down here so we're gonna remove that and hey Singh manager doctor all and we'll make we will well assuming this actually works we didn't put DT in here let's say this main menu lets override the draw function just to see if we actually are working here so let's do love dot graphics dot print hello there remain menu hello my menu how are you yeah it's very good to see you and let's let's put it at 200 250 say and then we don't I don't think we need to do that so let's load that and we're not seeing that as of yet because we haven't switched to the scene yet and we haven't actually implemented this this scene switching so what we need to do is do that so we could do some some checks here you say if they can't see not exit and we could say we could also say if next scene so basically if it's not nil we could do some more checks here I'm wondering if we should believe it we'll leave it for now we may add them later so if next seem then self dot current self dot scenes of next scene okay so theoretically now there was some checking that we must do there of course what we're not there yet so here's our scene manager so then we could say scene manager please do something for me and switch to main menu scene thank you very much I appreciate that and we're not sure if it's working so what we should do is we should do a little print here all right and I do have debug mode enabled it's coming off the screen here so we're not seeing this and so we need to come in here and check as to why we might not be seeing it you let's make sure that we're we're actually getting in here and we are see main menu okay and are we actually getting here and that was the other thing just the same manager was required oh no no should be alright um let's let's print here let's make sure we're getting that main menu there looks is there we are yes so that scene is going into our self dot scenes let's come down here and have you and a let's just change us lighter a little bit so we can slant somebody so we'll know which is which we're still not getting anything so let's go back here to Maine so seen manager let's make sure we're doing this right see manage your draw scene manager update yeah let's see if it's even hittites kind of looking like it's not even getting called isn't it well will will will get to the bottom of that nope it's getting called so that's good so let us print well let's print here let's print to switch yep think we're getting switch which is it's getting switched yeah so self dot current oh that's the problem I'm ridiculous self dot current scene yeah we are now it should say hello there from main menu fantastic actually from man menu hello from a wall from main menu amen okay so now we we have a sin manager we can as we we haven't switch it between sins yet but we can do it more functionality here is necessary but will let's continue to add to this ad saying well if seen then basically if saying is not nil then let's also check out let's a search that scene type let's set the type of scene is equal to a table because by golly we enjoy tables don't wear and if it's not because that's what it's going to be if it is indeed a an actual scene because here let's see should we do we want to be able to add just basically yes let's yes let's say this adds a what would we call it is or should we just add scenes like this I want to add self singing directory equals same directory up here so just so that we can save this because if we're going to add yeah let's require that to be let's require that to be down here and then we can say we can assert that m dot or m dot now M : is and and in here's where we're gonna need to include the same library we're going to make sure that that class actually derives from Singh so we're gonna say we want to make sure that that scene file is actually actually derived from that because it if it's not we're gonna search we're gonna say file and then we'll say this and we'll also go ahead and drop a blue on there is not type same and you know again this is the the necessity of this is arguable you can comment it out if you want to let me just run it again to make sure I didn't break didn't broke anything and it doesn't look like I did what else we want to do is let's go ahead and keep track of our previous scene as we said that we would do but but we don't have buggers there you know buggers current scene kind of scene equals so current scene let's not do it like this let's say current scene equals next scene and then previous how do you previous scene a self dot previous thing we're going to make that a string instead of [Music] yeah we're gonna make that a string instead of the actual scene so we'll just come in here and say self scenes that we're so we'll just key it this way like like this yeah that way we can actually tell what the current previous scenes are without and I don't think that's gonna slow things down too too much you know what come to think of it let's just let's go back and let's have you I like the let's let's say this these are strings that strings the strings at our keys into the self dot scene table and then what we'll do is we'll say self dot current say this had this contains this contains the actual same object all right this contains the actual scene object mmm fantastic wonderful we're so glad that you contain the scene object so this contains the actual scene object what I'm saying is we're going to do this instead of current scene we're going to say current so current is going to be that actual scene and then the current scene gosh um I'm really we're really getting some okay let's call this scene name previous same name and current scene name that's a lot less ambiguous so here this becomes self dot current because we're looking for the object and then we'll say of self dot current call an exit and then we're saying basically if next scene then self dot current scene name equals next scene and then self dot previous scene name equals self time current scene name and then we also say self dot current equals self dot scenes of next sing and really we need to check and see if that next scene is in let's do that oh if next scene then let's assert that self dot scenes we want to make sure that scene actually is in our scene map of next scene is not equal to nil otherwise say unable to find scene you next so we're just gonna assert we'll bail it just kills the whole program perfect perfect I like it I do I like it a lot it's great it's fantastic here for list scenes let's let's say get seen to get available scenes let's just do it that way and then we could say four key comma value in pairs of self scenes do and then we'll have a table and they'll call it a local table and it'll be seen they seen names and then will return that table turn scene names and what seeing names will what we'll do here is we'll say see names of the size of scene names plus 1 equals the key which is going to be that same name so we're just going to return that so that gets us a list of all the same names at the scene manager it was about and okay here's good if that exists and we call update if this exists then we call drawl now the next scene if we see if self dot current exists then we say self dot current yourself type current exit and then we say self current equals self scenes next scene and then we also want to do then self dot current : enter right because we have this inter let's put something oh this is a oh and something else we need to do no we're good so I guess we'll put all of the scenes yeah I'm not sure about the add and remove yet yeah the remove we can do we can do the remove and let's say remove scene name so it's going to be it's going to need to be a scene name and we'll say if scene name then we'll do some stuff and that what we'll do is we'll go through all of the scenes and we will say if if if the self dot scenes of the key I'm sorry if the key equals the same name that we're asking to remove yeah let's yes yes then then yeah well we're gonna break and we also gonna say self dot scenes of key equals nil so that's how you get it get rid of it from our map so the scene will no longer be in the map should we have should we have a destroy I mean it's not gonna hurt anything let's do let's have a scene destroy just like that so so if we want to if our scene gets removed yeah then we can we can call a destroy on it just in case okay and and again this this this the usability of this I mean we may or may not use this but we've got it if we need it we can always comment it out if we don't so destroy and I guess what we want to do is if had seen name yeah so we should say if scene name equals self dot current scene name well then we're gonna need to destroy that one too right to be to be all proper then we're gonna have to say self current equals nil yeah so if we can actually destroy the complete complete scene that way now this one again not real sure about the since the way we're doing this is basically we're passing in a bunch of scenes that we want to stick in here and we're basically having saying hey the scene directory I want you to have a list of all these scenes so we may I guess we could do this a little bit differently if we want to I suppose we could say add a scene to the list where scene is a subclass of seen is it needs to be an instance of a subclass of seen so again we'll say if sane then and we may never use this either but yeah well we'll go on in sticky na and we want we want a couple of things here we want to know that this scene should be a table and we also want to assert that that thing sane is a thing it's not then we'll say I cannot add none on scene objects to the scene manager cuz they're non scene you get it you get it another stupid otherwise we can go ahead and add it and what we can do is we can take [Music] scene and then we can actually take a same name so we'll just do it that way so then we can come over here and do something like this except it's gonna be scenes of scene name equals scene so we're gonna assume but the scene is already so we're assuming a couple things assuming the scene is already constructed okay and let's also assert that the type of scene name is a string which again we don't need we didn't have to do this but we will parameter scene name must be a string and let's set that scene name is not equal to MA it must be filled out parameter seen name must be specified right I'm gonna run it again make sure they've got no errors and I do have an error line 35 and I know where that is seeing missing a preparing right there here we are so I mean I think like we can I think this will work and let's you let's have another function scene manager should we call it pop-pop what that does is say if self dot previous scene name basically if we have a self that previous scene name then let's call self dot switch with that previous same name and then we're also gonna say self dot previous scene name equals nil basically this lets us returns to the previous scene if there is one you see you see what I mean so it's not a full stack we could make it a full stack and we may end up doing that but for now we're just gonna say hey you know that's like for things like menus and things like that if we want to go hey you know when you're done just pop to the next pop off that pop yourself off the stack and then go to that previous scene so that sounds really good to me I think let's do a couple other things here then let's have a scene let's call it let's call it test cuz that that makes me feel good test a test scene so let's come and grab all this and we'll come here and we'll say and this is going to be test test test and what we'll do here is we're gonna we're gonna take all of our stuff from our main dot Lua and we're gonna stick it in here it's gonna be great stick it all in here and the room bills switch back and forth at least that's what we're gonna try to do and let me go ahead and we know that we're gonna need a t dot T n colon updates this episode is running long I know I know and what we are going to want to do here is we want sprite in animation we're going to take those out completely because we're going to put them in here into test you see so test is going to [Music] we're going to get we're gonna grab all of this we're gonna actually we're gonna grab all of this put this into our new little scene here and when we load it or when we knew it let's go ahead and say when we when we knew it yes so when we new up our little scene here we can go ahead and do self dot Super dot : new seeing manager that's not gonna work it's gonna be error attempted call a global sprite oh no I'm ill value of course it is a nil value we're not done we're not done we're gonna go into our creation in here I want to go ahead and even though we're running long on this one I'm gonna do it enter and let's print so that we can see it inter yest and really that's probably what all I should do but I'm gonna we're gonna go for the gusto we're gonna grab all of this stuff so we're gonna grab all this let's actually leave the love graphics here and let's go here grab all of that stick that in here and let me just double paint it double paint it ladies and gentlemen it's all double paned and we're going to want this that's our little punch sound I know we're getting tired of the little sprite guy he's still cool though right hopefully and then we want to update stuff we'll leave this here because that's that's gonna be good for us that'll be that'll be perfect we'll grab all this and we'll remove our event-specific stuff that we don't care about matter of fact over here we'll go ahead and remove this this event stuff because like I said we don't need that right now then let's do this main menu and test but we're gonna switch the test comment that out what we'll do that in here in a minute and we have punch oh that's good see there's our scene manager but we want these two things oh it's looking so wonderful now if I run it will it work will it work of course not not yet test Lua 3232 oh look yeah yeah of course I added a joystick thing here so let's let's leave that out for now expected near then of course it is cuz I've got that I had a another pair in there I'm an attempt to index global key yes because I don't have the keyboard in here well I think I'm kind of thinking at the scene manager my end up getting the keyboard himself if that makes any sense at all the scene manager may be the one debt or eat it out of the key the keyboard would be a global so see it says local let's make it a global for now we may change that later test attempted index a global key a new value okay hist and what is that key yeah key is hook events and key update of course yes that's that's an issue an issue I feared an issue of thoughts let's see if we can just get it to come up I want to see it just come up at first and test oh I'm I didn't save it I didn't save it 39 main.lua 39 you know pulling all this stuff I tell you what it's just a pain isn't it are we doing the update for the sprite well of course no we're not doing that we've got to do that too otherwise you won't walk right and sprite 39 I didn't save it again I'm gonna save those things there we go and let's do this say on update let's switch which scenes that'll be cool right and let's see if we'll comma work so we'll say if we're doing that let's switch to the main menu it's gonna be cool if it works it's gonna be cool it's gonna be cool and well in the episode cuz rod time we ran out of time comma well how about that it was so let's see about else if ah let's go back to our testing test just like that so if we're here we go period or a comma then we go period to go back and actually I want to let's do it a little bit different , the main menus good so yeah look at that it's working and go comma period comma great oh oh and I could switch between scenes that's gonna be really nice I think oh and you see here we've got our little inter we call recall and enter every time we enter back in to our test scene because I had that little print here and what we can do is we can have an additional boolean if we want to only have this enter B actually run once we can do that by by doing that in on a per class basis so we could have like a local is enter or entered could be set to false at first and think this could be if not entered then you do stuff you see what I mean so that we could do it like this and we would say entered you equals true basically so that basically says you only call this once when we enter the test the first time but otherwise they only gets called once so that's a nice way to do that basically so that we can differentiate and allow they seem to do it itself and that's about all I have today has a lot of stuff we got a cool scene manager so we could switch scenes where we're closer to getting where we want to be and that is some kind of game out of this stuff so we'll see you next time
Info
Channel: recursor
Views: 2,459
Rating: undefined out of 5
Keywords: Love2D, Tutorial, Programming, Lua, VSCode, Software, Love2D-Tutorial
Id: ZIHUaba-C1Q
Channel Id: undefined
Length: 53min 49sec (3229 seconds)
Published: Thu May 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.