x

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"

C++ code spawns actor twice

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 );
 
 }
 
 

 
Product Version: UE 4.8
Tags:
more ▼

asked Jun 23 '15 at 09:42 AM in C++ Programming

avatar image

Devrael
3 1 2 2

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

1 answer: sort voted first

Is this what you want?

PuzzleCrystal.h

 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "PuzzleCrystal.generated.h"
 
 UCLASS()
 class APuzzleCrystal : public AActor
 {
     GENERATED_BODY()
 
 public:
     APuzzleCrystal();
 
     // 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<AActor> WhatToSpawn;
 
     UPROPERTY(Category = Block, VisibleDefaultsOnly, BLueprintReadOnly, meta = (AllowPrivateAccess = "true"))
     TWeakObjectPtr<AActor> Crystal;
 
 private:
     UPROPERTY()
     class USceneComponent *Root;
 
     UPROPERTY()
     class UBillboardComponent *Billboard;
 };

PuzzleCrystal.cpp

 #include "PuzzleFromZiro.h"
 #include "PuzzleCrystal.h"
 
 #include "Components/BillboardComponent.h"
 
 // Sets default values
 APuzzleCrystal::APuzzleCrystal()
 {
     // 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/Blueprint/Red.Red"))
             , CrystalBlue(TEXT("/Game/Objects/Crystal/Blueprint/Blue.Blue"))
             , CrystalGreen(TEXT("/Game/Objects/Crystal/Blueprint/Green.Green"))
             , CrystalYellow(TEXT("/Game/Objects/Crystal/Blueprint/Yellow.Yellow"))
         {
         }
     };
 
 
     static FConstructorStatics ConstructorStatics;
 
     Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
     RootComponent = Root;
 
     Billboard = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
     Billboard->AttachTo(RootComponent);
 
     UBlueprint *CrystalType = nullptr;
     switch (rand() % 4)
     {
     case 0:CrystalType = ConstructorStatics.CrystalRed.Get();
         break;
     case 1:CrystalType = ConstructorStatics.CrystalBlue.Get();
         break;
     case 2:CrystalType = ConstructorStatics.CrystalGreen.Get();
         break;
     case 3:CrystalType = ConstructorStatics.CrystalYellow.Get();
         break;
     }
 
     if (CrystalType)
         WhatToSpawn = (UClass*)CrystalType->GeneratedClass;
 }
 
 // Called when the game starts or when spawned
 void APuzzleCrystal::BeginPlay()
 {
     Super::BeginPlay();
 
     if (WhatToSpawn != NULL)
     {
         FActorSpawnParameters SpawnParametrs;
         SpawnParametrs.Owner = this;
         SpawnParametrs.bNoFail = true;
         UWorld* const World = GetWorld();
         if (World)
         {
             Crystal = World->SpawnActor<AActor>(WhatToSpawn, GetActorLocation(), 
                 { 90.0, 0.0, -90.0 }, SpawnParametrs);
         }
     }
 }
 
 // Called every frame
 void APuzzleCrystal::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
 }
more ▼

answered Jun 23 '15 at 11:50 PM

avatar image

MiniTurtle
259 17 10 18

avatar image Devrael Jun 27 '15 at 10:40 AM

Thanks, that helps!

avatar image Devrael Jun 27 '15 at 12:29 PM

One more question. How can I spawn many objects. When I try create object of APuzzleCrystal class ue4 crushes. How can I spaw object of APuzzleCrystal class in location that I need?

(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