Hi!
I’m new to Unreal and I think I don’t understand how null pointers work.
In my lvl I’ve got a door that’s opening and I want to add a sound to it. I made all needed declarations in .h file:
If I do it with BeginPlay() on the start of the game, it does well and find my sound. but when I do it with TickComponent(), to check each frame if the door is close/open I get AudioComponent == nullptr.
So basically the question is why when I start the game I find the sound but I can’t find it in each frame?
You have declared an audio component pointer and told it to point to nothing.
Hence, null pointer (nullptr).
You have to create an UAudioComponent object for the variable to point at.
This can be in numerous places, but is often done inside the class constructor.
#include "MyDoor.h"
#include "Components/AudioComponent.h"
// Sets default values
AMyDoor::AMyDoor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
...
AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("OpenDoorAudio"));
AudioComponent->bAutoActivate = false;
}
void AMyDoor::PlayOpenDoorSound()
{
if (AudioComponent)
{
AudioComponent->Play();
}
}
...
Create a BP with AMyDoor as the parent, set the sound and call PlayOpenDoorSound() in BP when you need it.
Could you give us a little more info on what you doing?