GeorgeP Posted June 22, 2012 Share Posted June 22, 2012 Hello. I want to check if there is any player assigned to game. At the moment i use the following: Player current_player = engine.game.getPlayer(); if (current_player==0) { //There isnt any player assigned to the game yet ... } Is this the right way to do it ? i dont feel comfortable to check if current player is 0. thnx. Link to comment
danni.coy Posted June 24, 2012 Share Posted June 24, 2012 for development purposes I would be tempted to do the following #include <core/scripts/utils.h> Player player = Unigine::getPlayer(); assert(player != 0); Link to comment
danni.coy Posted June 26, 2012 Share Posted June 26, 2012 or start using unit testing. #include <core/scripts/utils.h> #define UNITTEST Player player; void init() { player = Unigine::getPlayer(); #ifdef UNITTEST unittest(); #endif //UNITTEST } void unittest() { assert (player != 0); } Link to comment
unclebob Posted June 27, 2012 Share Posted June 27, 2012 Hi George, Both ways are correct. It depends on task you trying to solve. Lets say, you want to write code that checks if the Player changes in runtime then you should use if statement. If your code requires to have some Player then assert statement is preferable. Also, unit testing can be based on assert statements. Also, you can use NULL instead of 0. But you'll need to include <core/unigine.h> from core.ung. I've modified your code sample: #include <core/unigine.h> // Somewhere in your code Player current_player = engine.game.getPlayer(); if(current_player == NULL) { // There isnt any player assigned to the game yet ... } Link to comment
GeorgeP Posted June 28, 2012 Author Share Posted June 28, 2012 Thank you for your time. i will follow the NULL solution. Link to comment
Recommended Posts