Problems using python with Editor in UE4

Hello, I met a problem when i using python with Editor in UE4.
I find we can use python command “unreal.EditorLevelLibrary.editor_play_simulate()” to play the editor and use python command “unreal.EditorLevelLibrary.editor_end_play()” to stop the play mode.
But I write these two commands in a file, for example “test.py”, and then run"py test.py", I find the editor is still in the play mode, the stop command doesn’t work.
Does anyone have suggestions?
Thank you.

I experience this as well, UE 4.27

Hello,
I guess it has to do with python working in one thread, or basically locking the process, and there is no time for editor to start when the end_play is triggered.

It works with a below snippet. Hope it helps.

import unreal
from threading import Timer

def hello():
    print("hello, world")
    unreal.EditorLevelLibrary.editor_end_play()

unreal.EditorLevelLibrary.editor_play_simulate()
t = Timer(5.0, hello)
t.start() # after 5 seconds, "hello, world" will be printed and editor will stop

Regards,
Alex.

1 Like

Great! It really helps!!

The threading Timer may result in “not in game thread” issues.
one possible solution is below: from Python in Unreal Tips | Ryan DowlingSoka

class MyClass(object):
	def __init__(self) -> None:
		self.frame_count = 0
		self.max_count = 1000

	def start(self) -> None:
		self.slate_post_tick_handle = unreal.register_slate_post_tick_callback(self.tick)
		self.frame_count = 0

	def tick(self, delta_time: float) -> None:
		print(self.frame_count)
		self.frame_count += 1
		if self.frame_count >= self.max_count:
			unreal.unregister_slate_post_tick_callback(self.slate_post_tick_handle)

test = MyClass()
test.start()