♪♪♪
Lauren Ridge: Welcome back.
In the last video, we set up the Pickup Mesh to represent our pickup within the level.
Let me show you how to compile within the editor, so you can see how that works.
We did the very basic setup, and we are going to switch back to the editor.
Click the Compile button, and the toast will pop up in the corner.
If you have an error during compilation for some reason, you will be able to
click on this to show the log and get some debug messages.
You will also be able to switch back to Visual Studio and debug it from there.
This is going to compile quickly.
It just finished.
Now, we can add our activity code to the pickup.
Go back to Visual Studio.
We want all pickups to have a state of whether or not it is active,
to have ways to report whether or not it is active,
and also to change it.
Let's start with our variable for whether or not it is active.
We are going to make this variable protected
which means we can change it in this code class,
another code class based on this one, or in a Blueprint.
If we wanted to create a Blueprint based on pickup and then
directly toggle the pickup behavior, we could do that.
[Refer to code onscreen]
This will be "True when the pickup can be used,
and false when pickup is deactivated".
Again, that is very straightforward. The variable is just a Boolean.
It will be true when the pickup can be used,
and false when the pickup can't be used.
We will go to the constructor where we set the default values.
[Refer to code onscreen]
"All pickups start active".
If you had a specific type of pickup where you wanted it to be
inactive until the character did something, you could make a
Blueprint or a code class based on this variable
and override it in the constructor or the Construction Script
because that variable is protected.
Again because this variable is protected, it can only be changed
by other types of pickups and not by the Character or the GameMode.
We would need to make functions for that.
Let's go ahead and do that now.
We will create a function to return if the variable is active or not.
Then, we will create a function to change whether or not the variable is active.
The function to return will return a Boolean. [Refer to code onscreen]
Booleans functions should ask a question that has a true or false answer.
Is it active?
Like we used the UPROPERTY() macro before, we are going to use
the UFUNCTION() macro on this function.
We are going to use the keyword BlueprintPure, which I will explain in a second.
[Refer to code onscreen]
The SetActive is not going to return anything. It will be a void function.
[Refer to code onscreen]
I will go ahead and set that up here.
[Refer to code onscreen] This is also going to be a UFunction.
This function is going to be BlueprintCallable. [Refer to code onscreen]
[Refer to code onscreen]
BlueprintPure and BlueprintCallable are both things defined in code,
but can be called from Blueprints.
Technically BlueprintPure functions can modify things, but in general they shouldn't.
BlueprintPure functions are run every time their output is used.
A BlueprintPure function that pops something off a stack that is called twice, either with
a loop or in two (2) separate nodes, will pop two (2) things off the stack.
On the other hand, BlueprintCallable functions only run when the input execution pin is hit.
BlueprintCallable functions save their results in the output pins.
BlueprintCallable functions can be used for modifying calls.
Expensive calculations that aren't expected to change frequently
are better in BlueprintCallable functions than BlueprintPure functions.
Fast calculations that will frequently change are better to make in BlueprintPure functions,
or by adding multiple Blueprint Callable nodes to your graph.
If you want to know more about using BlueprintPure versus BlueprintCallable functions,
we have lots of documentation on how to write APIs for Blueprints.
Definitely check that out.
While I am thinking of it, let's comment so when we come back in a week
we know what this function will do.
"Return whether or not the pickup is active".
"Allows other classes to safely change whether or not pickup is active."
Neither of these do anything right now.
Let's write the definition in the Pickup.cpp source file.
We have a Boolean for IsActive.
[Refer to code onscreen] It is very simple.
[Refer to code onscreen]
Setting it up with these public functions that manipulate
a protected variable keeps things code safe.
This is especially so if you have multiple people working on a project
and they may not understand the rule setup in code,
or if you have people trying to have different things interact together.
[Refer to code onscreen]
Our setter is very straightforward.
[Refer to code onscreen]
If you also wanted to print the new pickup state to the screen,
or change how the mesh looks depending on whether or not it is active,
you can handle all of that in here.
It is a little more complex than just changing bIsActive, so
you wouldn't want to change bIsActive without those associated pieces of code.
It looks like that is all ready.
Let's go back.
"Returns active state".
"Changes active state".
Save both of those.
Double check that everything is saved.
There is our Boolean, our getter and our setter.
Let's go back to the editor and compile.
Click Compile.
Okay, that just finished compiling.
Now, let's make a Blueprint based on our pickup.
In the Content Browser, Right-click on Pickup and select
Create Blueprint class based on Pickup.
I was going to save this in a Blueprints folder. Click Cancel.
Let's go make that new folder.
Right-click on the Content folder and select New Folder. Call this "Blueprints"
just to keep everything organized as we go forward.
In the Content Browser go to C++ Classes, and select BatteryCollector.
Right-click Pickup and select Create Blueprint class based on Pickup.
Save it in the Blueprints folder.
Call this "Pickup_BP".
Pickup Blueprint.
Click Create Blueprint Class.
This is the full Blueprint Editor.
I will save this and reopen it, so you can see something.
Now if I reopen Pickup_BP, this is a Data-Only Blueprint.
Because this won't have any full node behavior, I could leave it in this state.
There is our Static Mesh.
Set the Static Mesh to SM_Cube.
Click Compile and click Save.
Go to CollectionLevel. Drag-and-drop Pickup_BP into the world.
You can see it right there.
Click Play. Our new pickup is right there and ready for us to interact
with if it had any way to call that active changing function.
I do want to show you those two (2) nodes that we made.
I will go back to Pickup_BP to open the full Blueprint Editor so you can see this note.
This is a Data-Only Blueprint, so you only see the default values.
It does not have any script or Blueprint specific variables.
Click Open Full Blueprint Editor.
You can see some grayed out template nodes.
Right-click in the graph, and scroll down to the Pickup category.
Remember, we put the Pickup category on all of our nodes.
Expand Pickup and you see our Is Active function.
Right-click and search for "Pickup".
That didn't search as much as I wanted.
Type and select "Set Active".
Is Active is the BlueprintPure function, so it doesn't have any execution pins.
Set Active is the BlueprintCallable function and it does have execution pins.
It can be specifically called on Event BeginPlay, if we don't necessarily want them to be active.
Select Set Active and press Delete.
We don't want to set up any behavior.
I wanted you to see the difference between those two (2) nodes.
To wrap up, in these two (2) videos we made a very basic pickup in C++ and then
made a Blueprint to show you how to set variables defining code in the editor.
In the next video, we will make a Battery pickup class that inherits from the Base Pickup
and gives it a little more functionality.
Thanks.
♪♪♪