[Music] hey guys this is Mitch with fine boy CGI
and today we're going to talk about how to create a flexible grafting system inside of Godot so
this is going to be kind of like an expansion of our inventory system so if you haven't looked
at that please go check it out before you do this tutorial because there's a lot of interplay
between those two tutorials and this one they're basically required but just keep that in mind but
we're gonna go through the process of planning out our system
then we're going to go into adjusting
our items so that it can actually craft items then we're going to build out our crafting menu and
get that settled then we're gonna do a bunch of bug fixes because I never bug test my code and
then finally we are going to go ahead and learn a little bit about how windows work and the fact
that it actually eats my event and I have to go to a panel instead so that was something I had
to learn as well so that's what I have in store for you guys today let's
go ahead and get started
okay so the first thing we have to talk about is how we're going to change our actual design here
so I've kind of gone through and already changed it so that way I can just kind of run it through
for you guys so first things first we're gonna have to add a can of Ford so basically the idea
is that we're going to check if we can afford an item and if we can't then we're just going to
return false here but if we can we're going to filter our items we're going to chec
k our quantity
is over a required amount and then if it is that we're going to return true and say yeah we can
afford that and if not then of course we'll return false we'll say hey we can't afford this so it's
not going to happen now if we head over here once we can afford an item in our inventory then we
can remove those items so you can see that we'll check if we can afford it so that way we don't
remove something that doesn't exist we'll get all the items of that type we'll get an item
from
a list because what's going to happen is going to return a list of items that have those things
because it's possible you could have two or three stacks of an item then we're going to remove that
quantity of the item if the quantity left is zero we're going to remove it and then we'll Reflow
our buttons and then if the quantity that is remaining is zero then we will return successful
if not then we will attempt to pull again from our list and we'll Loop through this until all of the
i
tems in our list are completed and as long as our can afford is correct we should be okay with just
running through this code without error checking because we've already done our error checking
above so we should be okay now over here in our inventory class list we basically are going to
be adding in the states that's going to be new because we're going to need to be able to actually
check what our state is and we're going to have to set up a current state system as well that way
we know f
or crafting or if we're not crafting because if we're crafting our code is going to
have to react a little bit differently than if our state is not and also it's a good idea to
always have a global state of what your inventory sitting at so that way you're not doing something
that you shouldn't be doing now once we have that let's take a look at our inventory button so if
you remember we had an inventory button you can see we have added a inventory button State and
a current state that's go
ing to allow us to have one button that does two things or three things in
our case it's going to basically check to see what the state is and if the state is inventory then
it's going to use that item if it's not then it's going to craft that item that way we can use the
same button for two different interactions without needing to recode additional things now I know we
could use inheritance to do this but really it's a tiny change so I'm okay with just adjusting it
slightly so basically w
hat we'll do is We'll add in a state check here to use the item or craft the
item we'll also do the same thing with our setup button if our state is crafting our inventory will
set our state to crafting or inventory and then if we update our button we basically will be setting
our icon setting our quantity setting our text and then ending from there and that's basically the
gist of what our inventory button is going to do now in terms of our item because we have to have
an item to actually
make this work uh we need to adjust our item to actually add in a list of items
that make this into an item and actually I don't think I I actually don't think I have it in here
so let's add that so let me click on this guy there we go so we'll add item craft makeup which
will be a list of items and we'll want to add a few functions one will be cancraft item which
basically just says for all items in item makeup we will go ahead and check to see if our inventory
can afford it if it can retu
rn true if not return false and then that kind of leads into our craft
item so first what we'll do is we'll get our request our list of items then we're going to run
through those items and verify if we can afford them if we can afford all of them then we'll we
will remove the items from the inventory add them add the actual crafted item and then we will
return true if if we can't afford them then we will return false and that's basically how that's
going to work now in terms of design what
I'm thinking is something kind of like this so we'll
have a we'll have a button here we'll have a craft amount and a name so we can basically say you can
craft a hundred of these one of these things like that and they'll have the name underneath them
so that's kind of what I think we're going to go about and do so hopefully this gives you kind of
an idea of what we're going of how we're going to approach our crafting menu and let's go ahead and
build this okay so first things first we need
to actually set up our item so let's double click
on our item and bring that up as a script you can see here we have our base item we have our use
item we have our memberwise clone whenever we want to copy it so we have a lot of our stuff in here
what we need to do is we need to actually set up a dictionary for our item makeup so we'll come
in here and we'll say public Godot collections Dot dictionary and I'm going to make that a
type resource comma int and we'll call this item craftable m
akeup and that's basically going
to build out what our object is made out of so if you remember in our design we basically said
that this was going to serve as what items make up our item so remember our crafting system
is going to basically take these guys verify that we can afford the value which is that
integer value so it's going to be the item and the cost if that makes sense so you have your
resource which is your item and then the int which is your cost and that's basically how we're
going to handle this now we need to basically set up how we're going to check for our items so
you can see here we have our item craftable Bank up right so we're going to have our items inside
of that craftable makeup but we need to actually have it so that it checks if we can actually build
that item so we'll come in here we'll say public bull can craft item and in this function we're
going to Loop through our our item craftable makeup up here we'll Loop through those guys
and then check
our inventory if we can afford it so we'll say VAR count of afforded items is
equal to zero or in my case I should probably make that an integer and then we'll say
four each VAR item in item craftable makeup and here we'll need to do some checking so we
have us looping through so we're going to pull back our item collection so we'll pull back our
resource and our integer but we're going to run it to a slight problem so we need to check if our
inventory can afford that item if you remember
we have in our inventory if I pull this over here
we have in our inventory the ability to check if we can afford an item I believe it's right
here and we pass in an item right here now this is great but this is a private Boolean right so
it's a private function so we technically can't use this without having issues right because it's
private so we're going to change this to public which means that we're gonna also have to change
this to a Capital C but you'll notice that when I change this
to a Capital C it messes with my
references you see when I click up here it shows my different references I have two of them uh and
it looks like I basically have one I think this is just a generated Source I'm going to change this
to a Capital C and I'm going to change this to a Capital C like that way my stuff maintains that
nice naming convention that we're looking for now you'll notice that we don't actually have a
reference to our inventory over here on the left right all we have acces
s to is the inventory
object right so inventory you can see here's our object but that's not what we're looking
for so how can we make our inventory into a referenceable object in our project we have a
lot of different options but one of my favorite way to do it is with the game manager the game
manager is supposed to be the guy that houses everything that's Global anyway so it actually
works out pretty well so we'll scroll down here we'll find our gamemanager.cs we'll come down here
and w
e'll say public static inventory inventory like that and then we need to set that value so
I'm going to scroll all the way up here so first things first we need to set it so we'll say if
game manager dot inventory is equal to null then we could go ahead and set our
game manager dot inventory equal to this else if it is not null then
we know that our inventory already exists and you never want two inventories to ever
exist at the same time so we'll cue free and that'll delete our inventory i
nstance hey guys
editor Mitch here so something I just want to jump in and say is there is a possibility if you're
doing anything with your inventory on ready with anything in your code base you could run into an
issue where it's saying hey your inventory is null your game manager.inventory is no right and the
problem comes in because of how Godot does it script execution order so what I found can help
mitigate it which in this case the way we have our stuff set up you're not going to run i
nto this
issue but I want to make sure you guys are aware of it what you can do is you can do a Constructor
instead so if you come up here and you say public inventory and then you just pass in like this and
then you just cut this and paste it up here that will make this be ran immediately as soon as the
inventory gets con gets built and the game object gets initialized this will show up and it'll
run this so the reason why this is important is because how the script order execution Works i
n
Godot is the initialization of an object happens before the readies are Ran So if you have a bunch
of objects in a scene the initialization will run first and then it once all the initializations
are ran then it will come through and run your ready on all of your different scripts so if
you're having an issue with script execution order just make it a Constructor here and that
should solve it anyway let's get back to the video now once we have that then we can come
in here and say if gam
e manager.inventory Dot can't afford and then we can pass in our item
but we're going to run into a slight problem first things first this is an item so if I move
this inventory over here we can kind of get a better look at our item here first things first
this is not an item this is a generic key pair right so we need to hit dot key to pass in our
resource here but you'll also notice that hey well now we can't convert our key to an item so
what gives right what can we do to make this work
well we could always cast this
as a key so we could just pass in brackets here and and type item and then that'll
force it to be an item and that'll solve our problem but that doesn't necessarily solve
our actual problem so the way our logic works with our inventory if we pull this guy out and we
take a look at it if you look at your can of Ford you can see basically what we're doing is we're
passing in an item but unfortunately that doesn't fully do what we need an item has a value attache
d
to it right has a quantity you can see right here we have our little quantity and basically if
we run through and just check based off of our items quantity that doesn't actually solve the
problem because not everything has a one-to-one relationship right so you you can't say well one
ER Plus One gunpowder will equal a healing potion right you have to say Okay well you know there's
a possibility that we might need two herbs and one gunpowder to do a thing so what we're gonna
do is adjust
this so that flexibility is a bit more at the Forefront so I'm going to hit comma
and I'm going to type quantity so int quantity like that and you'll notice that that's going to
break our references so we have some references here for our can't afford you'll see that it
says there's no argument given that corresponds to required parameter quantity now we could do
one of two things here we could just do comma item dot quantity and call it a day or if we want
to maintain that backwards compa
tibility we can just come up here and say public Bool can afford
item item equals pointer can afford item comma item dot quantity and that way we achieve our
original goal of expanding our can afford but also maintaining that backrest compatibility for
everyone else that isn't going to use the quantity input if that makes sense so now we just have to
adjust our can of Ford now it should be relatively easy basically we're going to be checking where
the items are in a list we're going to be l
ooping through those guys and then we're going to add
up our items quantity and then we're going to be checking if it's greater than the looped
quantity so we basically can just change this to quantity and we should be good to go there now
once we have this it should be as simple as coming back to our item and then just saying comma item
dot value like that and basically that's going to run our can afford now all we have to do is
increment our count of afforded items so plus plus and then o
nce we get done with all of our item
craftable makeups we basically can just say if count of afforded items is greater than or equal
to our item craftable makeup dot count and if it does return true else we're going to go ahead
and return false so basically this this just says hey can we craft our items and if we can
return true if not return false simple enough now once we have this all we have to do is
we have to make one more function and that is to craft our item and it's going to be
r
eally easy it's going to be a public void craft item and what we'll do is we'll basically
just say if can craft item like that then for each VAR item in item craftable makeup
Bool success is equal to game manager Dot inventory dot remove and then we can remove our
item well hold on a minute a we need to cast our item like we did before right so we'll do this and
cast it as an item but B you'll notice that that this isn't going to work right we're going to need
to pass in some kind of quanti
ty value like we did last time much like we did up here with our can
of Ford we need to basically say hey we need to remove a specific quantity of things not just an
item so I'm going to hit comma item dot value and then I'm going to put a semicolon like that now
it's going to get matted to say hey no overload method for remove takes two arguments right well
that's okay so we'll go into our inventory and we'll make that quick adjustment we'll come up
here we're going to do the exact same th
ing so I am going to make this pointer equal to pointer
remove item comma item dot quantity like that and then I'm just going to put in a public pool
remove item item comma int quantity like that and that once again allows us to maintain
that backwards compatibility again backwards compatibility is the most important thing now you
might be wondering why am I doing a double check right so I have my craft where I check can craft
item where I check can't afford right but then I also do it here
in my inventory and that's just me
doing a safety check I want to make sure that my stuff is good before I start ripping things out
of your inventory because it could cause a lot of problems in the future if you do it wrong and
you don't want people to have items just getting removed from the inventory without them being able
to afford it so we're going to grab this quantity and I'm going to go and find anywhere where
it says item dot quantity which you can see we copy it right here so it'
s going to be current
item.quantity so right here is one right here is two right here is three four and five and six and in
theory that should just work now hopefully it does we'll find out when we get there now once we have
this we can just go into our item we can check if we were successful so if not success then we can
basically just say hey it failed so GD dot print fail to remove plus item dot name well that's not
quite what I was looking for item dot key dot name and we will return th
at way they don't get
their item right we don't want them to get the item if something goes wrong we don't want
somebody get something uh that they might not have actually paid for so we'll return and then
we'll also do game manager dot inventory dot add and we will add our self or I guess since this
is C Sharp it's going to be this there we go and there we go so now we've updated our item
we've got our items settled and ready to go now we need to go in and build out our crafting
menu so t
hat should be pretty simple so we'll go back into Godot here we'll right click on our
inventory add a child node and let's add in a window I have really never used windows so this
will be interesting so we'll come in here we'll type craft we'll type crafting I think crafting's
a good one we'll move the position down to here we'll increase the size to about here ish and then
we're gonna right click add a child node add in a scrollable container we're gonna right click
that add a child node a
dd in a grid container like that we'll set our grid container to four and
hopefully that'll just work but I'm gonna go into my scroll container and I'll drop in a full rect
just to make sure that it's to make sure it's full screened on this because for some reason I can't
even see it so that must be something to do with how windows work again I'm new to Windows so this
like I said is going to be very interesting now if you remember we said we were going to override
our inventory button and
use that as our actual button so I'm going to drop in an inventory button
and I'm going to duplicate it four times like that and you can see about how big this is gonna
get so if I duplicate these guys as rows okay it looks like that's gonna work
for what we're trying to do I'm gonna resize my window to be properly sized
for that so we'll make it about 190. I think that'll work so I'm going to grab these
guys I'm gonna delete them yep and I'm gonna rename this to crafting menu like that now
I'm
gonna right click my crafting menu I'm going to attach a script and I'm going to call it crafting
menu uh dot C sharp I want to make sure it's a C sharp so I'll hit create now the goal of this
window is basically we're just going to house our craftable items we're going to house our
craftable button and we're basically just going to be adding some buttons to our craftable
window so we'll come in here and we'll just export out a variable and our variable is
going to be an array so we'l
l type public item we'll make that into an array and then
we'll call that craftable items so graph items like that what this is going to do is
it's going to house all of our craftable items so think of it as every item that's available
to be crafted in the game is going to be right inside of that list we also need to house button
of some description so I'm going to export out a public packed scene and I'm going to call it
craftable button and this craftable button here is actually going to
be used to basically be
our button much like how our inventory Works where when we start up we populate our buttons and
we have a section here where we add child to our grid container we have to do basically the same
thing and that means that we should also have a public grid container as well so we'll head
to our crafting menu here we'll say private grid container grid container and in our ready
we'll set our grid container equal to get node grid container and we'll pass in our grid
conta
iner so if we take a look at our Godot project it is under scroll container grid
container so we'll type scroll container slash grid container like that and now much like our
inventory we need to add our inventory buttons right so we have to add our craftable buttons
so we'll say for each of our item in craftable items and will will instance an inventory button
so we'll say inventory button inventory button is equal to craftablebutton Dot instantiate
like that and you'll see it's going to b
e mad at us so what we'll do is we'll cast this as a
inventory button so we'll come in here and say inventory button like that and then
we'll say inventory button dot update item and we'll pass in our item comma our index
which doesn't really matter in this case Zero and we should be good then we can just
type grid container dot ADD child and add our inventory button now you might think
that this would work right well I mean we're adding our inventory button so it should work
right well ye
s this is going to work but it's not going to actually make a craftable button
to do that much like how we planned we need to go into our inventory button here and we need
to actually make our inventory button be able to handle different states and the reason why is
because the inventory button and the craftable button are literally the exact same thing the
only difference is one fires off a craft event and one fires off a use event so it's really
that simple so all we have to do is come up
here and create an enum and create a state for
this so enum inventory button type and we'll say inventory comma craftable like that and then
we can just come down here and go public inventory button type current inventory button type you see we
have an underline that's because this is not a public enum so it's going to get a little
mad at us so there we go and under our update item we're going to need to be able to tell
it what our inventory type is so we'll say inventory button type type
and we'll
basically just come down here and say current inventory button type is equal to
type and that'll just allow us to set our button type now again you might be wondering
why are we doing it this way well basically the inventory type doesn't necessarily matter
the only time it really matters is when we want to do some limiting factors so for instance
being able to click and drag right we don't want to be able to click and drag on craftable
inventory types right we only want to be abl
e to click and drag on our actual inventory so
that's why we're doing it this way because that gives us that little bit of checking so that
way we know what type of button is being spawned so I'm going to save this guy I'm going
to go into my references and I'm going to have to update all my references so you'll
see our crafting menu has one so we'll say inventory button dot inventory button type
craftable like that and then we'll go to our inventory you can see here where we're updating
t
his guy we'll just say inventory button type inventory I'll just copy this guy so I have it
and these two are indexes so that's fine none of these are going to be used for us I don't believe
oh actually they will be used for us so we'll just comma paste and comma paste and that way we just
maintain having everything just kind of work for us and the one up here is just a generated
script so I'm not super concerned but once we have this all we have to do is go into our
crafting menu this shou
ld just work and we'll need to do one more thing before we can close
this part out and that is if you look at our inventory button you'll see here under inventory
button pressed we have a check if inventory item is null if it isn't then use item right well that
means that we also need to check if our current inventory button type is equal to our inventory button type inventory and if it
is an inventory item then we will use it else if our current inventory button type is equal
to craftable
then we can do inventory item Dot craft item like that and that'll basically
hook up our button event so now at this point we should be almost there I think
so what we'll do is I'm going to pull some Sprites because previously in my GD
script version I just use the Godot Sprite and that didn't quite read very well so let
me grab some Sprites and I will be right back all right now that I have the icons imported
you'll see I have three icons right here so I have a gun powder looking s contain
er I am not
an artist by the way so these will probably look pretty terrible to you art guys but I have a small
gun powder right here I have a small iron block right here and I have a small key right here
although some of the lighting in it is broken all right now with that fixed let's go ahead
and build out our items now if you remember we were discussing how two items can make up
another item so I'm going to come up to my res I'm going to right click create a new folder and
I'm going to
call it items and that'll just be a place where I'm going to store some of my items
because at this point it's going to get kind of ridiculous as I create more items and things like
that it could become a problem so what I'm going to do is I'm going to right click create new
resource and I'm going to click on resource and I'm going to call this iron so I'm going to
click on this resource click on the script load a script and I'm going to load under add-ons FP
inventory system script and I a
m going to add in my item and that you will see creates my item
and gets it all set up now all I need to do is I could just duplicate this guy and call it key
and then I can duplicate this guy and call it gunpowder now for the name I'm going to type gun powder resource path I don't have any for the
icon we're gonna come in and use my UI icons and we'll drag gunpowder into there and for
sub item or anything like that we don't need to worry about that and we'll set the value here
for the ID t
o be five for iron we're going to set the ID to six we're going to name it iron our
icon is going to be our iron icon our quantity needs to be one our stack size needs to be one
and for our gun powder same thing one and one and then for our key we're gonna make uh ID 7
and we're gonna call it key and for the icon we will use our little key icon and for our
quantity one stack size of one we're gonna save all of that now to get our crafting menu to
actually work what we need to do is we need
to set our crafting menu to have that craftable
item so I'm going to build this real quick and you'll see it built successfully which is
good I'm going to click on my craftable items I'm going to add in one to my array and you'll
see I have it empty so I am going to drag in my key just like that easy enough now if you're
running a different version of Godot you may need to set this as an object in 4-0 I had to set it
as an object and then pass in my key from there so just keep that in mind
but in it looks like this
time Godot may have fixed it so what this is going to do is it's going to populate a button over here
in my crafting menu so that should work perfectly the only thing that we're really missing now is
the craftable button so we'll add that in real quick which is going to be our inventory button
so we'll go into our add-ons we'll come down here to our inventory button we'll drag that guy in
and that should do that and then we should be relatively good we do have the
add item and
add item two because we're going to need to add items to our inventory to be able to craft
so let's head into our inventory and make that quick adjustment so we'll come over here we'll
drag this guy down to here where it says add and remove so in our case we're going to be adding
and removing our gun powder so we'll grab that will control F paste and drop that in so it's
going to change out that and then I'll do the same thing for chest combined Ctrl F I'm going to
grab my iro
n control a control C and Ctrl V we're casting this as an item and I set my gunpowder dot
PNG not my actual gunpowder that was my fault so I'm going to go into my items I'm going to
copy this select this guy Ctrl F and paste so that way it grabs the correct item that was
just my fault for not paying attention so we'll grab these guys and paste them in there we go
now if we refresh and you will see that if we hit go it's just going to create a key so what
happened well our craft of item craf
table makeup for that key is empty so that means we can
technically craft it whenever we want if we actually click on our key you'll notice that our
item craft will make up as a dictionary that's empty so let's actually create this so we'll
click on our new key we're going to come up make it into an object and then we'll drag our
iron in and then we'll come in here click make it integer say I want one iron add that key
value pair do the same thing here so object gunpowder and then integer o
ne and
we'll add that and now if we hit play you'll notice that if I click this it no longer
does anything you'll notice that if I add two of these guys and I click they get removed and the
key gets created so that's cool and if I click it again nothing so what happens if I add two of
each and I click it creates the key easy enough so now we have a very basic crafting system that
actually works and the coolest thing is that we can actually click these guys and you can see we
could use our
items so that's all really cool but there are some things that I don't like about this
first things first I don't know what object this is it's just an icon right and second of all I'd
like to know what makes this object up so what are some things that we can do well first things first
we can expand this button and make it a little bit bigger that's going to allow us to have the name
underneath it and I think that will be a good first step the other thing that we should add is a
hover over
menu here that allows us to hover over it and it'll tell us what is required for this
item to be made up so to make our adjustments to our actual our inventory button we're going
to come over into our inventory button.tscn here and I'm just going to make a quick small
adjustment here so first things first I'm going to make my inventory button just a little
bit larger so I'll pull this guy down like that and then I'm going to come into my theme
overrides I'm going to change my style for norm
al to be style box empty I'm going to right
click add any child node and add an a panel and that's going to give me that background I'm
looking for now the reason why I'm doing it this way versus doing it any other way is because if I
make my inventory button this long then the user can click anywhere in this area and it will fire
off that click of it whereas if I choose to have the button up here then the user couldn't click on
the name and have it actually affect the label and we're also
going to need to grab this label here
and drag it up so that way it's in the correct spot I'm going to duplicate it and I'm going to
pull it down below and I'm going to put test name as the name I'm going to come in here and look for
these new justification flags are new I'm going to look for auto wrap and I'm going to do word and
that should work looks like they fixed the bug I ran into earlier in the previous version of this
tutorial for the GD script part where it would auto wrap and the
word would go above now it goes
below which is perfect that's exactly what we want now I'm going to change the horizontal alignment
to left so that way it's nice and left aligned and then I'm going to come down here into my theme
overrides font sizes and I'm going to change this to 10. that's going to make it nice and small
and properly sized now once we have this we could just hit Ctrl s go into our inventory button here
scroll down here and find where we're doing this section here and I'
m just going to say duplicate
this change this from quantity label to name label and I'm going to change this to Label 2 or I can
just change this to name label and call it a day I think that would be a better way to keep our stuff
organized instead and then we'll come up here to the top I'm going to grab that label duplicate
it and change the quantity label to name label that way we actually have our stuff now I need to
duplicate this guy I need to duplicate this guy and I basically need t
o change this to name label
name label and I can change this to item dot name and that will set up our name for our inventory
button now something that we can look at is here we have a section that says item quantity
to string now this is how much we're going to craft so it's up to us on if we want to
keep that or if we don't want to keep that I'm leaning towards keeping it but it's up
to you guys if you guys don't want to keep it all you have to do is just throw an if
statement and then j
ust check the inventory button type so you can basically just change
this say if current button type is equal to crafting then don't show this or set this to empty
and it won't show up anyway now add item add item and click key and there's our key and we can now
use that item now all we have to do is set up our on Hover menu so to do that that's going to be
pretty quick hopefully so what we'll do is we'll right click our inventory we'll add an A Child
node we're going to add in a control no
de like so we'll make it about I don't know about this
big give or take because I don't quite know how big I want this we're going to right click
add another child node We'll add in a panel we'll make that a full rect panel we're going to
right click add another child node adding a label and we will call this header label like
so we're going to Center align it so we'll go to horizontal alignment center and we'll
drag this out so that it matches if we want to we can actually just do that and
that will
make it be right at the top we'll call this item name like so and then we'll duplicate this
we'll drag this guy down and we will type wait like so we're gonna line this to the left and
then we will pull this guy over we're gonna duplicate that drag it over like this and we're
gonna align this to the right like that we'll set this to something like zero and then we're
gonna change this to weight label and weight value and then all we have to do is create what our
components are g
oing to be so I'm going to right click add any child node and we're going to add in
a a v box container we're gonna pull this guy down something like right here we'll make this about
this big give or take and we're going to need to add another label so we'll duplicate this guy
we'll expand it out and we'll type components like that and then we're going to Center align this now
I'm going to control a on this and type components label and then I'm going to grab this guy I'm
gonna right click
added an H box container I'm going to right click add another child
node and I'm going to add in a label and I'm going to duplicate that label and I'm
just going to throw some test text in here and I'm going to throw a number I'm going to click
on this label and I am going to make it so that it expands all the way across to the other end and
basically to do that we just got to go and lay out container sizing and expand and that will go ahead
and expand that now our container can probably be
buffered just a tiny bit give us a little bit more
space there and what this is going to be is it's going to be a component object so I'm going
to double click this Ctrl a and add this as a component list object I'm going to right click
attach a script and I'm going to call it component list object and I'll get rid of these guys because
I don't need them what we'll do is we'll just basically say public void set up list object
and I will pass in a string name comma string and amount like th
at and we're going to rename
this label as name and we'll rename this label as amount easy enough now all I have to do is
come in here and say get node label quote name dot text is equal to name and then I can
basically just copy and paste this and say amount like that and then I'm just going to
cast this as a string so I'll say amount dot to string and there we go easy enough
so that's basically how we're going to set up this component object now we got to set up
our actual control node s
o we'll call this a craftable menu pop-up we're going to right
click attach a script we'll call it craftable menu pop-up that's fine and we'll get rid of
these guys because it's all completely not needed so what we'll do is first we need to get a
reference to our component component list object so I'm going to right click my component list
object I'm going to save that Branch as a scene I'm going to save it as a component list object
and then I am going to delete it now once we have that de
leted all we have to do is basically get
a reference to that object so I'm going to export and I'm going to say public packed scene opponent list object and then I'm
going to set up a function to actually um set up this uh pop-up
so I'll say public void set up menu and I'm going to pass in some things
so what I need to do is pass in all of the information that I need for an item so at
that point I might as well just pass in an item right because it's going to have all
that information alre
ady so I'll just pass in an item item and then I'm going to put get node
label item name dot text is equal to item.name I'm going to duplicate that a few times
and I'm going to come in here and do the same thing for weight value and I'm
going to set this to item dot weight interesting so I actually don't have a weight
value this must be something that I missed in one of my tutorials so I guess what I'm
gonna do is I'm going to disable my weight label and my weight value and we'll have to
r
eturn to that at a later date I'll pull this guy up and I'll make my vbox container
kind of match so we'll pull this down pull this up and then what I'll do is I'll go
into my craftable menu I'm going to comment this guy out so what we'll do is we'll basically
just Loop through our components and then just print those out so we'll say for each bar item in
item Dot item craftable makeup and we'll call this craftable component in item.craftable makeup and first we'll have to actually instance
our component list object so we'll say component list object C is equal to component
list object Dot instantiate like that and you'll see it's going to get mad at us because we
haven't converted it over to something so what we can do is say as component list
object and that should handle that now all we have to do is just say C Dot setup list
object we need to pass in a name and an amount so in our case it'll be craftablecomponent
dot key comma craftablecomponent dot value now you see it's
getting mad at me because it
says hey your key is not the correct thing so we'll do key Dot pretty sure it's a resource so
we're going to need to cast this as an item so let's come down here and let's actually
cast that real quick so we'll say item component is equal to craftable
component Dot key as item and then just do component dot name and there we go and now all we have to do is
basically make this follow our Mouse so I'm going to drag this up into my mouse area 2D so
now it's there
instead much like our inventory button so now it'll just follow our Mouse around
as we expect and what I'll do is in my inventory we have a segment in here where we actually
hover over items and I believe that's under input so we can come down here and find it so
I think it's actually up so we'll take a look that's fine it actually looks like I do it under my
thing here so what I'm going to do here is on hover over button if hover
over button does not equal null if our hover over button wh
ich is what
type it is considered an inventory button so if hover over button Dot current
inventory button state is equal to inventory button inventory then run all of this code so all of
this stuff where we're doing our click and drag stuff just go ahead and run all of that because
we don't want to allow them to click and drag anyway because that can cause problems so we'll
do that and then we can just say else if and the reason why I'm doing it else if is because
that's going to give me
a little bit more flexibility for the future so we'll do that I'll
copy this paste it and say is craftable like that and if it is a craftable button I just
want to make it so that it gets my node of craftable menu pop-up and the location
is going to be Mouse area to D comma craftable menu pop-up and it's not going
to be a comma it's going to be a slash and we'll hit dot setup menu like that it says that's going to require us to do a
item so we'll say hover over button dot current inventory
item and now we're gonna
have to show it so we can either do that in the setup menu which
I'm leaning towards so if we go back to our craftable pop-up menu
we'll just go with this dot show that way we're not actually you know
messing around with that now we do need to make it so that if hover over
button is null then we need to actually hide that so we'll just grab this bit of
code here and then we need to look for if our hover over button is
null else if our hover over else we can just d
o this right here instead
of doing setup menu we'll just do height in theory that should just work so we'll hit play well not quite it looks like so let's see
what's going on so it looks like this is um so it looks like this crafting menu window is
actually just eating my events so what I'm going to do is I'm going to right click it I'm going
to click change type and I'm going to type in control and I'm going to make it into a
control node now you'll notice that that literally eliminated ev
erything but that's
okay because that's what I wanted so I'm going to pull this guy down I'm going to come
in here add any child node let's add in a label and we will make this go
all the way across the top I'll type crafting menu and Center align it
then I'm going to right click my crafting menu add in a button and I'm going to snap it
to the upper right hand corner I'm going to make this a little bit bigger like that and
I'm going to add an x button just like that finally I'm going to ri
ght click add in a
child node and I'm going to add in a panel and put this in the back and I'm
going to make it full rect like that we'll make sure that this guy is nice and set
up and then if we hit play what is that gonna do right it's going to be mad because this is
Windows so we'll type control click control s if we hit play we hover over this you'll
see that it pops up so that's good but you'll notice that we're not getting any components
so why is that well if we take a look at our s
tuff we're actually not setting this as a
child of our grid component so we actually need to do that for this to work so if we look
at our craftable menu pop-up we can say add get node as a v box container
we'll pass in our V box container Dot ADD child and we will add
our c just like that now we also need to make sure that we remove all the children
underneath our vbox container to make sure that we don't have additional ones so for
each VAR item in get node vbox container quote vbox cont
ainer dot get children like that
and instead of item we'll just call it child .q free and that's going to eliminate those
guys out as well so now we can hit refresh we need to make sure that we have V box
container in here not box container I don't know how I missed that and if we
hit play or I guess refresh in our case there we go if we click add and add and then we
click on the key you'll see nothing happens but if we go down here and we click you'll see that
it works so what's going on
well if you come back into here and you look inside your craftable menu
pop-up it's actually exactly on our center of our point and that can overlap and cause a problem so
if we just drag this guy off by about a few pixels that should fix it so if we refresh it you'll see
we have the object following us but if I go over to the key and I click you'll see now that event
is getting respected so I can add two items I can click and drag my iron click and drag my gunpowder
click on my key and I g
et my key and that's pretty much it so I know that it's been a long tutorial
it's probably been like an hour and 20 minutes but that's pretty much how we make a crafting system
inside of Godot so if you like this video go and hit that like button hey you know if you dislike
this video go and hit that dislike button because I'm here to make content for you guys this video
was a viewer suggested video so if you have any suggestions please let me know in the comments
below or you can jump on m
y GitHub and I'll be more than happy to take your request under my
issues for fine point CGI suggestions next up on this series is going to be a pause menu and
then hopefully we'll be able to get the enemy to kill us and then we can start either wrapping
this up or if there's any additional features you guys are looking for you know I'd love to know so
that way we can accommodate whatever it is that you guys think would be cool for this and hey if
you have any questions or comments please h
it me up in the comments below or jump on my Discord
links in the description and I'll be more than happy to help you out with any issues you might be
having but that is all I have for you guys today so thank you so much again for watching and I
will see you all next time thanks [Music] foreign [Music]
Comments
👌👌
Hello guys!
You should reduce the return in "CanCraftItem" to just "return (counOfAffordedItems <= ItemCraftableMakeup.Count);".