0

Blender ゲーム エンジンのマルチスレッド python スクリプトに少し問題があります。問題なく動作しますが、ゲームを停止すると、いくつかの例外が発生し、クラッシュすることもあります。

from bge import logic
import time
from threading import Thread

def init():

    if not hasattr(logic, 'init'):
        logic.init = 0
        logic.thread = new()
        logic.thread.start()

    logic.thread.restart()

class new(Thread):
    def __init__(self):
        self.Thread = Thread
        self.Thread.__init__(self)
    def run(self):
        number = 0
        while 1:
            number += 1
            print(number)
            try:
                main()
                time.sleep(0.1)
            except:
                break

    def restart(self):
        self.Thread.__init__(self)

def main(): #this part isn't important now ...
    cam = bge.logic.getCurrentScene().active_camera
    obj = bge.logic.getCurrentController().owner
    obj.worldPosition.x = cam.worldPosition.x
    obj.worldPosition.y = cam.worldPosition.y

コンソールは次のように書いています。

Unhandled exception in thread started by <bound method new._bootstrap of <new(Th
read-80, initial)>>
Traceback (most recent call last):
  File "C:\Program Files (x86)\Blender Foundation\Blender\2.64\python\lib\thread
ing.py", line 709, in _bootstrap
    self._bootstrap_inner()
  File "C:\Program Files (x86)\Blender Foundation\Blender\2.64\python\lib\thread
ing.py", line 784, in _bootstrap_inner
    with _active_limbo_lock:
AttributeError: __exit__

誰かがそれの何が問題なのかを見つけられたら、私はうれしいです。ありがとうございました

4

1 に答える 1

2

これは Blender での Python スクリプト作成のよく知られた制限です

問題は、Blender がスレッドの前に Python を破棄することです。あなたがしようとすることができることは、何らかの形で Blender (またはあなたのゲーム) が終了していることを登録し、あなたのスレッドに通知joinし、メインスレッドからそれを通知することです。

于 2013-02-10T18:46:57.307 に答える