♪♪♪ Wes Bunn: Hello everyone and welcome. In this video,
we are going to take a look at how to spawn physics objects inside Blueprint script. First, we need is a physics object to spawn. We are using the Third Person Blueprint template project,
but you could use any project you would like. I did include Starter Content so that
we have some meshes to play with. First, go to the Content Browser
and click the Add New button. Click Add New and select the Blueprint Class button. In the Pick Parent Class menu, select Actor. I am going to name this Actor "PhysicsObject". Double-click PhysicsObject to open it up. Here, we are going to specify
the object that we want to spawn. We are currently looking at
the Viewport inside the Blueprint Editor. The Construction Script and the Event Graph are graphs
we can use to script the functionality of our Blueprints. We are not going to worry about
those for this particular object. All we are going to do is create
something that we can spawn. Go to the Components panel and
click the Add Component button. We are going to add a Static Mesh.
Scroll down, and select Static Mesh. I am going to name this Static Mesh "Object".
That is fine. In the Details panel under Static Mesh, we have the option to choose which Static Mesh we want to assign to this Component.
Click the Static Mesh drop down. I have several Meshes to play with
because I included Starter Content. I am just going to choose one.
I am going to type "table". Select SM_Table. There is a table. That is the
object we are going to spawn. We are also going to make sure we choose
to Simulate Physics on our object. Click Compile and Save. Now we have a physics object.
If I click Play here, you should see that the table starts to fall.
There is no floor, so it is just going to continue to
fall off into space down there. Let's not do that. Click Compile and Save, and close this Blueprint. We are going to go into the Level Blueprint
to actually spawn our Blueprint here. Go to the Blueprints button on the Toolbar. Click the Blueprints button
and click Open Level Blueprint. Here is the Event Graph,
just like in our other Blueprint, where we can provide the
functionality for our Blueprints. Right-click and add a new node.
Let's add an input. Type "Input". Scroll up, and there are options for Mouse Events
and Keyboard Events that we can input. Actually, I probably should have just typed "F". Yes, that was easier. Under
Mouse Events, select F. Whenever I press F, execute
whatever is connected to this wire, which is going to spawn our Blueprint
that we just created a moment ago. Right-click, type and select,
"Spawn Actor from Class". Left-click + drag off Pressed and plug it into
the Spawn Actor from Class executable. Click the Select Class drop down menu,
type and select "PhysicsObject". I probably should have given it a better name
than PhysicsObject, but this will do. We are going to spawn our PhysicsObject. It is asking for a Spawn Transform,
which is the transform or position in world space
where this object will spawn. To find the transform,
we are going to cheat a little bit. Go back to the level by selecting
the ThirdPersonExampleMap tab. In this Modes panel, type and select "Target Point". A Target Point is really useful
for defining spawn locations. Left-click + drag a Target Point into the level. I am going to drag it up and place it
somewhere like that up in the sky. So now that we have a Target Point in our level, I am going to the Level Blueprint,
and I am going to minimize it really quickly. I don't want to minimize the whole thing.
I will just undock the Level Blueprint. There we go.
[Laughing] There are a couple ways you can do this. Probably the most common way is to Right-click, and select Create a Reference to Target Point because
we still have the Target Point selected in the level. Right-click and select
Create Reference to Target Point. Another way is in the World Outliner
with the Target Point selected. Left-click + drag Target Point
inside the Level Blueprint. I will maximize this window so you can see better.
With the reference to Target Point in our level, drag off, type and select, "Get Actor Transform". We are getting the transform of this Target Point and
using that as a location to spawn our PhysicsObject. Connect the Return Value to
Spawn Transform. Click Compile. I will undock this window and move it over.
Click Play in the level. Now when I press F, it should spawn our table.
There you go. It is a physics object. whole I could spawn several tables top of each other. Oh, look at that! There we go. So there you go. That is how
you can spawn a physics object. There is a handy thing I want to show you
really quickly. Press Esc and go back to the level. Let's say we want several different spawn points.
We don't always want to spawn at this same location. Hold Alt + drag out a copy of Target Point. Hold Alt + Left-click to drag out
a copy of Target Point. Select Target Point in the Modes panel again,
hold Alt + drag out another copy. Hold Ctrl + select each of the Target Points. Hold Alt + drag out a copy of all the Target Points. Now we have 6 potential locations
where our table could spawn. Go back to the Level Blueprint.
Let's move our F key Event over just a little bit to the left here because we are going
to do something cool in here. Right-click in the graph, type
and select "Get All Actors of Class". We are going to connect the Pressed to
the input of Get All Actors of Class node. Get All Actors from Class needs a class assigned.
Click the Select Class drop down, type and select, "Target Point"
because that is what we want to find. Once it has all of this data, Drag off the Out Actors pin,
which is all the Actors that it gets. We are going to get one of the Actors.
Drag off Out Actors, type and select "Get". Let's move the Target Point and
Get Actor Transform nodes down. Left-click + drag to highlight
the nodes and drag them down. We are going to get one of the Actor transforms.
The Get node is asking for an Index. The Get node is asking for which Actor transform to get. Left-click + drag off the Integer pin,
type and select "Random Integer in Range". The Random Integer in Range node has a Min and a Max. We know the Min is 0 because in programmer speak
an array, which is this checkerboard box, starts at index 0 instead of 1.
It is a 0 based system. Therefore, we know that Min is 0;
but we don't know the Max value. Well actually, we do. We could just say 6
because we placed 6. [Laughing] But we are going to do something even fancier.
Drag off of Out Actors again, type and select "Length".
Get the Length of our array. The Length node has an Integer pin.
Plug the Length result into Max. Delete the Target Point variable
that we were using before. Select the Target Point variable and click Delete. Connect the result of the Get node to the Target
of the Spawn Actor Transform. Make sure the Get All Actors of Class is
connected to the executable of Spawn Actor PhysicsObject. That looks good. Click Compile.
Go to the level and click Play. Now each time we press F, we are going to spawn
our table at a different location in our level. It is raining tables on us. There you go. You have learned how to spawn a physics object. Not only that, you have learned how
to spawn it at a random location in your level that you specify as spawn points. I hope you all enjoyed this video
and thank you for watching. ♪♪♪