In this video, we're going to fix a situation where the player could drive straight over to the customer
and deliver the package, but they didn't actually ever pick up the package.
So we want to make sure you can only deliver the package if you actually have the package.
How do we do that?
Well, we're going to do that using a bull.
What is a bull?
I hear you ask.
Well, bulls are types of variables that can store one of two values, either true or false, and they're
often used with if statements to decide whether something happens or not.
So if something's true, do the thing.
If it's false, don't do the thing.
So bulls are very handy from that point of view.
And in our code we're going to jump back over into our delivery script and write up the top up above
our own collision.
Enter right up here.
We're going to create ourselves a variable which be of type bull and we'll call this has package.
You want to make sure that you name your bulls in a way that it's clear that it's either true or false
has packaged its true or has package is false.
If you just name this for example package, what does that mean?
Does it make sense?
Does it mean it's a big package or small package or red package or blue package?
But if we say has package, then that's do I have it or not?
And we do semicolon here now.
We didn't actually assign a value to it so we could assign it false to say we're pretty sure when the
player starts the game it's going to be false or we could have signed it as true.
When you start, you've got the package.
It's true.
But just a little experiment here.
If we don't assign it anything, what will it be?
How do we know?
Well, we could go and Google the answer what the bulls default for in unity or we can do ourselves.
Just a little quick experiment.
We'll start and start or delete this again in a moment and start and we'll debug dot log and in our
parentheses what is type has package and then semicolon.
So what that would do when we first start our game, it'll just print out whether it has packages,
true or false.
So we'll save that back over into unity.
We'll just do this as a little thought experiment.
I also want to get you in the habit of doing printing things to the console so you know what's going
on.
You're constantly testing and looking and not getting sideswiped by these things, so it starts off
as false.
So we know for sure this starts off as false.
That means that we won't be able to deliver the package until we actually go and get the package.
So how do we get the package?
Well, in our on trigger enter, we've got the if the other tag is package, then we want to pick it
up.
What would we do in this instance?
After we pick it up, we want to change our ball to be something we want to change it to be has package
equals what equals true.
So the logic here is if we bump into something that is the package now that has package ball or that
has package variable, we'll be true.
Excellent.
When we bump into the customer, we need a way of saying okay, the other tag has to be the customer
and you actually need to have the package, my friend, or else I'm not going to accept delivery.
The way we can do that is in our if statement.
We can have multiple things.
We're checking to see whether they're true or not.
So it can be saying, is it the customer?
And then we can use the double ampersand.
That's what that little symbol is called, ampersand or it's called the end sign.
So you do two and back to back that's the and operator and then we're going to type in simply has package
we don't need to say has package equals true you could do that but you don't need to because when you
just add in the variable in here, it's going to say, okay, it's a ball.
If it's true, do the thing.
If it's false, don't do the thing.
So let's save that and see if that actually works.
We'll do, first of all, by driving over our customer and see what message we get.
Make sure we don't accidentally pick up the package.
Drive, drive, drive, drive.
What happens here?
Nothing.
Nothing gets printed.
The customer is like, well, unless we bump into the house and it says, ouch.
Customer is like, No, man, I don't know what you're talking about.
We go over, we get the package now we've got the package picked up, drive back over to the customer
and yes, package delivered.
Excellent.
However, what's going on here?
Oh, I can keep delivering the package.
I'll deliver it another time and get, you know, suite, bonus and commission and another commission.
Okay.
So we need to do something so that we can't keep doing that.
We can't exploit that because, you know, your players absolutely exploit that.
So little challenge for you here.
I don't have a slide, but the challenge is add some logic to make sure that we can't deliver the package
over and over.
So what do you think we would do?
Pause the video and have a go at that?
So in logic we need to say once we've delivered the package, then we don't have the package anymore.
So has package is going to equal false and let's just check that that works.
Before we celebrate too much, click on play.
We'll go through this standard process.
We'll make sure that we can't deliver it until we have it.
No, we can't.
We'll grab it and then we'll bump into a tree, then we'll deliver it.
Yes.
Package delivered.
And then is it going up?
No, the number doesn't go up.
But if we go back over, we grab the package again and then drive back over.
It does go up exactly as we want it to do.
So there we go.
We've used a ball in conjunction with our if statement to make sure that we can only deliver the package
if we have the package.
Okay, I'll see you in the next lecture.