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()
Using cast to fix type hint errors
This section is out of date and unneeded with UE 5.1. Click here to expand.
🥳 UE5.1 has proper type hints! All these workarounds aren’t necessary anymore!
https://github.com/EpicGames/UnrealEngine/commit/ec3db1b24ccac9ac92564001d30dd7136d7963ac
Unfortunately unreal didn’t implement type hints properly in their stub file, so it’s just a bit wrong. Almost everything returns →None so pylance is convinced you will be making mistakes constantly.
You can get around this by using some of the cool typing features like cast
and Optional
def get_first_level_actor_name() -> str:
eas = cast(unreal.EditorActorSubsystem, unreal.get_editor_subsystem(unreal.EditorActorSubsystem))
if eas:
actors = cast(List[unreal.Actor], eas.get_all_level_actors())
if actors:
return cast(str, actors[0].get_path_name())
return ""
The docstrings for the stub file say what each function should return, so we can use the cast(type, variable) syntax from the typing module to tell pylance what to expect.
But why go through this effort? Well if we do this, then we know what each variable is, and if we know what each variable is then pylance can tell us what each variable can do.
The docstrings for the stub file say what each function should return, so we can use the cast(type, variable) syntax from the typing module to tell pylance what to expect.
But why go through this effort? Well if we do this, then we know what each variable is, and if we know what each variable is then pylance can tell us what each variable can do.
test 1
hola hola hola