This is concerning a tutorial on this wiki
Specifically, this part isn’t working for me:
In the tutorial, these lines are added in a class declaration
UPROPERTY(EditDefaultsOnly, Category=Projectile)
TSubclassOf<class AFPSProjectile> ProjectileClass;
This is the tutorial function which instantiates the projectiles
void AFPSCharacter::OnFire()
{
// try and fire a projectile
if (ProjectileClass != NULL)
{
// Get the camera transform
FVector CameraLoc;
FRotator CameraRot;
GetActorEyesViewPoint(CameraLoc, CameraRot);
// MuzzleOffset is in camera space, so transform it to world space before offsetting from the camera to find the final muzzle position
FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(MuzzleOffset);
FRotator MuzzleRotation = CameraRot;
MuzzleRotation.Pitch += 10.0f; // skew the aim upwards a bit
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// spawn the projectile at the muzzle
AFPSProjectile* const Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile)
{
// find launch direction
FVector const LaunchDir = MuzzleRotation.Vector();
Projectile->InitVelocity(LaunchDir);
}
}
}
}
I tried to adapt this a little bit and I wonder where I’m going wrong. When I try to instantiate, nothing happens. At all.
I wrote this:
//.h file
UPROPERTY(EditDefaultsOnly, Category = Weapon)
TSubclassOf<class ARockWeapon> RockWeapon;
//.cpp file
std::vector<ARockWeapon*> rocks;
void AHero::UseWeapon(){
if (RockWeapon){
FActorSpawnParameters params;
params.Owner = this;
parans.Instigator = Instigator;
ARockWeapon* const someone = GetWorld()->SpawnActor<ARockWeapon>(ARockWeapon, this->GetActorLocation(), this->GetActorRotation(), params);
rocks.emplace_back(someone);
}
}
I did reference the RockWeapon in the BP. There might be typo mistakes because I typed this from memory but that should be the gist of it. It compiled fine, the bindings worked, it’s just that nothing happened. No errors messages or anything.
If the tutorial is outdated, how would I best go about instantiating projectiles? I eventually got it to work like this but I think I’m unnecessarily jumping through hoops:
//.h file
ARockWeapon* ptr;
//.h file of projectile
ARockWeapon* get_pointer() { return this; }
//.cpp file
void AHero::BeginPlay()
{
Super::BeginPlay();
for (TActorIterator<ARockWeapon> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
ptr = ActorItr->get_pointer();
}
}
void AHero::UseWeapon(){
if (ptr != nullptr){
FActorSpawnParameters params;
params.Template = ptr;
ARockWeapon* someone = GetWorld()->SpawnActor<ARockWeapon>(ptr->GetClass(), params);
someone->SetActorLocation(this->GetActorLocation());
someone->SetActorRotation(this->GetActorRotation());
rocks.emplace_back(someone);
}
}
I would then put one projectile into the scene so I could get a pointer through the iterator.