How to call a function to begin at the start of game outside of BeginPlay()

Hi,

I’m fairly new to C++ and UE4 so please bear with me.

If I’ve created an actor class and I want a message to be displayed on screen or in the output log at the start of the game I simply put


// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	
	Super::BeginPlay();
	
	// Just to indicate the game and game output log have started
	UE_LOG(LogTemp, Warning, TEXT("game started!"));
}

in my .cpp - This outputs the message to the output log correctly at the beginning of the game. Now my question is if I’ve declared another UFUNCTION() in my header like so:


UCLASS()
class MyActor_API MyActor : public AActor
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, Category = "TCPConnection")
	void sendPacket();

public:
	
	// Sets default values for this actor's properties
	AMyActor();

protected:

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

And change the .cpp to


void ACannonBallActor::sendPacket()
{
	UE_LOG(LogTemp, Warning, TEXT("game started!"));
}

// Called when the game starts or when spawned
void ACannonBallActor::BeginPlay()
{
	Super::BeginPlay();
{

How do I tell Unreal Engine that I want that message to be displayed at the start of the game even though it’s not in BeginPlay()? I know this is a trivial example but I’m just trying to get the basics down and have a thorough understanding.

Thanks in advance

To clarify: After the code is compiled, an instance of the Actor is dragged into the editor from the content browser.

Hi,

Sorry, but I’m not a 100% sure on what you want to achieve, but you could call your sendPacket function from BeginPlay?



void ACannonBallActor::sendPacket()
{
    UE_LOG(LogTemp, Warning, TEXT("game started!"));
}

// Called when the game starts or when spawned
void ACannonBallActor::BeginPlay()
{
    Super::BeginPlay();

    sendPacket();
{  


If you want to call your function before BeginPlay, take a look at this, it has a nice picture of the chain of functions called when an actor is initialized:
https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/ActorLifecycle/

Lays head on keyboard Ugh. I was trying to over complicate things when it really is that simple. Thanks! Also, that link is great - made some things a lot clearer.

No worries, it happens :slight_smile: I’m glad that I could help!