I’m trying to spawn a random of 4 bluprints wich content sprite. But when I start dragging there is a sprite under cursor, and when I release LMB there spawns another one in the same place, but a random other.
Here is the code
PuzzleCrystal.h:
#pragma once
#include "GameFramework/Actor.h"
#include "PuzzleCrystal.generated.h"
UCLASS()
class PUZZLEFROMZIRO_API APuzzleCrystal : public AActor
{
GENERATED_UCLASS_BODY()
public:
APuzzleCrystal(FVector location);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere, Category = General)
TSubclassOf<class AActor> WhatToSpawn;
UPROPERTY(Category = Block, VisibleDefaultsOnly, BLueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UBlueprint* Crystal;
};
And PuzzleCrystal.cpp:
#include "PuzzleFromZiro.h"
#include "PuzzleCrystal.h"
// Sets default values
APuzzleCrystal::APuzzleCrystal(FVector location)
{
// 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;
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<UBlueprint> CrystalRed;
ConstructorHelpers::FObjectFinderOptional<UBlueprint> CrystalBlue;
ConstructorHelpers::FObjectFinderOptional<UBlueprint> CrystalGreen;
ConstructorHelpers::FObjectFinderOptional<UBlueprint> CrystalYellow;
FConstructorStatics()
:CrystalRed(TEXT("/Game/Objects/Crystal/Blueprit/Red.Red"))
, CrystalBlue(TEXT("/Game/Objects/Crystal/Blueprit/Blue.Blue"))
, CrystalGreen(TEXT("/Game/Objects/Crystal/Blueprit/Green.Green"))
, CrystalYellow(TEXT("/Game/Objects/Crystal/Blueprit/Yellow.Yellow"))
{
}
};
static FConstructorStatics ConstructorStatics;
location = { 0.0, 0.0, 0.0 };
Crystal = CreateDefaultSubobject<UBlueprint>(TEXT("Crystal0"));
switch (rand() % 4)
{
case 0:Crystal = ConstructorStatics.CrystalRed.Get();
break;
case 1:Crystal = ConstructorStatics.CrystalBlue.Get();
break;
case 2:Crystal = ConstructorStatics.CrystalGreen.Get();
break;
case 3:Crystal = ConstructorStatics.CrystalYellow.Get();
break;
}
WhatToSpawn = (UClass*)Crystal->GeneratedClass;
if (WhatToSpawn != NULL)
{
FActorSpawnParameters SpawnParametrs;
SpawnParametrs.Owner = this;
SpawnParametrs.Instigator = Instigator;
SpawnParametrs.bAllowDuringConstructionScript = false;
SpawnParametrs.bNoFail = true;
UWorld* const World = GetWorld();
if (World)
{
AActor* SpawningObject = World->SpawnActor<AActor>(WhatToSpawn, location, { 90.0, 0.0, -90.0 }, SpawnParametrs);
}
}
}
// Called when the game starts or when spawned
void APuzzleCrystal::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APuzzleCrystal::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}