-
Notifications
You must be signed in to change notification settings - Fork 748
Calling a deleate from a Unity Coroutine #1471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was closing the engine before the execution could finish This code works: using Python.Runtime;
using System;
using System.Collections;
using UnityEngine;
public class EventTest : MonoBehaviour
{
public enum Mode
{
SimpleCallback,
CoroutineCallback
}
public Mode mode = Mode.CoroutineCallback;
private void OnEnable()
{
PythonEngine.Initialize(mode: ShutdownMode.Soft);
}
private void OnDisable()
{
PythonEngine.Shutdown(mode: ShutdownMode.Soft);
}
void Start()
{
Test();
}
private void Test()
{
using (Py.GIL())
{
PyScope scope = Py.CreateScope();
SetupLogger(scope);
scope.Set("callbackTest", this.ToPython());
var code = string.Empty;
if (mode == Mode.SimpleCallback)
{
code = GetCodeForSimpleCallback();//Working
}
else
{
code = GetCodeForCoroutineCallback();//not working, PyScopeException: object is not a module
}
var pyCompile = PythonEngine.Compile(code);
scope.Execute(pyCompile);
}
}
private string GetCodeForSimpleCallback()
{
string code =
@"
from System import Action
def PrintText(text):
print(text)
callback = Action[str](PrintText)
callbackTest.Test(callback)
";
return code;
}
private string GetCodeForCoroutineCallback()
{
string code =
@"
from System import Action
def PrintText(text):
print(text)
callback = Action[str](PrintText)
callbackTest.TestCallbackWithCoroutine(callback)
";
return code;
}
public void Test(Action<string> func)
{
func("Test");
}
public void TestCallbackWithCoroutine(Action<string> func)
{
StartCoroutine(ICallback(func));
}
private IEnumerator ICallback(Action<string> func)
{
yield return new WaitForSeconds(10f);
func("Test Coroutine Callback");
}
#region miscellaneous
private void SetupLogger(PyScope scope)
{
scope.Set("Logger", this.ToPython());
string loggerSrc =
"import sys\n"
+ "sys.stdout = Logger\n";
scope.Exec(loggerSrc);
}
public void write(string line)
{
Debug.Log(line);
}
#endregion
} |
This looks like it might have been caused by #1489 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Environment
Details
Describe what you were trying to get done.
I trying to create a plugin architecture with pythonnet that allows async callbacks (for example, to wait a server response)
I have been able to pass a delegate from python to c#. Then, the c# code calls the delegate, which points to python again. This is working just fine.
The problem is when I try to do this, through a unity co-routine. When I try that, I get the a PyScopeException: object is not a module (see traceback section)
In summary;
Python -> delegate -> c# -> callback-> python. Working fine
Python -> delegate -> c# -> Unity coroutine -> callback-> python. PyScopeException: object is not a module
What commands did you run to trigger this issue? If you can provide a
Minimal, Complete, and Verifiable example
this will help us understand the issue.
This code reproduces exactly the issue.
Event.issue.mp4
The text was updated successfully, but these errors were encountered: