State Manager

class game_state.StateManager(window)

The State Manager used for managing multiple State(s).

Parameters:

window (Surface) – The main game window.

change_state(state_name)

Changes the current state and updates the last state.

Parameters:

state_name (str) –

The name of the State you want to switch to.

Raises:
StateError
Raised when the state name doesn’t exist in the manager.
Return type:

None

connect_state_hook(path, **kwargs)

Calls the hook function of the state file.

Parameters:
  • path (str) –

    The path to the State file containing the hook function to be called.

  • **kwargs (Any) –

    The keyword arguments to be passed to the hook function.

Raises:
StateError
Raised when the hook function was not found in the state file to be loaded.
Return type:

None

exit_game(**kwargs)

Exits the entire game.

Parameters:

**kwargs (Any) –

The keyword arguments to be passed on to the raised errors.

Raises:
ExitGame
Raised when the state has successfully exited.
Return type:

NoReturn

get_current_state()

Gets the current State instance.

Return type:

Optional[State]

Returns:

Returns the current State instance.

get_last_state()

Gets the previous State instance.

Return type:

Optional[State]

Returns:

Returns the previous State instance.

get_state_map()

Gets the dictionary copy of all states.

Return type:

Dict[str, State]

Returns:

Returns the dictionary copy of all states.

load_states(*states, force=False, **kwargs)

Loads the States into the StateManager.

Parameters:
  • states (Type[State]) –

    The States to be loaded into the manager.

  • force (bool) –

    Default False.

    Loads the State regardless of whether the State has already been loaded or not
    without raising any internal error.

    WARNING: If set to True it may lead to unexpected behavior.

  • **kwargs (Any) –

    The keyword arguments to be passed to the State’s subclass on instantiation.

Raises:
StateLoadError
Raised when the state has already been loaded.
Only raised when force is set to False.
Return type:

None

reload_state(state_name, force=False, **kwargs)

Reloads the specified State. A short hand to StateManager.unload_state & StateManager.load_state.

Parameters:
  • state_name (str) –

    The State name to be reloaded.

  • force (bool) –

    Default False.

    Reloads the State even if it’s an actively running State without
    raising any internal error.

    WARNING: If set to True it may lead to unexpected behavior.

  • **kwargs (Any) –

    The keyword arguments to be passed to the
    StateManager.unload_state & StateManager.load_state.

Return type:

State

Returns:

Returns the newly made State instance.

Raises:
StateLoadError
Raised when the state has already been loaded.
Only raised when force is set to False.
run_state(**kwargs)

The entry point to running the StateManager. To be only called once. For changing States use StateManager.change_state & StateManager.update_state

Parameters:

**kwargs (Any) –

The keyword arguments to be passed on to the raised errors.

Raises:
StateError
Raised when the current state is None i.e having no State to run.
Return type:

None

unload_state(state_name, force=False, **kwargs)

Unloads the State from the StateManager.

Parameters:
  • state_name (str) –

    The State to be loaded into the manager.

  • force (bool) –

    Default False.

    Unloads the State even if it’s an actively running State without raising any
    internal error.

    WARNING: If set to True it may lead to unexpected behavior.

  • **kwargs (Any) –

    The keyword arguments to be passed on to the raised errors.

Return type:

Type[State]

Returns:

The State class of the deleted State name.

Raises:
StateLoadError
Raised when the state doesn’t exist in the manager to be unloaded.
StateError
Raised when trying to unload an actively running State.
Only raised when force is set to False.
update_state(**kwargs)

Updates the changed State to take place.

Parameters:

**kwargs (Any) –

The keyword arguments to be passed on to the raised errors.

Raises:
ExitState
Raised when the state has successfully exited.
StateError
Raised when the current state is None i.e having no State to update to.
Return type:

NoReturn