top of page

Spawning Actors, Projectile Handling, TSubclassOf<>

  • Writer: James Burke
    James Burke
  • Sep 18, 2020
  • 2 min read

This is a fundamental skill, and a very basic one to know. Here's a concise tutorial, including projectiles as spawned actors as an extra.


Start with the header file of the class which will be responsible for spawning this actor. In my example, this projectile will be a bullet which will be shot from a tank. A turret also must fire these projectiles. These pawns share a base class, from which the functionality for shooting the projectile will be implemented.


In the aforementioned header files make a forward declaration for your projectile base class, assuming you have already created and configured this. (Line 10)



Now, under the private access modifier, make sure that you have defined a scene component - this will function as a set of coordinates that will be the spawn point for the projectile. This component is suitable as it has no rendering or collision capabilities and simply acts as a coordinate.


You will also need to make a declaration based on the projectile class that you just made a forward declaration to. Use 'TSubclassOf' with an argument of the projectile class. This makes it so that when you set the projectile in the editor, you won't be able to simply pick anything. Rather, you will only be allowed to pick anything which is based on this projectile class specified in the argument. This is the purpose of 'TSubclassOf'.


Then, in the cpp file make sure to include the path for this projectile class that we have been referring to. This will be the location of the class, i.e. wherever you put it when you created the class.


In the constructer, of course make sure to initialise the spawn point of the projectile.


Now, within the specific function responsible for shooting the projectile:

Line 44 (and 45) is the key here to spawning an actor, and in this specific example a projectile. We set the owner of the projectile to be able to identify the source of the projectile, which will be helpful in the future when implementing damage.





Recent Posts

See All

Comments


© 2023 by Name of Site. Proudly created with Wix.com

bottom of page