Jump to content

[SOLVED] Check engine game player


photo

Recommended Posts

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

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

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

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
×
×
  • Create New...