Collisions & Spawning a Blueprint class from C++ code
In this post we learned about class inheritance. Another case in which this technique is used by Unreal Engine, is when we create a Blueprint class based on a C++ class. We did this for the map at the beginning, and now I want to do the same with both player and enemies starships.
There are a couple of reasons why I came to this decision:
1) I want to add new static meshes to the base one, i.e. a simplified shadow under the hull, particle systems for thrusters, etc. and it would be a pain to create them all from code at right positions.
2) I want to use Blueprint Overlap Event to manage collisions between ships, so I don't have to check them from code.
The problem here is that we can't simply grab the Blueprint class and drag it into the level, we need to programmatically spawn Actors from code; we don't have a reference to the BP class though, so how can we do it?
Well, first of all let's create Blueprint classes derived from our PlayerShipClass and EnemyShipClass:
In MyPlayerController.h let's add the following variables:
Here we're saying that we want to create two pointers to a SubClass of APlayerShipClass and AenemyClass. ANd that is exactly what our new Blueprint classes are.
In the MyController constructor, these lines will find out the Blueprint class by using its reference path:
Now, we can change the spawning function to use the Blueprint class instead of the base class:
You can check if Unreal is really spawning the Blueprint extended version of the ships by adding another StaticMesh to the Blueprint, i.e. a little sphere over the ship, and see if it's there when you launch the game.
A good side-effect of using BP class is that we don't need to spawn the Static Mesh of the ships from code anymore, we can simply click "Add Component" in the Blueprint class, select "Static Mesh" and assign it to the appropriate asset.
After we click on the new Static Mesh component, we need to check the "Generate Overlap Events" in the details panel, to be sure that the event will be triggered everytime it touches other actors.
Back in Visual Studio, I'm adding the following method to EnemyClass.h:
and its implementation it in EnemyClass.cpp:
This will simply print a debug line and destroy the enemy ship at every collision; I added an AActor* parameter that will hold the pointer to the colliding Actor, because later I will check if it's the player ship or something else.
To trigger the execution of this method, we can go to the Graph Editor of the Blueprint class, and chain it to the On Begin Overlap event.
This is the final result:
The enemy ship is correctly destroyed (no fancy effects yet), and the debug tab will show a line saying
Beside overlapping, Unreal Engine has also a collision management system. I didn't use it because it involves physic simulation, friction, gravity and a lot of calculations we don't really need for our simple turn-based game; Managing overlapping is a faster and simple way to achieve the result we need, which is detecting if two objects are on the same tile and react accordingly.
Thanks for reading and game you next time!
There are a couple of reasons why I came to this decision:
1) I want to add new static meshes to the base one, i.e. a simplified shadow under the hull, particle systems for thrusters, etc. and it would be a pain to create them all from code at right positions.
2) I want to use Blueprint Overlap Event to manage collisions between ships, so I don't have to check them from code.
The problem here is that we can't simply grab the Blueprint class and drag it into the level, we need to programmatically spawn Actors from code; we don't have a reference to the BP class though, so how can we do it?
Well, first of all let's create Blueprint classes derived from our PlayerShipClass and EnemyShipClass:
In MyPlayerController.h let's add the following variables:
Here we're saying that we want to create two pointers to a SubClass of APlayerShipClass and AenemyClass. ANd that is exactly what our new Blueprint classes are.
In the MyController constructor, these lines will find out the Blueprint class by using its reference path:
Now, we can change the spawning function to use the Blueprint class instead of the base class:
You can check if Unreal is really spawning the Blueprint extended version of the ships by adding another StaticMesh to the Blueprint, i.e. a little sphere over the ship, and see if it's there when you launch the game.
A good side-effect of using BP class is that we don't need to spawn the Static Mesh of the ships from code anymore, we can simply click "Add Component" in the Blueprint class, select "Static Mesh" and assign it to the appropriate asset.
After we click on the new Static Mesh component, we need to check the "Generate Overlap Events" in the details panel, to be sure that the event will be triggered everytime it touches other actors.
Back in Visual Studio, I'm adding the following method to EnemyClass.h:
and its implementation it in EnemyClass.cpp:
This will simply print a debug line and destroy the enemy ship at every collision; I added an AActor* parameter that will hold the pointer to the colliding Actor, because later I will check if it's the player ship or something else.
To trigger the execution of this method, we can go to the Graph Editor of the Blueprint class, and chain it to the On Begin Overlap event.
This is the final result:
The enemy ship is correctly destroyed (no fancy effects yet), and the debug tab will show a line saying
"LogTemp:Warning: HIT BY: MyplayerShipClassBP_C_0"
Beside overlapping, Unreal Engine has also a collision management system. I didn't use it because it involves physic simulation, friction, gravity and a lot of calculations we don't really need for our simple turn-based game; Managing overlapping is a faster and simple way to achieve the result we need, which is detecting if two objects are on the same tile and react accordingly.
Thanks for reading and game you next time!
Commenti
Posta un commento