Hi, I was doing some work on my game, and rewriting it to avoid repetition of code, and have come across a problem I don't understand. On the main code, I have a function load where the user can load a game, within that function, I am reading from a file to determine how much the main character should level up. Anyway, it all works fine except for 1 thing. In the function for levelling up, I have an if statement to determine where or not to display a message, but it always appears to evaluate to true. What am I missing? The 2 relevant functions are below (all variables are declared, just not necessarily shown below):
first off line 55 should not be there, it should be at the top of your file by the includes above all other code. Also what is the point of the gameload parameter? I cannot see why it would always evaluate to true. Also, we cannot run the code as you have only posted a section of it, the best thing to do is make a suitable example that shows your problem or (please try to avoid this on these types of errors) post the full program source.
I've uploaded a zipped version of the project. You can download it at http://www.mediafire.com/?kp243m0hwviiqzw. The purpose of the gameload parameter is I do not want the message to display if the player is just loading the game (i.e. when gameload is true), but if they were to level up while playing, the message should display.
I'm guessing the print in that if-statement always displays because of the default value you are giving it in the parameter list of void Levelup(bool gameload=false)
Unless you explicitly change gameload somewhere inside the function or alternatively have a value of TRUE for the function's argument when calling this function, it will always be FALSE inside the function, therefore
!gameload will evaluate to TRUE, satisfying the if-statement and thus printing that out everytime.
@OP I am assuming that the LevelUp function you referred us to is inside your MainCharacter class. If not and you have a different one, please check that code.
After about 4 hours of searching, I finally found what the problem was. I forgot the AddExperience function had the while loop for levelling up in it, so that would get called (and displaying the message), and when it comes to the condition for the while loop, it would always be false so the stuff inside it was never getting called. I added a second parameter to the AddExperience to deal with the Levelup boolean, and now it works as intended. Thanks for the help everyone.