UE C++ Tutorial Series: Items, Interaction, Inventory #1: The ItemData struct and data tables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right let's get started so the very first thing that we're going to need to make is the item data structure um it's kind of it's kind of like a master structure that we're going to base uh our items off of and it's really important because once we have that in place we can create the item class then we can create the inventory to store the items and so on and so forth so the item data uh structure is is a pure it's not going to be a class it's just going to be a a struct stored in a header and so we're not going to create it through the editor like you normally would we're going to create it directly in the ide so if you're following along in visual studio the process for doing this is going to be really similar you should be able to the menu options might even be the exact same so when i before i started recording i like to reorganize my file structures to use the public and private uh hierarchy it's not mandatory it really is up to your personal preference but in the past when i've done projects it sorts your cpp files off into a private and your header files off into a public folder and i've just gotten kind of used to that uh and so i just like to reorganize my stuff that way so just go to wherever you want to have this header it's going to be kind of like a common header that we can include from anywhere and we're going to make a new folder so add directory i'm going to call it data just in case in the future we add more you know if you want to add more of these kind of data structs now i'm going to go back to add file and this one's just going to be called item data struct dot h item data structs because there's going to be multiple strokes in here okay and so now we've got a completely blank file there's nothing you know it it didn't set up any kind of framework or anything for us so when doing it this way creating it manually you have to add in a couple different things before you can get it to where the your you project will actually pick it up and let you use it as part of the code so first we need our pragma once that's a preprocessor directive that will basically make it to where it's kind of like a header guard it will make it to where you can't just keep including this thing over and over and over if something includes it and then something else includes that it will see that oh hey you know i already have a path to this header file and so it's essentially a way to keep it's all stuff that happens in the background during the process of compilation how it handles includes but anyway so there's that and then we need to include core minimal which is your one of the primary engine includes and then we need to include the name of the file item data structs dot generated oops dot generated dot h and so this is something you know when you compile the unreal header tool runs and generates all this other stuff in the background and it puts it into this generated dot h file and so you always have to include that and it has to be the last include just for you know future reference the only other thing we want to put in is the engine data table dot h file and this is because this the primary purpose of this file is to build this item data struct and then we're going to be able to be able to use it in a data table when we get into the editor and the type that we're making and go ahead and do that now is actually called a table row base so let's start by creating a struct and it's going to be called f item data and it's going to inherit from f table row base so this type as you can see here it's the base class for all table row structs to inherit from all anything that goes into a data table needs to be in of this type so now what's going to go in this so there's going to be a whole bunch of different pieces of info for the items that go into this the first thing is a couple of enums so uenum we're going to make item quality now if you don't know what this is about so you uh i believe it's that by default in an enum all the values in it it's just a way to match like a variable name up to an index essentially right so like enums can't really are the equivalent of like taking 0 1 2 3 and then putting a label on it that's easier to use so that normally uses an integer for each one of those but by doing this you're telling it use a unit 8 for each one and so it's kind of just a space-saving measure each of your indium fields goes into a un-8 instead of a full integer um so now at this point you know as i go through this if you don't like my nomenclature for stuff or the way that i describe things feel free to change the names uh to your you know liking and that goes for even the structs itself you know i'm just putting in the examples of what i've done but you you know when you think about what you want to put in to an item data collection put in anything you want the methodology is going to be the same as we move forward i kind of feel like with item quality i feel like games are all a little bit it's like repetitive nowadays everyone uses the same stuff you know you have like epic rare legendary and it it's just played out and it's just like everyone does that so i wanted to try to mix it up a little bit and did something like shoddy common quality something like that and then now they there's a metadata that you can put into enums you put you meta display name equals and then close paren and now you can put in a name here and what this is going to do if we actually go to this yeah see it sets the override display name and i believe what that's going to do is it's going to make it to where like say you had some kind of like identifier here that you didn't that was kind of uh i don't know something with numbers in it or something that was you know just a little bit ugly and you want it to show up in the editor a different way that's what this is about so this will make it to where when we access this enum via drop-downs and such in the editor we just see this so i uh i took a course or followed tutorial in the past where they used this and i just thought that it was seems like it's good good practice but again not mandatory all right so there's item quality so next we want item type now this one's really important because in the way i've implemented it in certain cases you want to especially in the user interface you want to display certain info or not display certain info for different kinds of items so say you hover over a consumable and in the item data you have like armor rating and damage value and this and that there's no reason to display that stuff for like a potion so by having an item type enum you can do a check for that in the user interface and other places and take actions accordingly so it's thrown a couple types here something like armor weapon um shield uh spell uh quest item and then like mundane where you know like in skyrim or something you can roam around and you pick up basically anything like silverware off the table nobody cares about it doesn't sell for anything that's what i would consider is like a mundane it's just stuff to kind of help flesh out the world so i'm going to copy this put these in and copy these names okay so there we have our type and let's see what's next so next we want um our like item stat so like item statistics so it's a struct item statistics and now this is where you would put in you know the stuff i just mentioned so um armor rating uh damage value you know anything you can think of this is where you put like strength bonus you know you equip an item and it gives you plus two to strength or something like that i'm not going to put in a whole bunch because actually i haven't gone to the point of using these um so like you know i'm still in the process of building up ideas for how to implement like a weapon and armor system and stuff like that and combat and everything so it's just more of a placeholder for now but definitely useful uh restoration amount for like consumables or potions cell value this is all kind of stuff that you want to put in here uh no so now each one of these is going to be a you property and it needs to be edited anywhere uh you the reason is because we're going to be doing all this editing of these items in in the data table editor in inside the unreal editor and so it needs to be specified properly for us to be able to set it and edit it in in the editor so there's item statistics so now we need item text data again this one's really important for your user interface this is where you would have your item name right crucial description [Applause] interaction text and usage text so interaction text would be if you encounter it in the world do you want it to say something specific if it's a book you know you could have it just be a readable thing so it just says read when you aim at it if it's a a box or something you find it would say open whereas you know when you have it in your inventory you want it to say something different for usage so like a potion's a perfect example when you encounter a potion in the world it's going to say pick up for interaction and then when you have it in your inventory it'll say drink or use you know something like that so again copy these you properties and before i forget for each u-struct we need to put in generated u-struct body that is just you know a macro that's important for the reflection system and put that at the top of each of these structs the enums don't need it they're fine the way they are all right so there's our text data we've got statistics we want uh numeric data so f item numeric data and now this one also very important this is where you're going to have stuff like weight maximum stack size so and then what i also did uh for the way i've done it as i put in a bool in here that it where it actually tracks is it stackable um this is because you know later on down the line when you have the inventory a stackable item and an um and a non-stackable item can be handled a little differently and also good good for the user interface because you know if it's not stackable there's no reason to ever really display this or the quantity because it's always assumed you're going to have just one so this one this is a very useful one that we'll use later on all right and so the last one for now is going to be item asset data asset data is going to be where we have our icon and our mesh now it would also be a really great place to store like a sound effect so you know how and stuff like diablo when you pick up items they all make a nice little sound effect like potions have a little bubble and the gems do like a ding um and that kind of stuff this would be a perfect place to store that because you know each item is going to be built off of this and this is going to be carried along with it so having uh those in here you know you set it all beforehand in the data table and then you can access those those really easily but for now i'm not too worried about sound right now i am going to handle meshes and like obviously you know setting that because we want to be able to drop the items into the world and then pick them back up so we definitely need a mesh but for now i'm just going to go with icon and mesh all right so this is all the crucial pieces that we need we can actually fill out the the data table struct now and all it's going to be is it's going to be one you know an instance of everything that we just made so we want item type item quality um stats i'll just call it keep the proper name text numeric and asset f item asset data all right and so now again each one of these needs to be a u property uh and but you know you can add a category on it because this is actually going to show up in the data table editor and then go ahead and put that on each of these all right and so now this is ready uh let's see so it's been added here it's all filled out it's got the required includes it's got our generated body macros it should be good to go but now how do we actually make the project know that this belongs to it and be able to include it in other places so for that i'm going to close my ide i'm here in my project folder and so by the way i called this cs tutorial for core systems being like interaction items inventory so that's what i named it obviously you know yours name whatever you want so whenever you make a kind of hierarchy change in terms of your source files whether that be adding a new thing like this from scratch or deleting because you know i don't know if you notice but you cannot really delete c plus source files from the editor you can do it here say i wanted to delete the cpp file you delete it come back and delete the intermediate and binaries folders and then you right click the u project and go to generate visual studio project files so like i said this goes for adding or deleting now you want to make sure before you do this especially with deleting that it's not being relied on by a bunch of other stuff but you know when i first started out i would act you know i'm real particular about naming of things a lot of times so i would name like a class and then get irritated because i didn't like the name and i realized you know well how do i rename this it's there's no easy way to do it really the easiest way is to create a new one with the name you want redirect everything in your source code and then come in here and do this deletion process that i showed so it's kind of clunky and it takes a little while but it is very useful and it's just generally the easiest way to make those kind of changes so i'm going to give it a minute and let writer reload everything and then i'm going to do a build and launch the editor and when all that's done we should be ready to use this struct and make a data table all right so i'm back in the editor now uh again you know all i did was after i did the regeneration i just did a full build i did have to add in one of these use truck body macros that i forgot give me a compiler error so that was quick then everything built and the editor launched so just a few quick things before i make the data table uh whenever i'm starting with a brand new project there's always a couple little things that i always set especially my editor preferences so okay so like if you ever go and open a blueprint and you're annoyed by the fact that it opens on top of everything the way that i am we can fix that go to editor preferences and then right here asset editor open location change this to main window that will make it to where it opens as a new tab i don't know why that's not the default the other cool thing i like to do is come down here to uh output log i made my font size a little bit bigger just for [Music] when you know i do print statements and stuff later hopefully it shows up a little better on the recording but this right here category colorization mode this is a cool feature that i just recently found if you set it to you can do like any of these options but category only i think is really nice because it just sets the name of the log category to a new to a color you can do as bad so it kind of like highlights it whole line but i kind of just like this it's minimal but it's a nice little bit of visual distinction there so those are two things that i always set uh the other thing is i go you know make sure that uh it's back in editor preferences make sure that under source code it's set to the ide you're using so obviously i'm on writer and then one thing i did before i was recording was i went in here to so i can reorganize these folders a little bit i made a blueprints folder i put the character in there and then i made a framework folder and went under my game to my game mode and made a blueprint of it this is important just because you can have a game mode override here under world settings where you can manually tell it to use certain things but i prefer to set it under project settings maps and modes you can set the game mode here and then specify what it's using here and then you don't have to worry about this override and so all i did you know default pawn class is the blueprint and then obviously the game mode is the this is the blueprint i made this third person character blueprint was one that was just part of the the default project you know when i made this project it's just a c plus third person so it came with this by default all right and so once that stuff is out of the way come back here to your content browser and we're going to make a new folder and this one can just be called item data because this is where we're going to have all the different data tables and so when i say data tables plural it's because the way i've decided to do it is to have a different data table for each kind of item grouping that i want right so go back in here when i talked about the item types so um you know mundane items there's no reason to have consumables and spells mixed together in the same thing there's you know there's no reason to really have a data table that mixes types it's just at least this is how my mind is working with it it's easier to kind of separate these into their categories because that's what you know this enum is essentially going to control so i decided to make a different data table for each type and you can obviously have as many of these data tables as you want you can divide it up however you want this just seemed the most logical way to me so go to right click in your folder miscellaneous data table and it's going to immediately pop up and say okay we need a row structure to use right because each row in the data table it's kind of like a spreadsheet needs to know what types are in its field you know what types are associated with its fields and so on and so forth so do that and you can either search or look right here and you can see there's our item data struct select that hit okay and i think i'm gonna start with uh i'll do mundane items to start with mundane items so open this up and you can see here these columns match what we put into our struct click add and this is what i was talking about you know by having it all in the item data category everything is nice and categorized for us in this tree structure we have the different structs here everything's really accessible it's all nice and clean and organized and easy to edit you know and you can add as many items as you want and go down through here tweak them and really it's nice because you just do it in this one place you this will be the math essentially what's you know the master copy of this item data and so if you ever need to change you just change it in one place rather than needing to go to multiple copies of an item and edit it in each one or something like that now one uh less important thing and i realized i forgot something so close this out i'm gonna close the editor it's just my personal preference whenever i make changes in c plus plus code i close the editor because i've been i'm still i don't really trust the live coding and especially not hot reload which hopefully is going away um but the main thing i was going to add that i meant to add that i forgot was an id so we want to have an item id because this acts as the essentially search field for the data table and i'll show you what i mean when we get back when i get back into the editor and open that up okay so i'm back in the editor and i'm going to go back in here to my data table and you can see now id has been added so if you look here and we have row name we have row name and then we have id now so when you go to search for an element in the data table it needs a row name and then it needs uh essentially like a search key let me find the uh so basically there's a function called find row and it takes in a uh here we'll just pull it up real quick yeah all right so here's the function so function to find the row of a table given its name it takes in a row name and then it takes in a context string uh and so these are you know and then an optional worn if missing weren't from missing but these two are mandatory but i didn't need them to be separate because i wanted to be able to essentially refer to a one like master item data to the database by the item id and that would be the way that i you know find and pull out the the item and all of its associated data so what i decided to do was just make these the same it's just the easiest thing um it doesn't you know it's it's easier to remember at that point and i'm gonna use some kind of uh like identifier that's like numbers and letters so mundane items it'll be like mun 0001 that gives us up to you know nine 999 possibilities of items and then you can go down and set that uh and you see the the row name needs to be something and so i just figured if my id that i'm going to use in the inventory is already a unique identifier why not just go ahead and set the row name the same kind of way so go down like this i don't need we don't need this many right now because you know don't have it i don't even know if i have meshes for everything probably have to go on the marketplace and find some so this really wraps up the data table creation section you can go down through here now and start adding stuff if you want to um i'll probably add a couple of little things uh just to have before moving into the next section uh but basically you know think of some items that you would like to have in a game mainly what should drive it is like if you want to use it as part of this tutorial series try to use something that you think you can get a mesh for because that'll be a lot more fun rather than using a cube or a cylinder or something like that you'll actually be able to get actual message meshes so they have a visual representation in the world so yeah just be thinking about filling out some of this and finding some some meshes and some icons for uh you know any kind of item that you want and we'll pick this back up in the next video
Info
Channel: George Pence
Views: 14,922
Rating: undefined out of 5
Keywords:
Id: LlAzsB-I1_8
Channel Id: undefined
Length: 31min 26sec (1886 seconds)
Published: Thu May 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.