Why do we do a Reset(); at the start, if it was initialized by the constructor?

Exactly that is my question.

At the very start of the Main.cpp we have
FBullCowGame BCGame; //Instantiate a new game

Which automatically runs the constructor, and thus running Reset.
Then we go to a Play Game that Resets our variables to their original value… Again!

Shouldn’t we have it in the “AsktoPlayAgain” Method? (Is Method the right name for it?, it gets kinda confusing) If they choose to play again, then we spend resources resetting the Game?

1 Like

Hi mate,

I’m stuck on this lesson at the moment, but I think the answer to your question is that FBullCowGame BCGame is instantiated once, when it is created. Therefore, you need the Reset function inside PlayGame(), because if the player decides to play again, you’re still playing in the same instance.

That is my idea anyway, take it with a grain of salt :slight_smile:

You are correct :slight_smile:

1 Like

I had the same question and it helped to think about reset working as a “one time only” deal, while the reset function provides the ongoing ability to reset the game. The double reset at the start doesn’t impede performance and doesn’t interfere with the rest of the code.

That being said, I would be curious to know whether this is standard practice or if this is something which we shouldn’t do on larger, more resource intensive projects.

I am interested in why we need Reset() function for initialization whereas we have constructor???

maybe Reset() every time change meaning of variables ??? maybe me view is right

In Unreal, and most C++ implementations, as well as many other programming languages, there is an automatic initialization of all standard variables when they are created. This, by proxy, tends to extend to full objects because at their deepest level, virtually every data structure is created from a combination of the standard variable types…

That being said…

It is unwise to -=rely=- on that programming practice. My first computer instructor (back in the 80s) had a large sign over the chalk board that said “Assume Nothing.”. Of course, back in the 80s, there were virtually NO programming languages that initialized variables.

Callling Reset() at the start is, in fact, completely and totally unneccessary in this environment, because, you’re right, in this environment, everything is already reset. But… best practices say you reset -=anyways=- because tomorrow you might be programming in an old version of Pascal that does NOT.

thank you

Privacy & Terms