PyActors¶
A PyActor is an Unreal actor that hands its events to a Python class. The plugin also has PyPawn, PyCharacter and a PythonComponent that work the same way.
Spawning one from Python¶
import unreal_engine as ue
class Hero:
# called when the game starts
def begin_play(self):
ue.log('Begin Play on Hero class')
# called every frame
def tick(self, delta_time):
location = self.uobject.get_actor_location()
location.z += 100 * delta_time # 1 m a second
self.uobject.set_actor_location(location)
Making one in the editor¶
- In the content browser, Add New → Blueprint Class and pick PyActor.
- In the Blueprint's Details, set Python Module (
my_module) and Python Class (Hero). - Add a component such as a sphere so you can see it, compile, and drag it into the level.
self.uobject¶
Your class is a proxy: it isn't the Unreal object itself. self.uobject is the actor. For a PythonComponent, self.uobject is the component and self.uobject.get_owner() is the actor.
From the actor, actor.get_py_proxy() gets your Python object back.
Events¶
Methods whose names start with on_ are connected to the matching event:
class Ball:
def on_actor_begin_overlap(self, me, other_actor):
ue.print_string('Collided with ' + other_actor.get_name())
self.uobject.actor_destroy()
def on_actor_hit(self, me, other_actor, normal_impulse, hit_result):
pass
Or bind them yourself:
class Ball:
def begin_play(self):
self.uobject.bind_event('OnActorBeginOverlap', self.overlap)
self.uobject.bind_key('K', ue.IE_PRESSED, self.pressed_k)
self.uobject.bind_axis('MoveForward', self.move_forward)
def overlap(self, me, other):
ue.print_string('overlapping ' + other.get_name())
def pressed_k(self):
ue.log_warning('you pressed K')
def move_forward(self, amount):
pass
In Starcel, input_devices.py gives more convenient key binding; see main.py for self.input.bind_press(...).
Starcel's own PyActors¶
Content/Scripts/actors/ holds the PyActors Starcel uses: pyactor_plotter, pyactor_table, pyactor_manim, pyactor_latex, pyactor_gizmo, pypawn_drone and more. They are good examples to copy.