Strange Bug Skips For Loop in PlayGame()

Ive encountered a strange bug in my PlayGame() function here​

but my game runs normally when i do this:​

int32 MAX_TURNS = 5;​
for (int32 count = 1; count <= MAX_TURNS; count++)​​{....}

But when i assign MAX_TURNS MyMaxTries from my .h file the for loop is entirely skipped

int32 MAX_TURNS = BCGAME.GetMaxTries();

this here gets skipped

for (int32 count = 1; count <= MAX_TURNS; count++)​​{....}​

and if i try this instead the loop will not run.

for (int32 count = 1; count <= BCGAME.GetMaxTries()​; count++)​​{....}​

ive even tried not putting const in to see if that was part of the issue

sorry if this is hard to read, i tried to make it as neat as possible.

if someone can explain why this happens id appreciate it.
my FBullCowGame.cpp here​

Unfortunately all three links appear to be dead (404 errors on GitHub), but I suspect that your BCGAME.GetMaxTries() is returning 0…
Try putting:

 UELOG(LogTemp, Warning, Text("BCGAME.GetMaxTries() = %i", BCGAME.GetMaxTries())

before the For loop and seeing what the result is.

How would i go about doing that? UELOG(LogTemp, Warning, Text(… is all undefined

ill see about uploading a snippet

Also heres my FBullCowGame.h

Ok, after reviewing my code ALOT, i found the bug.

int32 MyMaxTries

is declared twice. once in FBullCowClass.h and again in the Reset() function defined in FBullCowClass.cpp
A simple mistake, now it runs properly.

EResetStatus FBullCowGame::Reset()
MyCurrentTry = 1;
int32 constexpr MAX_TRIES = 3;
int32 MyMaxTries = MAX_TRIES; //OOOPS!!
const FString HIDDEN_WORD = "kanto";
MyHiddenWord = HIDDEN_WORD;
return EResetStatus::ok;

I’m assuming that you removed the int32 from before MyMaxTries=MAX_TRIES as the fix? I’d just been about to suggest that as I was browsing your code when I scrolled down to your Solution post. :slight_smile:

I would probably move the MAX_TRIES declaration out of the Reset() function as well, and declare it with your declarations… It’s a constexpr, so it’s not like you’ll be changing it… then Reset() would just contain MyMaxTries=MAX_TRIES;

Thanks, ill do that, its safer and simpler

Privacy & Terms