Download

Help with code

Since it wasn’t wroth it to make a new topic i’m asking it here.
Can someone please help me with this code :


if((Health <= 0)) {
        if (DeadSound) {
            DeadSound->Play();
        }
    }

i wrote this for when player health reaches 0 to play the deadSound but it never plays ! (it just play when i hold alt+tab and then it suddenly play !)
it’s not only that almost all of my “bool” variables works invert.like this one :


if (isPlayerFound == true) {
            if (AIFoundSound) {
                AIFoundSoundMain->Play();
            }
        }

But if i choose" if (isPlayerFound == false) { " it will works but it will makes a noise on my speaker when enemy isn’t found the player at it will gets worse if AI count being more.

Can anyone hell me with this ?

Your question was completely unrelated to the topic you posted it in, so you should have made a new topic. I’ve moved your post.

In regards to the question, it just looks like a logic error. There’s nothing inherently wrong with the syntax.

Use breakpoints to check the values of your variables. My initial thoughts is that ‘DeadSound’ is null, and there is a logical error with the way you’ve made ‘AiFoundSoundMain’ work. We can’t really help with that.

Thanks,

I really don’t know what’s this logical error but here is the part i wrote for when player dies :


if (Health <= 0) {
        //UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false);
        if (DeadSound) {
            DeadSound->Play();
        }
        isThirdPerson = true;
        //PlayerTPSModel->Activate (true);
        PlayerTPSModel->SetSimulatePhysics(true);
        CameraBoom->SetWorldLocation(PlayerTPSModel->GetComponentLocation());

    }

Everything works as it have to and it changes the camera to TPS Mode and actives the physics on characters TPS model bones but “DeadSound->Play();” don’t work at all.

And About the AI as i wrote before this code don’t work :


 
 if (isPlayerFound == true) {             if (AIFoundSoundMain) {                 AIFoundSoundMain->Play();             }         } 

But once i change the code to this :


if (isPlayerFound == false) {
            if (AIFoundSoundMain) {
                AIFoundSoundMain->Play();
            }
        }

It works and it plays the sound once AI finds the player but it makes a noise on the speaker when player isn’t found yet and the more AI be the worse and louder this noise will be.

Not sure what i’m doing wrong i don’t have a single clue about why this happens as it’s not normal at all.can it be a bug or something with the game engine ? i downloaded the engine from Epic game launcher haven’t compiled it myself.

And nothing is Null right now and i checked all of them.

I solved the issue with dead sound like this :


void APlayerCharacter::PlayerDied() {
    bool Do = true;
    if (DeadSound && Do) {
        DeadSound->Play();
        Do = false;
    }
}

i made a new void and named it “PlayerDied” as i tested the dead sound on “BegingPlay” and it was working well but on “Tick” it wasnt playing or it was playing weird and uncompleted so i made a new function to do the dead sound just once and works well but still plays at start of the game.it’s part of the code that plays it on “Tick” :



if (Health != 0.f) {
        PlayerDied();
    }

Not sure why it play at start of the game which health isn’t 0.

And about the AI Voice i tried to add a message on screen when AI finds the player like this :


if (isPlayerFound) {
            GEngine->AddOnScreenDebugMessage(-1, 0.002, FColor::Red, (FString::Printf(TEXT("Found player"))));
            AIFoundSoundMain->Play();

            //if (AIFoundSoundMain) {
                bool Do = true;
                if (Do) {
                    AIFoundSoundMain->Play();
                    Do = false;
                }
            //}
        }

It exactly shows the “Found Player” message on screen when AI finds the player.

But problem is that this Part :


AIFoundSoundMain->Play();

Will only happen when AI lost the player not when it finds the player !!

Why in the world the first line of the code works when “isPlayerFound” is true (and shows the message “Found Player”) but second line of exactly same code shouldn’t work when both work with same condition ?!

Ok I solved the problem.

I just found that the sound was playing all this time but tick function was turning it to a noise instead of playing it once as it was and it was looipng it all the time which “isPlayerFound” was true.

And it’s was exactly the reason that the sound was playing at the end which “isPlayerFound” was false cause it wasn’t trying to loop anymore and instead of that noise it was playing the sound that i wanted and i was thinking that it playing the sound when “isPlayerFound” is false which it wasn’t…

And whatever,the problem was that you can’t play a sound once with this :


AIFoundSoundMain->Play();

in a tick function at least not all by itself and you need a why to do that part of the code just once and here is the codes i wrote at the end for the sounds :


void AEnemyAI_1Character::FoundSoundPlay() {


    if (!PlaySound) {
        AIFoundSoundMain->Play();
        PlaySound = true;
    }
}

and the part in tick function to playing it :

i


f (isPlayerFound) {
            GEngine->AddOnScreenDebugMessage(-1, 0.002, FColor::Red, (FString::Printf(TEXT("Found player"))));

            FoundSoundPlay();

        }

And the about the dead sound in Player Code :


void APlayerCharacter::PlayerDied() {

    if (DeadSound && !PlayDeadSound) {
        DeadSound->Play();
        PlayDeadSound = true;
    }
}

And Tick Part for running it :


if (Health <= 0.f) {
        PlayerDied();
    }

And they work exactly as i wanted and all my problems with that noise is gone as well !