JarkkoPar / Utility_AI_GDExtension

This repository contains the binaries and example project for the Utility AI GDExtension.
MIT License
72 stars 2 forks source link

Is it possible to use the current plugin with c#? #9

Closed ice-ko closed 7 months ago

ice-ko commented 8 months ago

Is it possible to use the current plugin with c#?

JarkkoPar commented 8 months ago

I had to do a bit of research before answering your question, so I'm sorry for the delay. Just now using GDExtension in general from C# is a work-in-progress in Godot. There seems to be some ways of creating C# bindings for GDExtensions, but they seem a bit hacky to me. In GodotSteam GDExtension documentation there's a workaround to use GDScript as glue code between C# and GDExtension. So that could be one way of using this extension, too, until the C# bindings for GDExtensions are in better shape in Godot overall.

JarkkoPar commented 8 months ago

I stand corrected, you can use this with C# even without the bindings!

This is what you need to do, for instance with a Behaviour Tree leaf node, if you want to add a ticking method:

  1. Extend the node with a script as usual.
  2. Change the UtilityAI node class name to be just Node
  3. Add the on_tick method exactly as it would be spelled in GDScript.
  4. When ticking the root node, use the node Call() method: SomeRootNode.Call("tick", this, delta);

On my main test scene, this is how the _PhysicsProcess method looks like where I have the node UtilityAIBTRoot:

        public override void _PhysicsProcess(double delta)
    {
        GetNode("UtilityAIBTRoot").Call("tick",this, delta);
    }

Then on the leaf node:

using Godot;
using System;

public partial class testleaf : Node
{

    public int on_tick(Variant user_data, double delta)
    {
        GD.Print("Test!");
        return -1;
    }

}

And this seems to work just fine! The same way seems to work for State Tree as well and I guess with the Agent Behaviours (I don't have time to test it just now), so as long as you name your methods exactly as they would be named in GDScript, they should work.

ice-ko commented 8 months ago

I stand corrected, you can use this with C# even without the bindings!我纠正了,即使没有绑定,您也可以将其与 C# 一起使用!

This is what you need to do, for instance with a Behaviour Tree leaf node, if you want to add a ticking method:这是你需要做的,例如,如果你想要添加一个滴答方法,那么使用行为树叶节点:

  1. Extend the node with a script as usual.像往常一样使用脚本扩展节点。
  2. Change the UtilityAI node class name to be just Node将 UtilityAI 节点类名更改为仅 Node
  3. Add the on_tick method exactly as it would be spelled in GDScript.添加与 GDScript 中的拼写完全相同的 on_tick 方法。
  4. When ticking the root node, use the node Call() method: SomeRootNode.Call("tick", this, delta);勾选根节点时,使用 node Call() 方法: SomeRootNode.Call("tick", this, delta);

On my main test scene, this is how the _PhysicsProcess method looks like where I have the node UtilityAIBTRoot:在我的主要测试场景中,这是我拥有节点 UtilityAIBTRoot 的 _PhysicsProcess 方法的样子:

        public override void _PhysicsProcess(double delta)
  {
      GetNode("UtilityAIBTRoot").Call("tick",this, delta);
  }

Then on the leaf node:然后在叶节点上:

using Godot;
using System;

public partial class testleaf : Node
{

  public int on_tick(Variant user_data, double delta)
  {
      GD.Print("Test!");
      return -1;
  }

}

And this seems to work just fine! The same way seems to work for State Tree as well and I guess with the Agent Behaviours (I don't have time to test it just now), so as long as you name your methods exactly as they would be named in GDScript, they should work.这似乎很好用!同样的方式似乎也适用于状态树,我想使用代理行为(我现在没有时间测试它),所以只要你完全按照 GDScript 中命名的方法命名,它们就应该有效。

Thanks,