Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

spawning actor/blueprint via c++

I'm trying to use the FObjectFinder to reference a blueprint. I reference the blueprint in the constructor then try to spawn blueprint/actor in the Tick function but the FObjectFind.object is null in the Tick even though it had a value in the constructor.


 #pragma once
 #include "CoreMinimal.h"
 #include "GameFramework/Actor.h"
 #include "Engine/World.h"
 #include "Public/Balloon.h"
 #include "CoreUObject/Public/UObject/ConstructorHelpers.h"
 #include "BalloonGenerator.generated.h"
 class WATERBALLOONS_API ABalloonGenerator : public AActor
     // Sets default values for this actor's properties
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
     // Called every frame
     virtual void Tick(float DeltaTime) override;
     // Functions
     const void DropBalloon();
     // Reference to self
     AActor* Owner = nullptr;
     // Balloon Reference
      UBlueprint* BalloonBP = nullptr;
     // Timer variable for dropping balloons
     float LastDropTime = 0.0f;
     // Random delay for ballon drops
     float RandomDelayTime = 6.0f;
     float MaxDelayBetweenSpawns = 6.0f; // In seconds
     // Max/min X and Y for random positioning
     // for balloon drops
     float MinPossibleX = 0.0f;
     float MaxPossibleX = 0.0f;
     float MinPossibleY = 0.0f;
     float MaxPossibleY = 0.0f;
     float ZHeightToDropBalloonFrom = 0.0f;


 #include "BalloonGenerator.h"
 // Sets default values
      // 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;
 // Called when the game starts or when spawned
 void ABalloonGenerator::BeginPlay()
     // Get Reference to Self
     Owner = GetOwner();
     if (!Owner)return;
     if (!BalloonBP) {
         UE_LOG(LogTemp, Error, TEXT("%s does not have a reference to Balloon blueprint."), *Owner->GetName());
     // Reference ballon object
     ConstructorHelpers::FObjectFinder<UBlueprint> BlueprintObj(TEXT("Blueprint'/Game/Balloon_BP.Balloon_BP_C'"));
     if (BlueprintObj.Succeeded()) {
         BalloonBP = BlueprintObj.Object;
         UE_LOG(LogTemp, Warning, TEXT("Balloon Blueprint did not get referenced."));
     // Start drop timer
     LastDropTime = GetWorld()->GetTimeSeconds();
     if (MaxPossibleX == 0 || MaxPossibleY == 0) {
         UE_LOG(LogTemp, Error, TEXT("Max random X and Y for balloon generator are not set"))
 // Called every frame
 void ABalloonGenerator::Tick(float DeltaTime)
     float CurrentTime = GetWorld()->GetTimeSeconds();
     if (CurrentTime - LastDropTime > RandomDelayTime) {
         // Drop a balloon
         LastDropTime = CurrentTime;
         RandomDelayTime = FMath::RandRange(MaxDelayBetweenSpawns / 3, MaxDelayBetweenSpawns);
 // Spawn a balloon into world
 const void ABalloonGenerator::DropBalloon() {
     // Determine x,y to spawn balloon
     float X = FMath::RandRange(MinPossibleX, MaxPossibleX);
     float Y = FMath::RandRange(MinPossibleY, MaxPossibleY);
     // Create creation vector
     FVector CreatePosition = FVector(X, Y, ZHeightToDropBalloonFrom);
     FRotator Rotation = FRotator::ZeroRotator;
     FActorSpawnParameters SpawnInfo;
         UE_LOG(LogTemp, Warning, TEXT("Balloon Blueprint is null - no generation."));
     UE_LOG(LogTemp, Warning, TEXT("Create Position: %s"), *((CreatePosition).ToString()));
     GetWorld()->SpawnActor<ABalloon>(BalloonBP->GetClass(), CreatePosition, Rotation, SpawnInfo);
Product Version: UE 4.22
more ▼

asked Apr 30 '19 at 01:46 PM in C++ Programming

avatar image

5 1 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Just use in the header:

 TSubclassOf<ABalloon> BalloonClassToSpawn;

and in the cpp:

 GetWorld()->SpawnActor<ABalloon>(BalloonClassToSpawn, CreatePosition, Rotation, SpawnInfo);

no need to make it hard on yourself with explicitly pointing to blueprints.

more ▼

answered Apr 30 '19 at 02:12 PM

avatar image

Lardo Deepdelver
951 6 4 6

avatar image spinland Apr 30 '19 at 02:26 PM

wow. so simple! thanks so much! it works.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question