this.bga.states
JS State classes
To split the code of the Game class, you can create dedicated classes for each state you handle on the front end (some states like NextPlayer don't need to be handled on the JS side).
this.bga.states.register(stateIdOrName: string|number, stateClass: Object): void
This method links a state class to a state name. It should be used in the Game constructor like this:
// Declare the State classes
this.bga.states.register('PlayerTurn', new PlayerTurn(this, this.bga));
When a state class is declared like this, the framework will call its onEnteringState... functions automatically. Example of a JS State class:
class PlayerTurn {
constructor(game, bga) {
this.game = game;
this.bga = bga;
}
/**
* This method is called each time we are entering the game state. You can use this method to perform some user interface changes at this moment.
*/
onEnteringState(args, isCurrentPlayerActive) {
}
/**
* This method is called each time we are leaving the game state. You can use this method to perform some user interface changes at this moment.
*/
onLeavingState(args, isCurrentPlayerActive) {
}
/**
* This method is called each time the current player becomes active or inactive in a MULTIPLE_ACTIVE_PLAYER state. You can use this method to perform some user interface changes at this moment.
* In MULTIPLE_ACTIVE_PLAYER states, you may want to call this function in onEnteringState using `this.onPlayerActivationChange(args, isCurrentPlayerActive)` at the end of onEnteringState.
* If your state is not a MULTIPLE_ACTIVE_PLAYER one, you can delete this function.
*/
onPlayerActivationChange(args, isCurrentPlayerActive) {
}
// Put custom functions used only in this state here
}
this.bga.states.getCurrentMainStateName(): string;
Get the current main state name (ignoring private states)
this.bga.states.getCurrentPlayerStateName(): string;
Get the current player state name (private state name if there is one)
this.bga.states.logger: Function
You can write this.bga.states.logger = console.log to show debug information about state changes in the console. Remove before going to production!
this.bga.states.getStateClass(stateName: string): Object
Get a state class (instance) by state name.
this.bga.states.getCurrentMainStateClass(): Object;
Get the current main state class (instance) (ignoring private states)
this.bga.states.getCurrentPlayerStateClass(): Object;
Get the current player state class (instance) (private state if there is one)
this.bga.states.getStateClasses(): Object[];
Returns the registered state classes (instances)
Client states
Client states are a way to simulate a state transition without actually contacting the server. They are useful when you need to ask the user multiple questions before you send data to the server.
Client states don't have a matching PHP class. They can be used with JS State classes, as long as the stateName declared in setClientState matches the registered JS State class.
this.bga.states.setClientState(stateName: string, args: Object): void;
Example:
this.bga.states.setClientState("client_playerPicksLocation", {
descriptionmyturn : _("${you} must select location"),
});
For more information see BGA_Studio_Cookbook#Multi_Step_Interactions:_Select_Worker.2FPlace_Worker_-_Using_Client_States
To add custom string interpolation for the state title, use the args object like so:
this.bga.states.setClientState("client_playerPicksLocation", {
descriptionmyturn : _("${you} must select location for card {$card_number}"),
args: { card_number: 5 },
});
this.bga.states.restoreServerGameState(): void;
If you are in a client state, this will restore the current server state (cheap undo).
this.bga.states.isOnClientState(): boolean;
Returns whether the current state is a client state.