Announcement

Collapse
No announcement yet.

TArray of custom class..."not a valid template"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    TArray of custom class..."not a valid template"

    Cannot declare a TArray of type AIEnemy in AIHordeContainer. Not sure what I need to do.

    First..the class.
    Code:
    #pragma once
    
    #include "GameFramework/Character.h"
    #include "AIHordeHandler.h"
    #include "AIHordeContainer.h"
    #include "AIEnemy.generated.h"
    
    /**
     * 
     */
    UCLASS(abstract)
    class AAIEnemy : public ACharacter
    {
    	GENERATED_UCLASS_BODY()
    	
    	AAIHordeHandler * HordeHandler;
    	AAIHordeContainer * HordeContainer;
    
    	enum AIBehavior {WANDER};
    	enum AIState {NORMAL};
    	enum AIPersona {LONER, HORDE};	
    	AIBehavior behavior;
    	AIState state;
    	AIPersona persona;
    
    	AActor * DetectedTarget;
    
    	virtual void OnConstruction(const FTransform& Transform) OVERRIDE;
    	bool GetInHorde();
    	bool GetIsHorde();
    };
    Creates a Container to hold members of horde when Horde type Enemies interact.
    Code:
    void AAIHordeHandler::Tick(float DeltaSeconds)
    {
    	Super::Tick(DeltaSeconds);
    	//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, TEXT("WORKING"));
    
    	UWorld* const World = GetWorld();
    	for (TObjectIterator<AAIEnemy> Itr; Itr; ++Itr)
    	{
    		for (TObjectIterator<AAIEnemy> ItrOther; ItrOther; ++ItrOther)
    		{
    			float itrDist = Itr->GetDistanceTo(*ItrOther);
    			if (Itr->GetName() != ItrOther->GetName() && itrDist <= 750) {
    				if (Itr->GetIsHorde() && ItrOther->GetIsHorde())
    				{
    					if (!Itr->GetInHorde() && !ItrOther->GetInHorde()){
    						Itr->HordeContainer = World->SpawnActor<AAIHordeContainer>();
    						ItrOther->HordeContainer = Itr->HordeContainer;
    						//Itr->HordeContainer->AddMember(*ItrOther);
    						//Itr->HordeContainer->AddMember(*Itr);
    						//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, Itr->GetName());
    						//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, ItrOther->GetName());
    					}
    					else if (!Itr->GetInHorde() && ItrOther->GetInHorde()){
    
    					}
    					else if (Itr->GetInHorde() && !ItrOther->GetInHorde()){
    
    					}
    					else if (Itr->GetInHorde() && ItrOther->GetInHorde()){
    						//Check if in same Horde
    						//Merge Hordes or steal members
    					}
    				}
    			}
    		}
    	}
    }
    AIHordeContainer.h...cannot #include "AIEnemy.h"...results in an error. Cannot declare a TArray of type AIEnemy.
    Code:
    #pragma once
    
    #include "GameFramework/Actor.h"
    #include "Array.h"
    #include "AIHordeContainer.generated.h"
    
    /**
     * 
     */
    UCLASS()
    class AAIHordeContainer : public AActor
    {
    	GENERATED_UCLASS_BODY()
    
    	TArray<AAIEnemy> HordeMembers;
    	uint8 HordeSize;
    };

    #2
    Originally posted by alg0801 View Post
    AIHordeContainer.h...cannot #include "AIEnemy.h"...results in an error.
    What error? You need to declare a class before using it, which means including the header file.

    Oh, you seem to have circular dependencies. Don't include AIHordeContainer.h in AIEnemy.h, but replace it with a forward declaration like this:
    Code:
    class AAIHordeHandler;
    Last edited by Nasarius; 04-06-2014, 02:41 PM.

    Comment


      #3
      alg0801,

      You probably want to keep an array of pointers, not an array of AAIEnemy objects:

      TArray<AAIEnemy*> HordeMembers;
      Raw C++ pointers circumvent the garabage collector, so if your enemies get destroyed, these pointers may actually point to invalid memory. If you intend to keep the TArray around for a while, you should probably use weak object pointers instead:

      TArray<TWeakObjectPtr<AAIEnemy> > HordeMembers;
      Now, if an enemy gets destroyed, the weak pointer will simply be nullptr. When you iterate the array, make sure to check that the pointers are still valid, i.e:

      if (HordeMembers[Index].IsValid() { .... }
      Gerke Max Preussner | UE4 Programming Consultant

      Logo
      Follow me on Github | Goodreads | Linkedin | Pinterest | SlideShare | Twitter.
      Chat with me as gmpreussner on Discord.

      Comment


        #4
        Forward declaration fixed it...also looked over my dependencies and cleaned it up.

        Definitely wanted a pointer gmpreussner Thanks.
        Last edited by alg0801; 04-06-2014, 05:26 PM.

        Comment

        Working...
        X