i’m new in unreal engine. I have an problem that i modified the soundbase property in blueprint but in runtime it is null, i really appreciate that someone could help me solve this problem.
here’s evidence image and my scripts
Rifle.h
#include "CoreMinimal.h"
#include "Weapons.h"
#include "Rifle.generated.h"
class USoundBase;
/**
*
*/
UCLASS()
class ARifle : public AWeapons
{
GENERATED_BODY()
private:
ARifle();
USkeletalMesh* _rifleMesh;
void SetRifleMesh();
/** Location on gun mesh where projectiles should spawn. */
UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
USceneComponent* FP_MuzzleLocation;
public:
void FireBullet(FRotator SpawnRotation);
/** Gun muzzle's offset from the characters location */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
FVector GunOffset;
/** Projectile class to spawn */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
TSubclassOf<class ABullet> ProjectileClass;
/** Sound to play each time we fire */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay, meta = (AllowPrivateAccess = "true"))
USoundBase* FireSound;
};
Rifle.cpp
#include "Rifle.h"
#include "Components/SkeletalMeshComponent.h"
#include "Bullet.h"
#include "ShootingPlayerController.h"
#include "Kismet/GameplayStatics.h"
ARifle::ARifle()
{
_rifleMesh = NULL;
SetRifleMesh();
}
void ARifle::SetRifleMesh()
{
static ConstructorHelpers::FObjectFinder<USkeletalMesh> rifle(TEXT("SkeletalMesh'/Game/PrototypeWeap/Prototype_AssaultRifle.Prototype_AssaultRifle'"));
_rifleMesh = rifle.Object;
_weaponMesh->SetSkeletalMesh(_rifleMesh);
FP_MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("MuzzleLocation"));
FP_MuzzleLocation->SetupAttachment(_weaponMesh);
FP_MuzzleLocation->SetRelativeLocation(FVector(0.2f, 48.4f, 12.6f));
// Default offset from the character location for projectiles to spawn
GunOffset = FVector(100.0f, 0.0f, 10.0f);
static ConstructorHelpers::FClassFinder<ABullet> projectileClass(TEXT("/Script/TopDownShooting.Bullet"));
ProjectileClass = projectileClass.Class;
}
void ARifle::FireBullet(FRotator SpawnRotation)
{
// try and fire a projectile
if (ProjectileClass != nullptr)
{
UWorld* const World = GetWorld();
if (World != nullptr)
{
// MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
const FVector SpawnLocation = FP_MuzzleLocation->GetComponentLocation();
//Set Spawn Collision Handling Override
FActorSpawnParameters ActorSpawnParams;
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
// spawn the projectile at the muzzle
ABullet* bullet = World->SpawnActor<ABullet>(ProjectileClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
if (bullet)
{
// Set the projectile's initial trajectory.
FVector LaunchDirection = SpawnRotation.Vector();
bullet->SetBulletFlyingDirection(LaunchDirection);
}
}
}
// try and play the sound if specified
if (FireSound != nullptr)
{
UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
}
}