touilleMan / godot-python

Python support for Godot 🐍🐍🐍
Other
1.89k stars 143 forks source link

asyncio support #123

Open jkb0o opened 5 years ago

jkb0o commented 5 years ago

First of all, thank you for the great contribution! Amazing job! One of the most strong Godot features is yielding to signal. Especially yielding to complete signal of GDScriptFunctionState.

It would be amazing to have some asyncio support out of the box

  1. Ability to define virtual methods as async methods:
    
    import asyncio

@expose class Player(Node2D):

async def _ready(self):
    print("hello")
    await asyncio.sleep(1)
    print("world")

2. Wrap Godot signals into asyncio.Future at runtime or as part of godot binding
```python
# runtime based wrapping
@expose
class Player(Node2D):

    async def _ready(self):
        print("Hello")
        timer = get_tree().create_timer(2)

        # runtime based wrapping
        await Signal(timer, "timeout")

        # bindings based wrapping
        # timer.timeout here is a getter which returns asyncio.Future
        await timer.timeout
  1. Wrap GDSriptFunctionState into asyncio.Future
    @expose
    class Player(Node2D):
    def _ready(self):
        await get_node("gdscript").async_method()

I'm looking deeper into it, but I didn't touch python for some years.

cr8ivecodesmith commented 4 years ago

One of the most strong Godot features is yielding to signal. Especially yielding to complete signal of GDScriptFunctionState.

I'm currently figuring out a way to do this. Can this be achieved with the current implementation? Use case: I'm trying the "Your first game" tutorial and it makes a yield call to for a timer timeout signal. Is there a workaround you could suggest?