Where is my thinking mistake?

I make a BeginOverlap function, get the enemy char out of it.
I put this character into a function.
I create a function that is triggered when F is pressed.
This F key function fetches the enemy char and calls a function from that char.
Where is my mistake?

in .cpp
void AMyCharacter::BeginBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)

{

if (OtherActor != this)

{

AMyCharacter* MeChar = Cast<AMyCharacter>(OtherActor);

    if (MeChar)

    {

    GibtGegner(MeChar);

    }

}

}

AMyCharacter* AMyCharacter::GibtGegner(AMyCharacter* Char)

{

return Char;

}

void AMyCharacter::F_Taste()

{

if (FinisherMontage)

{

GibtGegner()->PlayAnimMontage(FinisherMontage, 1.f, FName("KO"));

}

}

in .h

AMyCharacter* GibtGegner(AMyCharacter* Char);

GibtGegner takes AMyCharacter* and returns what was passed into it. How are you calling GibtGegner without passing any arguments to it in

GibtGegner()->PlayAnimMontage(FinisherMontage, 1.f, FName(“KO”));

?

You need to take the character that triggered the overlap and store it in a member variable of AMyCharacter, instead of calling GibtGegner. Then when you call F_Taste later you can retrieve the overlapped character from the member variable and play an anim montage.

You should also do more error checking as well. Make sure the character exists and is valid before playing the anim montage.

AMyCharacter* AMyCharacter::GibtGegner(AMyCharacter* Char)
{
     return Char;
}

This doesn’t make any sense. The function asks for a character, and it returns the same character that you gave it. So it does nothing basically. This is what I’d do. (Also I’m not sure if AMyCharacter is supposed to be the enemy, or the player class.)

// .h

// The latest enemy character from the overlap check.
UPROPERTY() // <-- Use this every time you are storing pointers in headers. Otherwise the garbage collector will delete it from memory.
AMyCharacter* EnemyCharacter;

// Returns the cached enemy character pointer. Can be null!
AMyCharacter* GetEnemyCharacter();
// .cpp

void AMyCharacter::BeginBoxOverlap(...)
{
     AMyCharacter* Enemy = Cast<AMyCharacter>(OtherActor);
     if (Enemy )
     {
          // Cache the enemy character.
          EnemyCharacter = Enemy;
     }
}

AMyCharacter* GetEnemyCharacter()
{
     // Return the character that is set in EnemyCharacter var.
     // Returns null if the variable is not set.
     return EnemyCharacter;
}
1 Like

@STRiFE.x i try this, but doesnt work, why?

in .h

UPROPERTY()
AMyCharacter* EnemyCharacter;

in .cpp

void AMyCharacter::BeginBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)

{

if (OtherActor != this)

{

AMyCharacter* MeChar = Cast<AMyCharacter>(OtherActor);

    if (MeChar)

    {

    EnemyCharacter = MeChar;

    }

}

}

void AMyCharacter::F_Taste()

{

if (FinisherMontage)

{
    if (EnemyCharacter)

    {

    EnemyCharacter->PlayAnimMontage(FinisherMontage, 1.f, FName("KO"));

    }

}

}

why??? thx for help

Hi, I’m not sure what you’re asking exactly but if it is why your beginoverlap function is not working it’s probably because there is a bug in UE (at least in 4.26) which is that the collision triggers are not hot-reloaded properly.

Try removing the actor from the scene and adding it again. It works for me.