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"

Spawning a Pawn

Hi everyone

I am trying to write a class that will spawn a pawn and i failed.

I have a pawn blueprint, i want to choose that from editor without adding to level and make spawn volume spawn that pawn.

Well, this class spawns nothing. There is a problem with it.

Here is the header file of SpawnVolume

 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "PursuerPawn.h"
 #include "SpawnVolume.generated.h"
 
 UCLASS()
 class SHOOTBOMBERS_API ASpawnVolume : public AActor
 {
     GENERATED_BODY()
     
 public:    
     // Sets default values for this actor's properties
     ASpawnVolume();
 
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
     
     // Called every frame
     virtual void Tick( float DeltaSeconds ) override;
 
     // Box component that defines spawn area
     UPROPERTY(EditAnywhere, Category = Spawn)
     UBoxComponent* SpawnVolume;
 
     // Pawn to be spawned
     UPROPERTY(EditAnywhere, Category = Spawn)
     // Maybe i shouldnt choose UClass, but AActor doesnt give me what i want
     class UClass* SpawnActor;
 
     // Minimum time required to spawn, Needed for random spawn time
     UPROPERTY(EditAnywhere, Category = Spawn)
     float SpawnTimeRangeMin;
 
     // Maximum time required to spawn, Needed for random spawn time
     UPROPERTY(EditAnywhere, Category = Spawn)
     float SpawnTimeRangeMax;
 
 private:
     // Gives random spawn location
     FVector GetSpawnLocation() const;
 
     // Gives random spawn rotation
     FRotator GetSpawnRotation() const;
 
     // To keep lines shorter, returns origin of box component
     const FVector& GetBoxOrigin() const;
 
     // To keep lines shorter, returns extent of box component
     const FVector& GetBoxExtent() const;
 
     // Returns random spawn time between min and max
     float RandSpawnTime() const;
     
     // Time passed since last spawn
     float SpawnTimePassed;
 
     // Random spawn time decided
     float SpawnTimeRandom;
 
 };
 

and source file

 #include "ShootBombers.h"
 #include "SpawnVolume.h"
 #include "Kismet/GameplayStatics.h"
 #include "PursuerPawn.h"
 
 
 // Sets default values
 ASpawnVolume::ASpawnVolume()
 {
      // 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;
 
     // A dummy root component
     RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
     
     // Box component that defines spawn area
     SpawnVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnVolume"));
     // Dont ask, name says it
     SpawnVolume->AttachTo(RootComponent);
 
 }
 
 // Called when the game starts or when spawned
 void ASpawnVolume::BeginPlay()
 {
     Super::BeginPlay();
 
     // Set default values
     SpawnTimeRangeMin = 0.5f;
     SpawnTimeRangeMax = 1.5f;
     SpawnTimePassed = 0.f;
     SpawnTimeRandom = RandSpawnTime();
 
 }
 
 // Called every frame
 void ASpawnVolume::Tick( float DeltaTime )
 {
     // If you dont know what this line does, well...
     Super::Tick( DeltaTime );
 
     // If we have a SpawnActor and World
     if (SpawnActor || GetWorld())
     {
         // Add delta time to passed time
         SpawnTimePassed += DeltaTime;
 
         // If enough time passed, spawn object
         if (SpawnTimePassed >= SpawnTimeRandom)
         {
             // Substract random spawn time from passed time
             SpawnTimePassed -= SpawnTimeRandom;
             SpawnTimeRandom = RandSpawnTime();
             
             FActorSpawnParameters SpawnParameter;
             SpawnParameter.Owner = this;
             SpawnParameter.Instigator = Instigator;
 
             // Spawn the actor with random location and rotation
             AActor* const SpawnedActor = GetWorld()->SpawnActor<AActor>(SpawnActor->StaticClass(), GetSpawnLocation(), GetSpawnRotation(), SpawnParameter);
         }
     }
 }
 
 FVector ASpawnVolume::GetSpawnLocation() const
 {
     // Gets a random location inside Box component
     FVector RandomPoint;
 
     RandomPoint.X = FMath::FRandRange(GetBoxOrigin().X - GetBoxExtent().X / 2.f, GetBoxOrigin().X + GetBoxExtent().X / 2.f);
     RandomPoint.Y = FMath::FRandRange(GetBoxOrigin().Y - GetBoxExtent().Y / 2.f, GetBoxOrigin().Y + GetBoxExtent().Y / 2.f);
     RandomPoint.Z = FMath::FRandRange(GetBoxOrigin().Z - GetBoxExtent().Z / 2.f, GetBoxOrigin().Z + GetBoxExtent().Z / 2.f);
 
     return RandomPoint;
 }
 
 FRotator ASpawnVolume::GetSpawnRotation() const
 {
     // Gets a random rotation
     FRotator RandomRotation;
 
     RandomRotation.Pitch = FMath::FRand();
     RandomRotation.Yaw = FMath::FRand();
     RandomRotation.Roll = 0.f;
 
     return RandomRotation;
 }
 
 const FVector& ASpawnVolume::GetBoxOrigin() const
 {
     // To keep lines short, returns origin of box component
     return SpawnVolume->Bounds.Origin;
 }
 
 const FVector& ASpawnVolume::GetBoxExtent() const
 {
     // To keep lines short, returns extent of box component
     return SpawnVolume->Bounds.BoxExtent;
 }
 
 float ASpawnVolume::RandSpawnTime() const
 {
     // Returns a random spawn time between Min and Max
     return FMath::FRandRange(SpawnTimeRangeMin, SpawnTimeRangeMax);
 }


Product Version: Not Selected
Tags:
more ▼

asked Apr 12 '15 at 01:15 PM in C++ Programming

avatar image

Corpse0327
186 26 25 35

avatar image Maximus Jobs Apr 12 '15 at 02:33 PM

Question for you, why can't you use AActor* for SpawnActor. The current spawning you have setup via GetWorld()->SpawnActor won't support anything but actors. There are other functions to spawn things such as UObject.

avatar image Corpse0327 Apr 12 '15 at 02:38 PM

When i use AActor, it ask for an actor inside level. How can i get an actor not added level, exist in Content folder?

avatar image shadowmint Apr 12 '15 at 03:04 PM

You probably want:

 UBlueprintGeneratedClass *factory; 

This is the type that can take a 'blueprint' object; you don't need to use UObject *.

avatar image Corpse0327 Apr 12 '15 at 03:08 PM

Ohh, thanks. Will try it out right away

avatar image Maximus Jobs Apr 12 '15 at 05:36 PM

Sorry, was at a restaurant. What you want is a TSubclassOf SpawnActor. That will be the class type, NOT a level actor. Pointers always are for live objects(the "this" variable is active). You can use TSubclassOf to use the specific class within Unreal Engine.

avatar image Corpse0327 Apr 12 '15 at 02:47 PM

Maybe i am asking for something silly. But i used to use Unity and i migrated 2 weeks before UE4 became free. But that was how i did things inside Unity. So i may be trying to do something silly.

avatar image shadowmint Apr 12 '15 at 03:08 PM

I can't see anything obvious wrong with this...

Obviously be aware that even if you spawn a Pawn, the player will not control it; you must call Possess() on a PlayerController() to bind the Pawn to the PlayerController.

...but other than that. It seems more or less ok.

 AActor* const SpawnedActor = GetWorld()->SpawnActor(factory, GetSpawnLocation(), GetSpawnRotation(), SpawnParameter);

Should definitely spawn an actor.

What happens when you run it? Does a new actor show up in the editor?

Try adding some debug messages (eg: UKismetSystemLibrary::PrintString(this, "Hit Value", true, true, FLinearColor(0.0,0.0,0.0));) to check that SpawnActor is actually being called...

If it is, what is the return? Is SpawnedActor NULL? Or has it got a value?

avatar image Corpse0327 Apr 12 '15 at 04:03 PM

Actually, i am having a hard time implementing it. Cant use UBlueprintGeneratedClass with SpawnActor.

How can i use it with SpawnActor?

By the way, this is for spawning purposes. Will work on AI later. It is training.

avatar image Corpse0327 Apr 12 '15 at 06:05 PM

Ahhh, silly me. One wrong letter and i created an undefined type.

// This was causing headache to me :D

class UBlueprintGeneretedClass* ActorToBeSpawned;

Everything is going fine but BlueprintGeneratedClass.h gives lots of errors. Related to itself. All compiler errors are from BlueprintGeneratedClass.h

I have done nothing else but adding these lines.

What is it wrong that i did.

 #include "Engine/BlueprintGeneratedClass.h"
 
 ...
 ...
 // Pawn to be spawned
 UPROPERTY(EditAnywhere, Category = Spawn)
 UBlueprintGeneratedClass* ActorToBeSpawned;


alt text

untitled.png (130.6 kB)
avatar image Maximus Jobs Apr 12 '15 at 06:07 PM

As myself and exi said, try using TSubclassOf class AActor SpawnActor

avatar image Corpse0327 Apr 12 '15 at 06:14 PM

Okay will try it out

avatar image Corpse0327 Apr 12 '15 at 06:35 PM

Thanks for the help, it did worked

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

There 2 ways to get UClass from class, from static function which does not require instace, every UOBject has it and you can replace AMyPawn with any other UOBject class (But SpawnActor works only with AActors)

 AMyPawn::StaticClass()

Or from instance of existing object

 ActorPointerVarable->GetClass()
more ▼

answered Apr 12 '15 at 03:08 PM

avatar image

Shadowriver
34.6k 917 164 1095

avatar image Corpse0327 Apr 12 '15 at 04:05 PM

Yes you are right. But what i want is not to use an actor from level. I want it to be loaded from Content folder. And if i dont use UClass, i cant choose one from Content folder.

avatar image eXi Apr 12 '15 at 05:49 PM

You want to be able to set a BP that you have in your Content Folder as this SpawnActor variable?

Try

 TSubclassOf<class AActor> SpawnActor
avatar image Corpse0327 Apr 12 '15 at 06:13 PM

Thanks for the help

(comments are locked)
10|2000 characters needed characters left
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