Spawning a projectile

Hello,
I’ve searched for hours, but i can’t seem to find a tutorial to spawn an actor/projectile without blueprint,
so my question is:

How can i spawn an projectile in the world from my top-down character, without the usage of a blueprint.

Thanks!

The templates are helpful for this kind of thing. I just opened the FirstPerson C++ project template to get their spawning logic for you here.

Ultimately what you need to do to spawn a actor is the following:

GetWorld()->SpawnActor<ActorType>(UClass* class, FVector* SpawnLocation, FVector* SpawnRotation);

Thanks only now i have an other problem:

UPROPERTY(EditDefaultsOnly, Category=Projectile)
	TSubclassOf<class TDProjectile> ProjectileClass;

When i press F12 on TDProjectile, it wont take me to that class,
When i do that in the example it happpens.

Also when i build it i get an error:

Error	1	error code: OtherCompilationError (5)	D:\...\...\...\...\...\Intermediate\ProjectFiles\Error	Shoot_It
Error	2	error MSB3073: The command ""D:\Program Files\Epic Games\4.9\Engine\Build\BatchFiles\Build.bat" Shoot_ItEditor Win64 Development "D:\...\...\...\...\...\Shoot_It\Shoot_It.uproject" -rocket -waitmutex" exited with code -1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	38	5	Shoot_It

EDIT:

Bug is gone

Thanks for your help, only now my Class stays NULL, so it wont be shooting, But im further than before thanks!

Hey I just thought I would add something relevant to you.

To set your projectile class you would have to extend the class to a derived blueprint and set it there. Or alternatively you can get it in code like this.

static ConstructorHelpers::FClassFinder<TDProjectile> ProjectileClass(TEXT("Path'"));

The path can be retrieved like so:

59670-gettingpath.gif

I’ve found out i didnt used i had a Typo:

// ---------------------Wrong one --------------------------------------
UPROPERTY(EditDefaultsOnly, Category=Projectile)
		TSubclassOf<class Projectile> ProjectileClass;


// ---------------------Good one --------------------------------------
UPROPERTY(EditAnywhere, Category = Projectile)
		TSubclassOf<class AProjectile> ProjectileClass;

Look at the name of class,
(names are different then before, rolled back in previous version)

When you say that “Class stays NULL”, you mean the TSubClassOf is Null?

I’m having this problem.

But I’m using the Good one already

My ProjectileClass is always null

Well when i pressed F12 on AProjectile in this part: TSubclassOf (before i had the A) vs didnt load the class, after that it did. But in runtime i hadn’t a link to a class so ive made id:

UPROPERTY(EditAnywhere, Category = Projectile)
		TSubclassOf<class AProjectile> ProjectileClass;

When the MainCharacter is placed in the scene, on the right side you can set the projectile, if you need to spawn your character in game you might try the other solution.

The on Jakey113G posted. That solution was handy for me to define a material and static mesh, im also going to use that for the class so i don’t need to set the UPROPERTY everytime.

My projectile is already set, I did it by C++ code. In my Character.h I call the projectile using

TSubclassOf<class AProjectile> ProjectileClass;

In the Character.cpp I use an if statement to check if my ProjectileClass is true, but is not.

Thanks a lot security20 and Jakey113G, my projectile class wasn’t set in the engine like the image above.

Thats was a very noob mistake

Is there a way to set this class in C++ instead of doing in engine editor?

Yes, but in the H you dont call it already, only if you use UPROPERTY(EDITAnyWhere)

so you can choose in the engine the correct file.
See image,
I can choose between none and Projectile.
If i choose None it will be NULL because i’ve selected projectile, it isn’t null but has a value (Projectile)

Are you definitely setting it, it sounds like it isn’t being set?

Security20 was suggesting you add the actor and then set the projectile class from the details panel, however that won’t work unless you do what Security20 did which is change the UPROPERTY macro to enable editing from there. Alternatively you can convert your C++ class into a derived blueprint and access and set the class within the blueprint.

//Default in the first person shooter c++ template
UPROPERTY(EditDefaultsOnly, Category=Projectile)

//UPROPERTY Change security used to edit from the scene details menu
UPROPERTY(EditAnywhere, Category = Projectile)

That isn’t how you set your class in c++ to my knowledge, unless I am misunderstanding the code snapshot.

I think you are meant to use the ConstructorHelpers::FCLassFinder(path).

For example it would be like this:

//Header 
UPROPERTY(EditAnywhere, Category = Projectile)
TSubclassOf<class AProjectile> ProjectileClass;

//CPP Constructor
static ConstructorHelpers::FClassFinder<AProjectile> projectile (TEXT("Path'"));

ProjectileClass = projectile.Class;

Yep I posted it above, though the comment chain is so long it is easy to miss.

static ConstructorHelpers::FClassFinder<AProjectile> projectile (TEXT("Path'"));

Path is the reference to the asset within the editor:

59670-gettingpath.gif

Thanks again for your help. My problem was resolved

Sorry for any mistake I wrote.

Don’t be sorry for it, it is how people learn after all. I am glad you got your issue resolved along side Security20.