TArray Functions .Num() not picking up HiddenWords.h is an Empty Array

Hi,

Been bashing my head against this for a few hours now and just thought I’d throw it to the community and see if anyone else has encountered this problem

I get no errors, but the HiddenWord TArray is empty. When I cursor over in code, it recognizes the data type and compiles no problem, but it’s not picking up the words. It comes in empty and the moment I try to do anything with the array, UE crashes…

Any help? Thanks

Scott

could you show the code?

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Containers/UnrealString.h"
#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetupGame();//Setting up game

    PrintLine(TEXT("The number of possible words are: %i."), Words.Num());
    PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord);
    PrintLine(TEXT("\nIts length is: %i"), HiddenWord.Len());
 
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    //Main loop on entry by Player   
    if (bGameOver)//is Game Over?
    {
        ClearScreen();
        SetupGame();
    } else if(!bGameOver)//checking guess
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::SetupGame()
{
     //Initialize Member Variable(s)
    HiddenWord = TEXT("ounces"); 
    Lives = HiddenWord.Len();
    bGameOver = false;

    //Welcome message
    PrintLine(TEXT("Welcome Player to Bull Cows!"));
    PrintLine(TEXT("Guess the %i letter magic word..."), HiddenWord.Len());
    PrintLine(TEXT("You have %i Lives to guess the word..."), Lives);
    PrintLine(TEXT("Type in your guess and\nPress <ENTER> to continue..."));
    
   /*  const TCHAR HW[] = TEXT("peaches");
    //const TCHAR HW = {TEXT('o'), TEXT('u'), TEXT('n'), TEXT('c'), TEXT('e'), TEXT('s'), TEXT('\0')};
    PrintLine(TEXT("Character 1 of the hidden word is: %c"), HiddenWord[0]);//should print first char of the word "o"
    PrintLine(TEXT("Character 4 of HW array is: %c"), HW[3]); */
}

void UBullCowCartridge::EndGame()
{
     bGameOver = true;
     PrintLine(TEXT("\nThe Game is Over\nPress <ENTER> to play again..."));
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You Won!"));
        EndGame();
        return;
    }

   //Check length of guess to length of Hidden Word
   if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The HiddenWord is  %i letters long"), HiddenWord.Len());
        PrintLine(TEXT("Sorry, guess not same length as word\nYou have %i lives left..."), Lives);
        return;
    }

    //Check if it's an isogram 
    if (!IsIsogram(Guess))
    {
        //IsIsogram return a bool on whether it's an Isogram or not, if not
        PrintLine(TEXT("There are no repeating letters,\nPlease guess again..."));
        //remove Life 
        --Lives;
        return;
    } 

    //Remove a Life
    PrintLine(TEXT("You have lost a life...\n"));
    --Lives;

    //check if any Lives left
    if (Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("You have no lives left, the cows rejoice"));
        PrintLine(TEXT("The Hidden Word was %s.\nGood job for doing as well as you did..."), *HiddenWord);
        EndGame();
        return;
    } 

    //Show player the Bulls and Cows
    PrintLine(TEXT("Guess again, you have %i Lives left..."), Lives);

}//end ProcessGuess

bool UBullCowCartridge::IsIsogram(FString Word) const
{

    //get length of Guess, so we know our outer bounds
    int32 GuessLength = Word.Len()-1;

    
    //check number of characters
    //set a value to number of chars
    //step through each value and see if there is another char like it in the array
    //if no doubles return true as it is an isogram

    return true;
}![EmptyArrayError|690x401](upload://n3qAgNAWGMxTYaso57Q9GvJEusr.jpeg) 

BullCowCartridge.h

// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;
	void SetupGame();
	void EndGame();
	void ProcessGuess(FString Guess);
	bool IsIsogram(FString Word) const;

	// Member Variables
	private:
	FString HiddenWord; // HiddenWord container
	int32 Lives; //init Lives for game
	bool bGameOver; //init game over
	bool bIsIsogram;//init IsIsogram
	TArray<FString> Words;// wordlist
};

You have declared a member variable called Words which is empty. It’s shadowing your global variable that isn’t. Remove this line.

That’s the ticket. Thanks :smiley:

You can mark this solved…

You can also do that by clicking the 3 dots next to reply.

Awesome, thanks :smiley:

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

Privacy & Terms