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"

Tutorial code for SpawnActor not working? Alternative?

This is concerning a tutorial on this wiki

Specifically, this part isn't working for me:

In the tutorial, these lines are added in a class declaration

 UPROPERTY(EditDefaultsOnly, Category=Projectile)
 TSubclassOf<class AFPSProjectile> ProjectileClass;


This is the tutorial function which instantiates the projectiles

 void AFPSCharacter::OnFire()
     {
         // try and fire a projectile
         if (ProjectileClass != NULL)
         {
             // Get the camera transform
             FVector CameraLoc;
             FRotator CameraRot;
             GetActorEyesViewPoint(CameraLoc, CameraRot);
             // MuzzleOffset is in camera space, so transform it to world space before offsetting from the camera to find the final muzzle position
             FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(MuzzleOffset);
             FRotator MuzzleRotation = CameraRot;
             MuzzleRotation.Pitch += 10.0f;          // skew the aim upwards a bit
             UWorld* const World = GetWorld();
             if (World)
             {
                 FActorSpawnParameters SpawnParams;
                 SpawnParams.Owner = this;
                 SpawnParams.Instigator = Instigator;
                 // spawn the projectile at the muzzle
                 AFPSProjectile* const Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
                 if (Projectile)
                 {
                     // find launch direction
                     FVector const LaunchDir = MuzzleRotation.Vector();
                     Projectile->InitVelocity(LaunchDir);
                 }
             }
         }
     }


I tried to adapt this a little bit and I wonder where I'm going wrong. When I try to instantiate, nothing happens. At all.

I wrote this:

 //.h file
 
 UPROPERTY(EditDefaultsOnly, Category = Weapon)
 TSubclassOf<class ARockWeapon> RockWeapon;
 
 //.cpp file
 std::vector<ARockWeapon*> rocks;
 void AHero::UseWeapon(){
     if (RockWeapon){
         
         
         FActorSpawnParameters params;
         params.Owner = this;
             parans.Instigator = Instigator;
         
         
         ARockWeapon* const someone = GetWorld()->SpawnActor<ARockWeapon>(ARockWeapon, this->GetActorLocation(), this->GetActorRotation(), params);
         
         
         rocks.emplace_back(someone);
     }
         
 }


I did reference the RockWeapon in the BP. There might be typo mistakes because I typed this from memory but that should be the gist of it. It compiled fine, the bindings worked, it's just that nothing happened. No errors messages or anything.

If the tutorial is outdated, how would I best go about instantiating projectiles? I eventually got it to work like this but I think I'm unnecessarily jumping through hoops:

 //.h file
 
 ARockWeapon* ptr;
 
 //.h file of projectile
 
 ARockWeapon* get_pointer() { return this; }
 
 //.cpp file
 
 void AHero::BeginPlay()
 {
     Super::BeginPlay();
     for (TActorIterator<ARockWeapon> ActorItr(GetWorld()); ActorItr; ++ActorItr)
     {
         ptr = ActorItr->get_pointer();
     }
     
 }
 
 void AHero::UseWeapon(){
     if (ptr != nullptr){
         
         
         FActorSpawnParameters params;
         
         params.Template = ptr;
         
         ARockWeapon* someone = GetWorld()->SpawnActor<ARockWeapon>(ptr->GetClass(), params);
         someone->SetActorLocation(this->GetActorLocation());
         someone->SetActorRotation(this->GetActorRotation());
         
         rocks.emplace_back(someone);
     }
         
 }

I would then put one projectile into the scene so I could get a pointer through the iterator.

Product Version: Not Selected
Tags:
more ▼

asked Feb 27 '15 at 06:13 AM in C++ Programming

avatar image

TWill
1 1 1 1

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

1 answer: sort voted first

Sorry, this can be marked as answered. Here's how it works -> https://answers.unrealengine.com/questions/60897/spawn-actorobject-from-code.html

more ▼

answered Feb 27 '15 at 04:53 PM

avatar image

TWill
1 1 1 1

(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