Download

After modified variable in blueprint but still get null at runtime

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());
    	}
    
    }

The image at the bottom isn’t uploaded properly.

You have your FireSound marked as “EditAnywhere”. Are you dragging your character + weapon into the map or spawning them from the blueprint at runtime? If you have your character dragged into the map, try to inspect that instance (not the blueprint itself, but the instance). Since FireSound is marked as “EditAnywhere”, you can actually edit FireSound for different instances. You might have it set to null for that instance.

Alternatively, edit FireSound to be “EditDefaultsOnly”. This way you can only edit FireSound in the blueprint, and all instances will have the same value as the blueprint.

So to clarify. In the editor, click on the ARifle you have dragged into the world and look at its properties on the right. Is FireSound set there?

sorry i updated the image.
My character and weapon are all spawning from c++ scripts, i’m not dragging them into the map.
Do i need to drag the blueprint created from class into the map to activate it?

No, you don’t. The way you’re doing it is fine. Can you check your output log and see if it’s giving any errors when you press play? If the engine can’t find your sound when the game is launched there should be an error message.