Hello and welcome.
In this lecture, we're going to add some click behavior to our UI buttons so we can actually select
what action we want.
Okay, so in the previous lecture, we already created these UI buttons.
They're are created dynamically depending on the actions present on the selected unit.
Well that's missing.
Is that clicking on them doesn't do anything.
So let's handle that now.
Now for these, let's inspect the prefab.
So let's go inside.
It's going to see in view.
And for this one we created regular buttons.
So these are using the regular built in button UI class.
Now on these buttons over here you have the on click where you can just add a bunch of events.
However, in this case we're dynamically creating the buttons, so we need to set this up through code
and not through here.
So let's go into our action button.
UI So this is the script attached to each individual button on this one.
We are receiving the base action now here.
We already have the button reference so we can access the button and inside go inside on click.
And the way you work with this is you just call add listener.
Then here, note how this receives a delegate, the type, the action we've already learned about delegate.
So this is pretty simple.
It's just a normal action delegate.
So returns void and has no parameters.
And here is one more thing related to delegates, which are anonymous functions.
As we learned before, we can make a function, so a standard function.
So for example, let's go private void, move action button click.
We can do this and we use this function just like this.
So this is pretty much exactly what you used on the unit action system over here.
When we pass in the clear busy, this is what we did.
So we defined the function and pass it in.
So this is one approach.
However, just for learning purposes, let's learn about anonymous functions which can make it so we
don't have to always make a separate declaration.
And the way you write an anonymous function is very easy.
You start off by adding parentheses to all of the parameters.
Now in this case, we don't want any parameters, so just open and close parentheses.
Then we do a little arrow with equals and angle and bracket.
And finally here we can open up a code block with our currently braces.
So the same as usual.
And now here we can write whatever code we want.
So what we have written here is pretty much exactly the same as this.
So this is exactly the same as this.
We've got no parameters.
No parameters.
Then inside we have a code block, and in there we do whatever we want.
This is an anonymous function or another word for it.
It's a lambda expression.
We've actually already seen this once before.
So if we go into the level grid over here for the passthrough functions, we're already using lambda
expression.
So like I said, since this is a teaching course, let's go with this new approach to be able to get
used to it.
So we're defining our anonymous function.
So when we click, we're going to run this code.
Now, when we click on this button, we want to tell the detection system to set the selected action
to this base action.
In order to make that, let's go into the unit action system.
And right now, the only thing that we have is a selected unit.
Let's also add a field for the selected action.
So of type based action, call it the selected action.
Now let's go to where we are setting our selected unit.
So over here we have our function sets, unlike the unit.
So it sets that one.
And by default, let's like the move action.
So we set the select an action equals go into this unit and get the move action.
So by default when we change the selected unit, it's going to default to the MOVE action.
Based on our design, we're always going to make sure that every unit has a move action.
And finally, in order to make sure this always works, we're already calling it here when we manually
select the unit, but we also need to call it up here before we start.
So let's make a private void start.
And in here let's call that function or to run that code.
So set the selected unit and let's set the same one.
So pretty much just recently in the default unit.
Okay, so that's good.
No, let's make a function to set just sound like an action.
So let's go down here after the set, select the unit.
Let's do one.
Let's make it public void.
Set the selected action and we're going to receive a baz action.
And here we just set the selected action equals this base action.
And also we can clean up our code a little bit.
Just make sure we only assign selected action once.
So up here, instead of sending that film directly, let's just call the function.
So we set this, like, unit sets in action, then fire off the event.
All right, so now in the UI script, we can access this function.
So let's go back into our button UI.
And in here, let's access the unit action system.
X is the static instance, then set the selected action to this base action.
Okay, so far so good.
So the UI button now has this click logic.
Now let's go back into our unit action system script.
So up here, this is where we're handling our input.
So we've got our update.
Inside the update we're testing for the leftmost button and the rightmost button.
Like I've mentioned previously, now is the time to refactor this into some proper code.
Let's make a function to handle this connection.
So private void and only select an action.
And in here let's test for most click.
That's how we're going to activate each action.
So input get mouse button down on the left mouse button.
When we do.
Then here we want to activate our select an action.
We want to take this action.
So here we have another code architecture question.
Right now, each action has its own function.
So if we look into the move action, the function to triggering move is over here.
It's called the move.
Then for the spin action, this one is called spin.
So each action has a different function name.
Now the reason why we did this, as opposed to a general take action function, is because different
actions have different parameters.
So the spin really only requires the on action complete delegate, whereas the move requires no one
but also the grid position.
So now the question is, is this the better approach or would it be better to refactor this code to
make a general take action function?
Since this is a teaching course, let me actually teach both methods.
If we go with this approach of using a different function for each method, if we do that, then let's
go into the inspection system and over here, after testing for Click, we have our Select an action.
Now what we need is to know which type this selected action is.
We need to know if it's a move or a spin action in order to call the right function.
So here we can use a really awesome feature from C sharp seven.
We can do a switch on the selected action and then over here we can add cases for each action type so
we can add case move, action, move, action.
Then here, do move, action, dot move somewhere and then do another case, case, spin, action,
spin, action.
And here we do.
Spin, action, not spin.
So what this switch is doing is it's basically doing a switch on the type of selected action.
So it tries to see if this type is of type move action.
If so, then runs this case.
If not, it has if it's a spin action.
If so, it runs this case.
So this is one approach.
Let's use the proper parameter so we can test and see if this does work.
So first of all, we're going to need the mouse.
Great position.
So let's get this one.
Then we just need to copy what we were doing previously.
So over here on the move, we test for this and we move.
And on the spin we do this.
So spin action, not spin.
Passing the clear busy the same thing and appear.
Instead of going through the selected unit to get that one, we can use the MOVE action.
So going to move action check if it's a valid great position.
If so, do that cone move with this.
In this we refactored the code and everything is correct.
There's no errors anywhere.
We just need to call this function up here and let's get rid of this other if and over here we see another
code architecture question.
So on the handle select action inside the function we're testing for the mouse button down.
But on the try handling and selection, we're testing for that one outside of it now both these work.
So the more important thing is just that we are consistent.
So if we are going with this approach of testing for the input inside the function, then let's apply
that also to this other function.
So here we've got the function that we built quite a while ago.
So before we run this, let's test if the input mouse button, if we have the button click, then we
do this.
If not, then we don't.
And now if we go back into the update, then we can just call these like this.
And also to avoid any errors, let's make this a proper code log instead of an inline.
If it doesn't change anything.
So just to make the code a bit more clear.
So we try handling and selection, which in turn tests if the mouse button is down.
If so, then test if it's clicking on top of a unit and then we have the handling selection.
So this one also tests if the mouse button is down.
If so, then we do the same thing we're doing previously.
So it checks for this connection.
If it's a move action check if it is on a valid position.
If so, set, busy and move.
Or if not, it has.
If this action is in steady spin action.
If so, then just doesn't.
So it just sets busy and starts spinning.
So let's see if everything is working.
So here we are in by default and as the move action select.
And if I click somewhere?
Yep.
There you go.
It does move.
So it did take the move action.
Now if I click on this other button, it's not like the spin action.
So I click on it.
Although do make sure that when you click on the spin button there is no valid grid position under the
button.
We're going to see why in a bit.
And now if I click anywhere in the field, I click and I go, it does take a spin action.
All right.
Awesome.
So like I mentioned, this is how you would do it if you wanted to make a unique function for each action
type.
But like I said, since this is a teaching course, let me show you both methods.
So let's refactor our code to use a general take action method in the next lecture.