I have a reference to an Actor in python, which implements a custom UInterface.
This interface has some methods I need to call from python, how can I do this?
Here is some example code how I tried it already.
First I get all actors from the scene which implements the interface.
I already checked, but I can’t call the interface function on them directly, so it is necessary to cast the actor, but this doesn’t work, the cast always fails.
actor_array = unreal.GameplayStatics.get_all_actors_with_interface(
unreal.EditorLevelLibrary.get_editor_world(),
unreal.MyCustomInterface)
for actor in actor_array:
try:
interface = unreal.MyCustomInterface.cast(actor)
except:
print('cast failed')
class MyServer(IInterface):
def show(self):
print ‘Hello, World 2!’
class MyBadServer(object):
def show(self):
print ‘■■■■ you, world!’
class MyClient(object):
def __init__(self, server):
if not isinstance(server, IInterface): raise Exception('Bad interface')
if not IInterface.version() == '1.0': raise Exception('Bad revision')
self._server = server
def client_show(self):
self._server.show()
#This call will fail with an exception
try:
x = MyClient(MyBadServer)
except Exception as exc:
print ‘Failed as it should!’
#This will pass with glory
MyClient(MyServer()).client_show()
Thanks for your answer, but I think this is not exactly what I’m searching for.
The interface I’m talking about is defined in C++ and inherits form UInterface.
Besides this I have multiple custom C++ classes which are inheriting from AActor and implementing my custom Interface.