Code an inventory system in Ren'py from scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to another renpai tutorial I've got a really cool one planned this time um something that I've been wanting to do for a long time and I've just been too lazy to figure it out but I'm going to show you how to implement a simple inventory system from scratch in renpai so I've seen a few different tutorials on this and the ones that I found are either needlessly complex or a little bit too simple for what I like so I thought I'd try to come up with an idea that's kind of right in the middle not too complex but also not too simple so we're going to design an inventory system that is relatively simple but is also extensible so we can add features to it later on this is most likely going to be the first part in a series I'm not entirely sure where this one's going to end up but like I said I'm going to kind of leave the door open so we can add more features later on I do have an idea or two but like I said we're just going to keep it relatively simple for right now so if you're new here be sure to hit the Subscribe button like this video and leave me a comment below if you have any ideas for uh where to take this inventory system and with that let's go ahead and jump in so before we get to actual coding um there will be a couple of pieces of requisite knowledge that you should have you should be somewhat familiar with Python Programming and you should also know a little bit about oop or object oriented programming thankfully I've done videos on each of these topics so if you need a little bit of refresher on that be sure to check my links above I'll put those on the screen for you um and you should also know a little bit about string formatting um so one cool thing is that in this one we're going to be using a new feature or two that is in rimpai version 8. so version 8 came out a few months ago and the main difference between version 7 version 8 is that version 8 is built on top of Python 3 whereas version 7 and before were built on python on python 2. so there's one new feature in particular that's and in particular that's going to make our lives a whole lot easier and I can't wait to get to that but it has to do with string formatting um all right so with that let's go ahead and get into our code um so I've got my YouTube tutorial seen that we've been working with in our previous tutorials already got that open um I've deleted everything from the script except for just the scene and our uh character um so really simple and I'm gonna go ahead and put the code for our inventory system just at the top of this script um I normally would put this in a completely separate file but just to keep things simple for right now I'm just going to put it all in the same script so the first thing we're going to do is we're going to create an init Python block so what this does if you don't remember is that in it means that it's going to run at initialization so whenever uh rimpy loads in it's going to load all the init statements before it does anything else and since this is a python statement that means that anything in this block is going to be pure python code so our regular our regular rimpy statements aren't going to work in this so we're going to have to use some statement equivalents which I have also covered before so be sure to check that out statement equivalents are just how to use our regular rimpy statements in uh pure Python and I'll get to that when it comes up all right the first thing we're going to need is we're going to need two classes so our first class is going to be for our actual inventory um so we're going to create an inventory class and we'll go ahead and get our Constructor created remember we need to pass in self as an argument and the other arguments that you passed and pass into your Constructor are going to be determined by what exactly you want to do with your inventory for right now all I want to do is keep track of what is in our inventory and how many items total exist in inventory so I'm going to go ahead and just say items that's going to be a list that contains the items that are in our inventory and I'm going to create another argument called number of items which is going to be the number of items in our inventory um then we have to do let's see self dot items equals items remember this is just some boilerplate that we have to do for any class that we create number of items equals number of items and that is it for the Constructor pretty simple so far um but like I said we're gonna make this extensible so there's there's a lot a lot of different places we could go with this um all right I'm gonna go ahead and leave that as is for right now we're gonna go put some methods in that a little bit later but for now I want to go ahead and create our inventory item class so class inventory item um so this is just going to be the individual items that we can put into our inventory and again there are lots of different directions that we could go with this um but for now we'll put in the self keyword all I really want to know is the name of the item and then we're going to give a description of the item and that's all we really need so self.name equals name and self.description equals description uh one thing that I also want to point out is that I am no longer using my beloved atom uh atom uh text editor but instead I'm using visual studio code that was another thing that they introduced in Python 8. I'm going to do a video soon about the differences between Python 7 and python 8. some of the important new things that they've added to it but one of the things is Visual Studio code support which I prefer Visual Studio code to atom I use it whenever I'm programming in pure python or whenever I'm doing anything in JavaScript HTML CSS any website stuff and now I use it for python for a rimpy as well uh but again I'll cover that in the future all right so that's all we need for our inventory items and I believe that's all we need for that entirely all right the next thing I'm going to do is I'm going to attach a couple of methods to our class and remember a method is just a function that acts upon our class so methods just do things um so what do we want our inventory to be able to do or what do we want to be able to do with it so right now all I really want is to be able to add an item to the inventory I want to be able to remove an item from inventory and I want to be able to create an output list of all of our all of our inventory items that we currently have um so the first thing I'm going to do is I'm going to create a function for add item and we're going to pass in the item to add as an argument we're just going to pass for right now I'll go put in the code later uh we're also going to do remove item and we need to pass in as an argument the item to be removed oh and I also need I actually forgot this when I was testing this earlier we need the self argument that is required of every method to pass in self as an argument so we know that we're referring to this particular instance of the inventory class and then I'm going to do list items and for that one I think I just need self that's all I really need for that I don't need to pass any other arguments into it all right so whenever we add an item what we need to do is we need to actually add the item uh to our inventory uh which is going to be our items right there so that's a list that's going to keep track of every individual item in the inventory and we also need to increment the number of items by one um so the way we're going to do that is first of all we're going to say self dot items so this is referring to our um um items attribute and then we're going to say dot append and then whatever item that we passed in as an argument self dot items dot append item uh and then we're going to increment number of items so we're going to say self DOT number of items plus equals one and so that's going to increment that by one and then when we remove an item we just need to do all of that but in Reverse so we're going to do self dot items dot remove item instead of append so again if you need you might need to brush up on your list uh methods um if any of this is a little bit confusing but a pinned just means to add an item to the end of a list and then remove means to remove that item from a list um then we're going to say self DOT number of items minus equals one so that will decrement the number of items whenever we remove one all right and then I want to be able to list the items so what I'm going to do is I'm actually going to have our main character uh I'm gonna have our main character describe all of the items that she is currently carrying um so we're gonna have to use a couple of say statements in here so the first thing we're going to do is check to see if there's anything in the inventory and if there isn't then our character is going to say I'm not carrying anything um so we'll do that first we'll say if uh self dot items actually let's say length of self dot items if that is less than one uh then we are going to say so here's where we're going to use a statement equivalent so let me go back down here to my main script we're going to show Lana neutral at left so normally um actually let's do I'm going to change that to Luna there we go all right um normally what we would do is we would say um just type in Luna dot C and say I'm not carrying anything there we go so again like I said before this is a pi is a um rinpai statement and python is not going to know what to do with this like we're putting it in a Python block so we have to use pure python so instead we have to use a statement equivalent we have to put it in terms that python will understand so the way that we do that is we're going to type in the name just like before luna.c and then in parentheses we're going to put in our statement I'm not carrying anything and we'll go ahead and put in an else block and then pass it there we go now let's go ahead and test this so far I'll see how this works so the way that we are actually first I need to um I need to actually create an inventory object let's go ahead and do that inventory equals inventory so here's just where we're creating an instance of this inventory class so um again to recap on our oop stuff this is just a template by which we can create objects you can really have as many inventories as you want so you could create a different inventory for every single character in the game if you wanted to um but in that we the self argument happens automatically but we have to tell it a list of items to include and then the number of items total so right now we're going to start with nothing so I'm just going to pass in an empty list by putting in Brackets and then I'm going to put in a 0 for the number of items there we go and then we're going to call a method on that inventory class or that inventory object we're going to say inventory Dot and then what did I put what I call that list items and since this technically is a function we have to end it with empty parentheses normally we would put um any uh parameters arguments in there but we don't have any so we don't have to put anything in uh now let us check that real quick and see what happens oops I'll see what I did wrong oh so this is also a python statement so I've got to precede that with a dollar sign and up here I have to say default inventory there we go so I'm declaring this with a rempi statement but when I call that function I have to use a python statement and another way to call a python statement instead of doing the Python block is you can just put a dollar sign at the beginning of a line and it will just treat that one line as a python statement now let's give that a try there we go we did not crash and burn this time so let's go to start and yeah she says I'm not carrying anything and I just realized that I called in an empty Sprite so I'm going to go ahead and change this back to Lana where it was before there we go and let's check that one more time excellent there we go there we go we're good so when I called that method it basically ran this bit of code that said if the length of our items list is less than one which it is it's a zero then we run this bit of Coach he says I'm not carrying anything so now we're going to create the else so if it is not less than one meaning if it's one or above um then we're going to do this so we're going to go ahead and put in a bit of text so it says I am currently carrying and this is where the fun part comes in so again if you need to brush up on your python go back and check out my my earlier lessons on that but we're going to use a for loop we're going to say for item and self dot items so we're going to run through all of these as a in sequence and we are going to have her say oh sure I did I am currently carrying let's see so here's where we're going to use our F strings and this is a feature that was added in Python 3. so by putting F immediately preceding a string you can put variables right in the string now we can already do that in um in renpai by using brackets uh square brackets in our dialog statements but in this we have to use the F and we have to use curly brackets whenever we're working in pure python uh so she is going to say item dot name and then item dot description there we go so for every single item that's in our inventory list she's going to say the name of the item and then the description of the item the problem right now is we don't have any items so let us go and create some items we'll do that right under our inventory so we'll say we're going to do this as Define generally you should use the default keyword for something that can change and then the Define keyword for something that does not change if you remember when we were creating our character objects we used Define so I'm going to go ahead and do that for the inventory items we're going to assume that they can't change and as I said before you can create as many instances of a class as you want so that's what we're going to do right now we are going to instantiate or create different instances of this inventory item class every one of them is going to behave just like this but we can put a different name and description in each one of them if we want to that's what we're going to do so we're going to say Define inventory item oops and oh wait no no I'm doing that right actually no I'll take that back sorry about that when I Define it I'm giving it the name of the variable so I'm going to call this door key there we go then here's where we instantiate and we're gonna call that inventory item and then we have to pass in a name and a description this is how we want it to appear on the screen so we're going to say door key and try using it in a door now let's do another couple real quick let's say Define chest key like maybe for a treasure chest equals inventory item chest key and try using it in a chest and let's do one more thing Define stack of money Tori item you can use this to buy things there we go so right now if I run this nothing really is going to change our script's going to look exactly like it was before because we still don't have anything in our inventory when we list items so we have to add things to our inventory and how do we do that well we created an add item method for our inventory class so we can use that to add an item so let's say um inventory dot add item and we will add a door key and let's also do oh I forgot these have to be pure python so we have to put our dollar sign there and let's do inventory dot add item chest key so if we say and we put in some dialogue let's say let's see what I'm carrying and then we'll go ahead and put inventory.list items so when she calls up it calls this one she's not going to be carrying anything then we'll say I'm going to pick up the there we go all right let's run that and see what we get all right let's see what I'm carrying so she's not carrying anything and then I'm gonna pick up these Keys now let's see what I'm carrying I'm currently carrying door key try using it in a door and chest key try using it in a chest there we go and just to show you how the rest of this works let's say we want to and I'm going to go ahead and put these in a in a Python block just to keep it a little bit cleaner so we don't have to keep putting dollar signs so let's say after we list items um let's do let's say What if I put down the door key so let's say inventory dot remove item and pick up this stack of money and this is going to prove that we can set items down and pick items up by or remove them and adding them from into our inventory let's see what I'm carrying so carrying nothing I'm going to pick up these Keys now I am carrying a door key and a chest key what if I put down the door key and pick up this stack of money oh and I forgot to hit list items again there we go now let's give that a try that should work let me just reload that yeah shift R uh if you don't remember the hotkey for reload that really comes in handy to save some time uh let's see what I'm carrying I'm not carrying anything pick up these keys door key chest key now I'm going to put down the door key pick up the stack of money I am currently carrying just the chest key and a stack of money there we go so again a really simple way that we can create an inventory system that is reusable so we have these really simple methods right here add item remove item and then list items so we don't have to put out this bunch of code every time we want to do one of those things we just put in one simple line and it does it for us automatically so as I said before this is extensible so what else could we do with this well one of the easiest things that we could do is um I'm not going to do this now but I'm going to leave this to you to experiment with but let's say that you wanted to um only have your character be able to carry a certain weight or a certain number of items then you could create a weight attribute uh for every inventory item that you pick up and whenever you add an item check the weight and see maybe add a maximum weight value to your inventory and then if you exceed that weight it won't let you pick up any more items or if you pick up too many items like if you can only pick up 10 items at a time keep track of that with number of items and um then if you pick up too many items then uh like it won't let you pick up the 11th item if you set 10 is a limit so lots of cool things you can do one thing that I'm currently experimenting with is doing a graphical interface for a menu that we can also make extensible so hopefully I'll be doing that pretty soon but in the meantime be sure to hit the like button to subscribe to my channel if you haven't already leave me a comment below if you have any questions about this or if there's anything else that you would like to see from a from an inventory system and I'll see if I can do a video on that in the near future and until then have fun and we will see you next time goodbye
Info
Channel: Coding With B and E
Views: 9,912
Rating: undefined out of 5
Keywords: renpy tutorial, renpy games, renpy, renpy programming, inventory system, renpy inventory, renpy inventory system, python tutorial, python basics, game making, visual novel tutorial, oop, object oriented, renpy oop, python oop, how to make a visual novel, game development, visual novel, renpy tutorial for beginners, renpy tips and tricks, renpy guide, renpy how to, intermediate renpy tutorial, intermediate renpy, statement equivalents, custom functions, beyond the basics
Id: 237jY-KtbVA
Channel Id: undefined
Length: 23min 15sec (1395 seconds)
Published: Tue Nov 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.