Python API: How to get World Context Object?

Hi all,

Is anyone familiar with the Python or even C++ interface to control the editor? Essentially, I have a Python script that can start and stop Play-in-Editor, and that script can be run in the Unreal terminal. I’d like to add pause functionality using this:

classmethod set_game_paused(world_context_object , paused ) → bool
docs

But I’m not sure how to get the right World Context Object. If anyone is familiar with getting the right one during play state, whether in Python or C++, that’d be great.

Things that I’ve tried:

  • world = unreal.UnrealEditorSubsystem().get_game_world()
  • world = unreal.UnrealEditorSubsystem().get_editor_world()
    Both of these seem to work when running the Python line individually in the Unreal terminal, but not as part of a Python script.

It works when the game is running.

I ran the python script from a Editor Utility Blueprint that inherits from Editor Utility Actor:
image

import unreal

unreal.log("Hey world")

world = unreal.UnrealEditorSubsystem().get_game_world()
unreal.log(f"World: {world.get_name()}")

unreal.GameplayStatics.set_game_paused(world, True)
unreal.log(f"Is game paused? {unreal.GameplayStatics.is_game_paused(world)}")

LogPython: Is game paused? True
1 Like