Skip to content

getGameState ​

Get the Current Game State ​

getGameState is used to query the lifecycle stage the mini-game is currently in. Since starting, running, and stopping a mini-game is an asynchronous process, this method lets you obtain the engine's internal state machine position in real time, so you can decide whether a specific operation can be performed (for example, play() can only be called in the RUNNING state).


Method Declaration ​

java
/**
 * Gets the current state of the game.
 *
 * @return an integer value corresponding to the GAME_STATE_* constants defined in the interface
 */
int getGameState();

State Constants ​

Constant NameValueDescription
GAME_STATE_UNAVAILABLE0Unavailable: The runtime environment has not been created, or has been destroyed by destroy(). No game operations can be performed.
GAME_STATE_WAITING1Waiting: The environment has been initialized via create(), but the script engine has not started, or has been stopped by stop().
GAME_STATE_RUNNING2Running: The script engine has started (via start()), and the game logic is executing in the background, but user input is not yet processed.
GAME_STATE_PLAYING3Playing: The game is in the foreground active state (via play()), receiving and processing player input such as touch and sensor data.

Runtime Behavior ​

  1. Immediacy: This method returns the engine's actual state at the moment of the call, not the expected state at the app logic layer.
  2. Driving the UI: Usually used together with setGameStateListener. getGameState is used for active querying (such as judging on button click), while the listener is used for passively receiving state change notifications.

Example Code ​

java
// Example: logic judgment when the "Start Game" button is clicked
int currentState = gameHandle.getGameState();

switch (currentState) {
    case SUDRTGameHandle.GAME_STATE_WAITING:
        // State is correct, start the script
        gameHandle.start("{}");
        break;
    case SUDRTGameHandle.GAME_STATE_RUNNING:
        // The script is already running, enter the input state directly
        gameHandle.play();
        break;
    case SUDRTGameHandle.GAME_STATE_UNAVAILABLE:
        Log.e("SUDOP", "The game environment is not ready. Please call create() first.");
        break;
    default:
        Log.d("SUDOP", "Current state: " + currentState);
        break;
}

Notes ​

  • Illegal operation interception: Calling start() or play() in the UNAVAILABLE state usually triggers an error callback or an invalid operation, so it is good programming practice to check the state before executing key lifecycle commands.
  • State transition latency: Switching from WAITING to RUNNING involves starting the JS engine, which may incur a slight delay. It is recommended to capture the final successful transition through a listener.