20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.74k stars 743 forks source link

Spawning actors out of begin_play #34

Closed Tibo669 closed 7 years ago

Tibo669 commented 7 years ago

Hi,

I'm having an issue when spawning new actors. The code works fine when inside the function begin_play, but when called from an other function it just crashes UE without explanation.

I'm new to UE so maybe I haven't completely understood how it should work...

Here is the code I use :

new_actor = self.ue_object.actor_spawn(ue.find_class('PyActor'), FVector(0, 0, 0), FRotator(0, 0, 90))

Thank you

rdeioris commented 7 years ago

Are you using a threaded build ?

Does the following class (mapped to PyActor) works:

class Spawner:
    def begin_play(self):
        self.uobject.enable_input()
        self.uobject.bind_pressed_key('X', self.spawn)

    def spawn(self):
        new_actor = self.uobject.actor_spawn(ue.find_class('PyActor'), FVector(0, 0, 0), FRotator(0, 0, 90))
Tibo669 commented 7 years ago

Yes I'm using a threaded build.

Your class works fine, it also works fine when I do :

class SuperHero:

    def begin_play(self):
        ue.log('Begin Play on Hero class')
        self.uobject.enable_input()
        self.uobject.bind_key('K', ue.IE_PRESSED, self.spawn)

    def spawn(self):
        ue.log("Spawned")
        new_actor = self.uobject.actor_spawn(ue.find_class('PyActor'), FVector(0, 0, 0), FRotator(0, 0, 90)) 

But UE still crashes when called from an other function inside SuperHero. The log only shows : [2016.10.04-19.24.26:412][452]LogPython: Spawned

rdeioris commented 7 years ago

Remove threading support, unfortunately an optimization we have in the PyActor constructor causes a deadlock. I am still unsure if investing in threading support is a good idea. It introduces lot of performance implications and complexities. I leave the issue opened as the fix is pretty easy (albeit it wil lrequire to disable the optimization when threading is enabled)

rdeioris commented 7 years ago

Latest version removed the python initializiation out of the constructor. This should support object spawning in any part of the code

rdeioris commented 7 years ago

Can we close it ?

Tibo669 commented 7 years ago

Yes thank you for the quick fix. Works fine for me now

UtkarshMoholkar commented 1 year ago

@rdeioris I am using multithreading to Spawn an Blueprint Actor and Unreal Engine is crashing suddenly when the spawn function called.