Bulls & Cows SetupGame function and variable scope

Hi,

I’m having an issue with a variable I’m trying to access not being in scope in the SetupGame() function. I’m currently on lecture 78 in the Unreal Engine C++ Developer course, and I’m trying to access the valid words array in order to pick the HiddenWord from that. In the screenshot, my code is slightly different than where the Github commit is at because I moved the loading of the word list to runtime in order to speed up compile times (see here for the full details).

Currently I can see the issue is that my Isograms variable is not in scope for the SetupGame() function, and so it doesn’t work. To me this is expected, since nothing is passed into the function to use and it’s declared outside. What I don’t understand is why in the lecture’s code the Words variable is accessible (see 2nd screenshot). Nowhere in the SetupGame() function is this passed in or declared, and it doesn’t even appear anywhere else in any other function in the file or even in the header file. For my specific example, it would be great to know of a fix, but I think it may be even better to understand how the Words variable is in scope in the class code. I think once I understand that, fixing my issue won’t be too difficult for me.

Thanks!

Is Isograms declared as a member variable of the class. It appears that you are trying to access the list in the setupgame function but the Isograms array is local to the begin play function as far as I’m able to tell from the screenshots. You would either need to pass the Isograms array as an argument to setupgame or assign it to a membe variable of the UBullCowCartrige class

1 Like

Thanks for the reply. You’re right, I was considering just passing it into the function. I had another look at member variables and that does sound like what I want, and it would give me access to the TArray FString variable “Isograms”. I think I found out the answer to my other question too: I was really confused why the class’ code had access to the “Words” variable, since it’s not passed into the SetupGame function or declared as a member variable in the BullCowCartridge.h file, but it still has access to it. I used CTRL+click on the Words variable and it brought me to the HiddenWordList.h, a file I forgot about since I deleted it when I moved the words list to runtime to speed it up. So it is in scope because of that header file being included. Since I don’t intend on using that file for my project, I will try just declaring it in the BullCowCartridge header file instead.

1 Like

I’m glad you were able to resolve the problem. Enjoy the rest of the section!

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

Privacy & Terms