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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Trouble spawning an actor

This is more of a "I don't understands templates or function templates" post, and I'm hoping someone can help me out with understand how they work.

I am trying to spawn my actor, and I have successfully done so by doing

 APawn* ResultPawn = (APawn*)GetWorld()->SpawnActor(APawn::StaticClass());
 
 

The issue is I want to spawn it at a certain location with a certain rotation, and this https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/Spawning/ says I need to use templates for that.

I gave it a shot by literally copying the last example: (.h)

 template< class T >
 T* SpawnActor
 (
     UClass* Class,
     FVector const& Location,
     FRotator const& Rotation,
     AActor* Owner=NULL,
     APawn* Instigator=NULL,
     bool bNoCollisionFail=false
 )
 {
     return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
 }

(.cpp):

 APawn* ResultPawn = SpawnActor(DefaultPawnClass, StartLocation, StartRotation, NULL, Instigator);
 

so this is my code:

(.h)

     template< class T >
     T* SpawnActor
     (
         UClass* Class,
         FVector const& Location,
         FRotator const& Rotation,
         AActor* Owner = NULL,
         APawn* Instigator = NULL,
         bool bNoCollisionFail = false
     )
     {
         return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
     }


(.cpp)

     const FVector startLocation = FVector(-8908.868164f, -12233.826172f, 3496.578613f);
     const FRotator startRotation = FRotator(0.f, 0.f, 0.f);
     
     ATower_Archer* ResultPawn = SpawnActor<ATower_Archer>(ATower_Archer::StaticClass(), startLocation, startRotation, NULL, Instigator);
 

I get these errors:

 [2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error e:\users\*\documents\unreal projects\towerdefenseproject\source\towerdefenseproject\MainPawn.h(124) : error C2661: 'UWorld::SpawnActor': no overloaded function takes 9 arguments
 [2016.08.01-19.10.40:490][259]CompilerResultsLog:Error: Error E:\Users\*\Documents\Unreal Projects\TowerDefenseProject\Source\TowerDefenseProject\MainPawn.cpp(48) : note: see reference to function template instantiation 'T *AMainPawn::SpawnActor<ATower_Archer>(UClass *,const FVector &,const FRotator &,AActor *,APawn *,bool)' being compiled

I followed the example exactly like it says and I can't quite figure it out.

Product Version: UE 4.12
Tags:
more ▼

asked Aug 01 '16 at 07:13 PM in C++ Programming

avatar image

Snowl0l
49 11 13 21

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

1 answer: sort voted first

You don't need to write the code for the template function - Unreal Engine has already provided that. You just need to call it. To call a template function, the class you want to use needs to go in the angle brackets. Like so:

 ATower_Archer* ResultPawn = GetWorld()->SpawnActor<ATower_Archer>(startLocation, startRotation);

This should spawn an ATower_Archer at the desired location and rotation. If you wanted to spawn another class, you'd just replace ATower_Archer with AAnother_Class.

 AAnother_Class* ResultPawn = GetWorld()->SpawnActor<AAnother_Class>(startLocation, startRotation);

You only need that line of code - delete the declaration and definition of SpawnActor in your own header and cpp files.

more ▼

answered Aug 01 '16 at 07:35 PM

avatar image

geoffrey1218
106 6 4 10

avatar image Snowl0l Aug 01 '16 at 07:40 PM

Oh. Thanks. Just tried that and it worked.

avatar image geoffrey1218 Aug 01 '16 at 07:49 PM

Great! Could you mark the answer as accepted? There's a checkmark under the upvote/downvote button.

(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