Blueprint/C++ interaction when spawning instances

I’ve created a C++ class that inherity from AStaticMeshActor (it’s meant to represent a building within the game world). I’ve created a new blueprint that inherits from this class.

When loading the game (which should be creating instances of the blueprint) I’m not seeing my breakpoint in my C++ constructor being hit.

What I really want is a way for C++ code to be called when creating an instance of the blueprint object. How do I do this?

Dear Neil,

If you review StaticMeshActor.h

you will find that there is a msg that SMA does not call post initialize components

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "StaticMeshActor.generated.h"

 * An instance of a StaticMesh in a level
 * Note that PostInitializeComponents() is not called for StaticMeshActors
UCLASS(placeable, hidecategories=(Input), ConversionRoot)
class ENGINE_API AStaticMeshActor : public AActor, public INavRelevantActorInterface

But you’re saying you have a breakpoint in the actual constructor itself, not postinit ?

My Solution

I spawn SMA’s quite extensively in my game

Since I do the spawning through code, I can therefore run my own custom Init() function on the new SMA

To spawn a BP, you have to have a BP in your .h file as a UClass that you set in the editor*

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category=BPClasses)
	UClass* YourBuildingBP;

and then in the code you do

AStaticMeshActor* NewBuilding = GetWorld()->SpawnActor(YourBuildingBP, etc);

#Where to Spawn From?

I spawn stuff from my player controller class, but you could do it anywhere

and even write a BlueprintCallable function to do all this in a blueprint event graph somewheres



Right, I saw that. :slight_smile:

But I’m trying to do the exact opposite - you’re spawning instances from C++, I want to spawn my C++ object (or at least call a function on construction!) of a BP object.Maybe it can’t be done - but it seems like it should be possible and I’m just missing something!

Thats actually reallly easy, thankfully!

just use the post begin play event in your BP of your SMA

by the way I tested the above as actually working :slight_smile:

please note it is your EventGraph for your BP of your SMA

This was a super-useful hint. Thanks!

Aha! So the trick is to take your C++ function and prepend it with:

UFUNCTION(BlueprintCallable, Category=Building)

Where, in this case, “Building” is the sub-heading that your function will appear under in the Blueprint editor.

For a better example let’s say that I have a function called MyDebugTest(). It might end up looking like this:

UFUNCTION(BlueprintCallable, Category=Building)
void MyDebugTest(FString pString);

Simple once you realize how BlueprintCallable works!


please click the check mark next to my answer above to indicate your situation is resolved so everyone knows :slight_smile: