Page 1 of 2

[Resolved] Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 20:45
by BrianLovesMarvel
Hi BGA studio gurus,

I'm about 40 hours into programming this game, learning the BGA studio framework
and the game states model. Most things are working for me, but I ran into this weird condition
and I'm hoping someone can tell me why it is happening. :?:

Apparently I can't call getCurrentPlayerName() in some game states and I don't understand
why. I set up a few states: startRound, playerTurn, nextPlayer, endRound and some associated
state functions. Within stPlayerTurn, I cannot call self::getCurrentPlayerName() or I get an error
and the game doesn't even get created.

Unexpected error: Propagating error from GS 1 (method: createGame): Fatal error during xxx setup: Not logged
Unexpected error: Invalid player number for this game: 1


Can you explain why calling getCurrentPlayerName() doesn't work? Is stPlayerTurn getting called before the
game is fully initialized? I'm calling $this->activateNextPlayer() inside setupNewGame(). I would assume
that the state machine is not started and no state functions are called until after setupNewGame(). Is this wrong? :?

Help me out here, please. I've searched the forum but I don't see an answer.

Thanks much, Brian

Portions of states.inc.php:

Code: Select all

$machinestates = array(

	// The initial state. Please do not modify.
	1 => array(
		"name" => "gameSetup",
		"description" => "",
		"type" => "manager",
		"action" => "stGameSetup",
		"transitions" => array( 
			"startRound" => 10 
		)
	),

	// Start of the round of player turns. Advance the round number.
	10 => array(
		"name" => "startRound",
		"description" => "",
		"type" => "game",
		"action" => "stStartRound",
		"transitions" => array( 
			"playerTurn" => 20,
			"endGame" => 99
		)
	),
 
	// Start of a player turn.
	20 => array(
		"name" => "playerTurn",
		"type" => "activeplayer",
		"action" => "stPlayerTurn",
		"description" => clienttranslate('${actplayer} must choose an action.'),
		"descriptionmyturn" => clienttranslate('${you} must choose an action: '),
		"possibleactions" => array( "pass" ),
		"transitions" => array( 
			"playerTurn" => 20, 
			"nextPlayer" => 80, 
			"endRound" => 90, 
			"endGame" => 99
		)
	),
Javascript console log shows:

Code: Select all

Starting game setup
notifications subscriptions setup
Ending game setup

onUpdateActionButtons: playerTurn
Entering state: playerTurn
onPass event handler
Leaving state: playerTurn

onUpdateActionButtons: nextPlayer
Entering state: nextPlayer
Leaving state: nextPlayer

onUpdateActionButtons: endRound
Entering state: endRound
Leaving state: endRound

onUpdateActionButtons: startRound
Entering state: startRound
Leaving state: startRound
Game log displays:

Code: Select all

Starting round 1
Start of turn
brianhannamn0 passed
End of turn
Start of turn
brianhannamn1 passed
End of turn
End of round 1
Starting round 2
stPlayerTurn state function:

Code: Select all

function stPlayerTurn()
{
	// Get the current player
	// Can't do this, it throws an error that's very hard to debug. Why?
	//$player_name = self::getCurrentPlayerName();

	// Notify players of the turn
	self::notifyAllPlayers( "announceStartTurn", clienttranslate( "Start of turn" ), array() );
		
	// (very often) go to another gamestate
	//$this->gamestate->nextState( 'drawEventCard' );
	//$this->gamestate->nextState( 'advanceLava' );
	//$this->gamestate->nextState( 'nextPlayer' );
}	

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 21:29
by RicardoRix
looks good to me.

You could look into the players DB table, see if you can see anything wrong.
Do you have all the player setup stuff in the setupNewGame() ?

You could try here.
https://en.doc.boardgamearena.com/Troubleshooting

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 22:02
by BrianLovesMarvel
Yes, I'm doing the normal user setup stuff, using one of the demos as a model. I'll take a look at the player table, good idea.

On the troubleshooting page it says my "not logged" error can come from

Calling self::getCurrentPlayerId () or using $g_user from 'args' state function, see also below

I'm not using an args function, but it does seem like it might be a related issue. Args and state
functions are both defined in the machine_states array. In another part of the page it says:

As a general rule, you should use getActivePlayerId() and not getCurrentPlayerId().

and references the states tutorial. I had gone through that before, but reviewing it, on slide 8 I now see: :idea:

Pay attention: active_player != current_player
In BGA framework, the current player is the player who played the current player action
(= player who made the Ajax request). Most of the time the current player is also the active player,
but in some context this could be wrong.


So maybe that partly explains it. When I get to stPlayerTurn(), no actions have taken place yet, so the current player
has not been set. I'll try some of the getActivePlayer functions instead and see how that works.

:? I guess I still don't understand why stPlayerTurn() is being called before the game setup is complete. I wouldn't
think the state functions would be called until that state is first activated. Here it keeps the game
from even being initialized. It crashes and shows just a single player in the game.

Very mysterious. Inquiring minds want to know. :D

Thanks, Brian

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 22:06
by RicardoRix
Are you stopping at any time for the user to press a button and then making a callback ajax action before moving to the next game state?

Just noticed this under the zombie funtion:
you must _never_ use getCurrentPlayerId() or getCurrentPlayerName(), otherwise it will fail with a "Not logged" error message.

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 22:22
by BrianLovesMarvel
Yes, that's exactly it. I'm setting up a button for the player to choose an action. At that point, current_player isn't defined.

You know how client_translate changes ${player_name} into a colored name? Player name is not defined at those points.

The getActivePlayerName function works in those state functions, but interestingly, client_translate won't color it.

How does the zombie mode fit in? I didn't think I had any zombies, but maybe the framework uses one to start with?

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 22:26
by Benoit314
Your problem comes from the difference between Active / Current player.
The current player is the player who sent the request to the server. State, state args or zombie functions aren't triggered by a player request, so there is no current player in these.
You need to use the active player, which is the player(s) whose turn it is.

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 22:36
by BrianLovesMarvel
Thanks, that makes sense! :D

Re: Can't call getCurrentPlayerName in some states

Posted: 20 January 2021, 23:35
by RicardoRix
but there is a currentPlayer because the state is a "type" => "activeplayer" state...

do you check for this in the JS before they can click the button.
if ( !this.isCurrentPlayerActive() ) return;

Important: the variable for player name must be ${player_name} in order to be highlighted with the player color in the game log
${player_name2} works too, but not ${player_name3}.

Re: Can't call getCurrentPlayerName in some states

Posted: 21 January 2021, 15:54
by BrianLovesMarvel
Thanks for the help, guys! I'm back to functional again. :D

Its still a mystery why the game does not initialize properly. I understand now there would be no current_player in a state, args, or zombie function, but why does just the code in the state function cause it to crash during initialization? :?:

It seems there is something mysterious happening in or near setupNewGame. :?

Debugging the game initialization is a bit difficult. :idea: It might be nice to have access to a log of initialization steps that are happening behind the scenes, or :idea: an improvement of the errors thrown in the "not logged" section so we can see where things are going off the rails.

When I got into this stage, I had to comment out almost all of my code and finally ask for help before being able to get back on track. The troubleshooting page and state machine presentation had some good hints, too!

Thanks very much, Brian

Re: Can't call getCurrentPlayerName in some states

Posted: 21 January 2021, 17:26
by Benoit314
Check in the wiki, there is a page with common errors messages with some hints on the cause.
https://en.doc.boardgamearena.com/Troubleshooting