Having difficulty with an Array of Structs

I’m working on an AOE status effect that involves creating weak points on enemies caught up in a sweep. I tried using a 2D array, but because I couldn’t use a non-constant for the size I had no luck. So my next idea was to use an array of structs. Each struct containing a pointer to that enemy and then an array for storing pointers to the weak points I spawn on their meshes. The trouble is I have no idea what the syntax for using .Add() when dealing with a TArray of structs in.

AActor* HitActor = OutHits[i].GetActor();
                if (HitActor->IsA(AEnemy::StaticClass()))
                {
                   
                    EnemiesHitByTactical.Add();
                }

As of right now I’m getting a red line under EnemiesHitByTactical.Add();

You have two options. The first is to declare an instance of your structure, fill out the members and then use that as the parameter when calling Add. The second option is to give your structure a constructor and call Emplace passing in the parameters that you would pass to the constructor.

1 Like

I mean we can’t even tell what kind of array EnemiesHitByTactical is so it’s hard to reply to this. I’ll try to write some code to maybe explain where my brain is on this issue.

struct FWeakSpot {
	FWeakSpot() {}
	FVector Loc
}

USTRUCT(BlueprintType)
struct YOURMODULENAME_API FActorWithWeakSpots {
    GENERATED_BODY()

public:
    // Default constructor
    FActorWeakSpots() {}

    // Constructor with actor only
    FActorWeakSpots(TWeakObjectPtr<AActor> InActor) : Actor(InActor) {
		//You could cast here to a specific actor type and extract the "weak points"
	}

    // Constructor with actor and weak spots already known
    FActorWeakSpots(TWeakObjectPtr<AActor> InActor, const TArray<FWeakSpot>& InWeakSpots)
	: Actor(InActor), WeakPoints(InWeakSpots) {}

 references
    TWeakObjectPtr<AActor> Actor;
    TArray<FWeakSpot> WeakPoints; // Assuming weak points are represented as positions; adjust the type as necessary could just as easily be an array of pointers to:
};

UCLASS()
class YOURMODULENAME_API AMyClass : public AActor
{
    GENERATED_BODY()

public:
    AMyClass() {};

protected:

public:
    // Dynamic delegate instance
    UPROPERTY(BlueprintAssignable, Category="Collision")
    FOnActorCollision OnActorCollision;

private:
    UFUNCTION()
    void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};


inline void AMyClass::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) {
	//Im declaring this locally in the function but maybe it's part of your class?
	TArray<FActorWithWeakSpots> WeakSpotsArray;

	AActor* HitActor = OutHits[i].GetActor();
	if (HitActor->IsA(AEnemy::StaticClass()))
	{
		//Create a new struct, Im using the constructor that "auto extracts" the weak spots
		const FActorWithWeakSpots WeakSpotStruct(OtherActor)
		//Otherwise i would need to do this
		// TArray<FWeakSpot> WeakSpots //Somehow get your weak spots
		// const FActorWithWeakSpots WeakSpotStruct(OtherActor, WeakSpots)

		WeakSpotsArray.Add(WeakSpotStruct)
	}

    OnActorCollision.Broadcast(WeakSpotsArray);
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.