Welcome back my fellow game developers
to a brand new video,
a brand new long video.
But it is worth it because now when we run the game,
we pick up some coins.
We go here, okay, So we can restore health,
we can upgrade health or we can do something much cooler,
and that is by ourselves some weapon.
So we get a rifle and then we
can go and shoot it everywhere.
So let's not waste
any more time and let's get to buying weapons,
visas like Paxos, any case.
So time for the last item that we can buy,
which is the weapon.
So I'm going to go into the item sprite.
And by the way, what I did offscreen is that I
added a couple of animations and make
sure that the item sprite is on a different level.
So there you can see all of them rotate and
you can see the platform have
this little animation on it.
Pretty simple, pretty easy.
I'm not going to go too deep into that.
But what I am going to do is I'm going to
go in here and change these pride on the last one.
And let's go into the sprites.
Pickups.
Nope, not pickups.
Weapons weapon,
weapon weapon weapon pickup.
Select this one and let's just add the rifle for now.
There we go.
Can see it.
Let's make it a bit bigger.
Sorry for that.
We should somehow keep the aspect ratio.
Now what?
Let's just increase the scale by two,
by two, by 1.5, by 1.5.
Okay, great. So now when we run the game,
I believe this should be
spinning because it has the animator. There we go.
You can see that it's spins
around and wait for us to come and collected.
Now, the difference between
the two other items and
the weapon item is that we don't want to have,
for example, just an item 40 rifle.
We want to get some kind of random item or
random weapon when we want to buy a weapon.
So I'm going to go into
the item script and this error is bothering me a lot.
And in the shop item in here,
I'm going to create a couple of lines.
So first thing first,
I want to create a serialized field
of the weapons system.
And it's going to be an array of
the potential weapons to buy.
That is a long name for a variable.
And then I'm going to create a private and the weapon
system and the actual weapon, no weapon too.
By, There we go.
And finally, we are going to do two things.
The first thing is going to
get this right because we want
to change the sprite based on
the weapon that we are going to buy.
So private sprite, renderer.
And there we go. And the item sprite,
not the item sprite, the weapon sprite.
So weapon,
so because this is only going to be used for the weapon.
And then we are going to get the serialized field,
the text Mesh Pro or TMP Pro,
Tempe pro, yeah, shorthand P Pro.
And this is going to be
the price text we want to change the price based on,
okay, So we can't use this because we
need TMP Pro, sorry for that.
Or you know what we can do actually, we can,
instead of using TMP probe dot text Mesh pro Gu.
There we go.
So that way we don't need to add an entire library.
So this is just a novel way of doing things.
And now that we have a price for each of
our weapons and a sprite for each of our weapons.
Let's go into the weapons system.
And let's go ahead in here and create
a serialized field for the price of the weapons.
So int weapon price.
And we'll also create a serialized field for the sprite.
And this will be the weapon shop sprite.
And obviously all of these are serialized fields,
so we cannot access them.
So we need to create a couple of methods in here.
The first one will be a public and what and,
and get a weapon price.
And this will return the weapon price.
And then we will have a public sprite for right,
and get a weapon shop sprite.
And this will return,
return the weapon, right?
Weapon shop sprite. There we go.
So the weapon image and the weapons
Sprite are totally different things.
Maybe they shouldn't be,
maybe we can have just
one that returns the entire sprite.
But if you look in here,
I want to have the one with
the white little white thin line
around it so we can easily distinguish the weapon.
In what you can even change this right here, this one.
Let's not worry about that.
Let's keep going the way we are.
Those little small tricks can then at
the end be adjusted and we can change all of this.
So now let's go into the weapons.
Were are the weapons they are in the prefabs.
The residual, residual reserved,
the broken enemy weapons.
There we go.
So the rifle that
slot this down and let's see what we have in here.
So the weapon shop sprite and the weapon price,
how much does the rifle cost?
Let's say 10. Let's say the pistol costs 20.
Nope, sorry for that.
Now what?
Let's make the rifle costs 20.
Let's go back. The pistol will cost ten,
and the shotgun will cost 30.
And these are just arbitrary numbers.
I don't know whatever you want to use US.
Law.
Let's go set up the sprites.
So I'm going to make this one a bit bigger
so I can see the names in here faster.
And what do we have in here?
Pickups?
Nope. Every single time. Okay.
So add the shotgun sprite for the rifle.
Unlock this for the rifle.
I'm going to no, no, no, no.
This is a bit cumbersome, isn't it?
So anywhere at the rifle.
And finally, we select
the weapons and the Prefab, the pistol.
Weapons, pick up the PESTEL, we save that.
And now we have a reference to all of these. Great.
Now that we have the sprite,
we have the weapon price,
the next thing to do is to go and start and set
everything up as soon as the weapons thoughts.
So in the shop item in here, I'm going to save,
I'm going to create a start,
and I'm going to issue you a challenge.
So your challenge is to check type,
select random weapons, set the sprite, and the tax.
So and start check if we have a weapon type item,
you'll need to select a random gun
from potential guns or potential weapons.
And you'll need to set the item cost as the weapon price.
You'll need to set the sprite as the appropriate gun.
You'll need to satisfy texts also.
And I believe this is the last point.
So with that out of the way,
pause the video right now and go to the HL7.
Oh, okay, welcome back.
So in start, That's that's not what I meant.
If we have an item type that is equal
to the item type weapon.
Then in here we are going to set a couple of things.
First of all, we'll create
a random selected weapon
and it's going to be equal to random dot range.
The 0 and the potential.
What is this?
The potential weapons to buy dot length.
Then in here we are going to select
the weapon to buy and it's going to be
equal to the potential weapon to
buy on the position of the selected weapon.
Then we are going to go ahead and get the item cost,
which is going to be the weapon to buy,
that get weapon price.
There we go. And finally,
we are going to set this pride.
So how are we going to first of all get the sprite?
We are going to get component and children the sprite.
Or know what maybe this one in here we won't be needing.
But anyways, let's just skip it.
We'll go into the sprite renderer.
This is the component on
the children, why the children while?
Because on this item which has the actual script,
the item sprite is down here.
And because no other game objects, oh,
we do have another game object
which is the item platform.
This will be a tricky one.
So, you know what,
for the sake of having everything properly set up,
I'm going to make this a serialized field and save that.
And then go back, sorry.
And then go back and to every single one of those
and wait a second.
Weapons sprite renderer.
I was and I was confused.
I was confused there for a second,
sorry for that back in here.
So I'm going to go ahead and add the item sprite.
And here the sprite.
And in here also the item sprite.
So we don't confuse any of these.
And also, as long as we're here,
let's go ahead and add also the price text. So.
Okay, so for some reason I
forgot to change the name of these.
So this will be the item price text
for some reason I thought we did change the student.
Be no worries about that.
And these are the things that we have to do.
We don't like them,
but we have to do them at some point in time.
So let's go ahead and set everything up.
There we go. So now we have a reference to
all of these. Back in here.
The weapons sprite renderer.
The sprite is going to be
equal to the weapon to buy that,
get weapons shop sprite.
There we go.
Save that.
And the last thing that we need to
do is we need to set the text.
So we're going to get the price text.
The text. And what do we say in here?
Let's see what was originally written in here by weapon.
So instead of just by weapon,
we'll start off with by.
Then we are going to concatenate it with the weapon
to buy dot get weapon name.
And this is a string,
so we can do this legally.
And then we will say the price.
So we'll add these two plus the item cost, item cost.
And this is an integer.
So remember when we had an integer
previously in somewhere, I forgot.
So you remember because it was an integer,
we couldn't just sell it.
We had to say dot toString.
So and maybe I wasn't clear what this does.
So toString just converts
any integer or any float
into a string and you can use it.
But in here, because we are concatenating,
concatenating means we add a plus
between a string and an integer or a string and a float.
So when we concatenate a string with an item or sorry,
with an integer, then we don't need
to add two string immediately.
Visual Studio and C Sharp code understands that this,
although it's an integer in here,
we are using it as a string.
So we save that and we also need to add in here BTC.
Save that.
And now let's go back into
our game and see if this works.
We still haven't added the gun,
but let's see if this actually
works are random selection.
So let's go down here.
Okay, so a rifle.
There we go buy a weapon and it
did not work. Why is that?
Because we haven't added anything to the array.
So in here, we need to add the potential weapons to buy.
And it's a consistent so up here in
the prefabs, prefabs weapons,
select the potential weapons
and let's just have the shotgun and
the rifle because we are holding a pistol and our hand.
So run the game.
Let's go and see what happens.
So a rifle there we go.
Buy a rifle for 20 bitcoins.
Let's try this again to try
to get the shotgun, make sure it works.
There we go. The shotgun by the shotgun, 430 bit coins.
But now when we press E,
nothing happens because we still
haven't added any way of buying.
So back down here,
we're going to create a case.
And this case will be for the item type dot the weapon.
There we go. And now
in here we are going to first of all,
find the player that we want to add, the weapons too.
So in here I'm going to say that we
have a player controller,
which is the player Who's buying
the player, the player buying.
So player buying.
I really have a hard time
with selecting names for
objects of finite object of type.
And as you can see, we have
redundant code every single time.
Maybe we should change this.
We are doing a lot of finding.
I don't know, I think we
should change this at some point.
So instead of declare health handler,
we are going to get the player controller.
There we go. So now we have
a reference to the player controller.
Then in here we are going to say
that the gun that we have,
so we're going to instantiate the weapon that we have.
So the weapon to buy.
And we're going to instantiate on the player buying dot.
Get weapons are.
So just like we did when we were picking up a weapon.
So we instantiate the weapon on the guns arm.
And the last thing that we need to do,
we need to also said that weapon
on the players available gun.
So we are going to go ahead and get a reference to this.
Because if I remember correctly,
if we are buying
gun dot add weapon available, there you go.
You can see that takes in the weapon to add.
So we need to get a reference in here,
or you can actually copy this and put it in here.
That doesn't look very pretty,
that doesn't look very professional.
If you look back at the code,
you won't understand exactly what's happening.
So instead, we're going to
create a weapon system in here.
And it's the weapon to add an equal to instantiate.
And then in here we said the weapon to
add and do not forget to break.
This is something that we need and
just in case we are going to create the default in here.
This says that if none of these work then default debug,
thought log, and no item type was chosen.
And all.
I really like the switch cases. They are really nice.
If you look at them. They really look very nice.
So you have a case,
everything is really properly arranged.
So let's go back and see what happens in here.
In case it's a weapon,
we get the player controller,
a reference to the player controller.
We only have one, so this is no problem.
We then instantiate the weapon.
We set it in a variable,
and of course we set it on the player buying,
which has an army always want to set the child,
we set the parent of
the weapon that we've instantiated as the arm,
and then we get the player buying,
we add the available weapon.
Obviously we need to have,
as you remember in here,
we have the available weapons on the player.
We add that weapon,
we break and we have our Robins.
So back in again.
Let's see if that works.
Run the game.
And we pick up some coins to buy ourselves a shotgun.
So now we have a pistol, we get the shotgun.
How cool is that?
So now we have a shotgun,
and not only do we have in here,
I wish I did not use the maximize on
play and maximize, Let's just remove it.
So not only do we have the shotgun and
arms and it works and it shoots and it does everything.
But we also, if we go, Where is it?
Where is it on the DOM player? Unlock this one here.
There you go.
You can see that we have a shotgun and a pistol.
So with that out of the way,
we've finished creating all of our shopping items.
I hope you enjoy it.
I hope you like how the game is turning out to be.
Make sure to always commit your changes
and make sure to leave me a review if
you haven't already with that said,
I'll see you in the next video.