State Manager

class game_state.StateManager(window)

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

Parameters:

window (Surface) –

The State Manager’s init.

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.

Return type:

None

Raises

AssertionError

Raised when the state name doesn’t exist in the manager.

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.

Return type:

None

Raises

StateError

Raised when the hook function was not found in the state file to be loaded.

exit_game(**kwargs)

Exits the entire game.

Parameters:

**kwargs (Any) – The keyword arguments to be passed on to the raised errors.

Return type:

NoReturn

Raises

ExitGame

Raised when the state has successfully exited.

get_current_state()

Gets the current State instance.

Return type:

Optional[State]

Returns

Optional[State]

Returns the current State instance.

get_last_state()

Gets the previous State instance.

Return type:

Optional[State]

Returns

Optional[State]

Returns the previous State instance.

get_state_map()

Gets the dictionary copy of all states.

Return type:

Dict[str, State]

Returns

Dict[str, State]

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.

  • **kwargs (Any) – The keyword arguments to be passed to the State’s subclass on instantiation.

Return type:

None

Raises

StateLoadError

Raised when the state has already been loaded. Only raised when force is set to False.

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

State

Returns the newly made State instance.

Raises

StateLoadError

Raised when the state has already been loaded.

run_state(**kwargs)

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

Parameters:

**kwargs (Any) – The keyword arguments to be passed on to the raised errors.

Return type:

None

Raises

StateError

Raised when the current state is None i.e having no State to run.

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

Unloads the State from the StateManager.

Parameters:
  • *states – The States 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

type[State]

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.

param state_name:

type state_name:

str

update_state(**kwargs)

Updates the changed State to take place.

Parameters:

**kwargs (Any) – The keyword arguments to be passed on to the raised errors.

Return type:

NoReturn

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.