real-ezTheDev / GodotEzDialoguePlugin

MIT License
109 stars 10 forks source link

C# Support #7

Open Fireye04 opened 6 months ago

Fireye04 commented 6 months ago

I'm using C# for my project, despite the addon being written in gdscript, and have been successful up until it comes time to implement the dialogue_generated signal, as it recognizes the DialogueResponse as Variant type. This renders the output, as far as I can tell, non-functional.

Is this intended behavior (meaning that the addon is incompatible with C#) or is it a bug? Are there any known workarounds or is a partial C# rewrite necessary to fix the issue?

It is my understanding that the purpose of this addon is to primarily function as a personal tool, and secondarily as a community tool, thus I don't see C# support being high on its priority list. Is there any framework for a potential C# support update being PR'ed to this repo?

real-ezTheDev commented 5 months ago

thanks for cutting this issue. I'll look further into this! since I have not personally tested the usage along with C#.

Metarract commented 5 months ago

hey came across this from the video; you should be able to use .Get("property name") on variants and they should retain props or .Call("method name", args) for methods, i think as long as the variant is an Object type that is (which it is in this case). Resource also retains this, you may have to force the signal to connect but it should be fine. You can validate the items exist as well using things like .HasMethod, .GetPropertyList, etc., though naturally it uses Reflection so it can be slowish.


example: i didn't load up the whole extension myself but i've done a little facsimile of it in the interest of being quick:

image

and if that's sound (which it should be enough), i tested grabbing the info out of it thusly:

public static void Test () {
  // instantiating a test node
  GDScript readerScript = GD.Load<GDScript>("res://Src/Externals/EzDialogueReader.gd");
  Node exampleNode = (Node)readerScript.New();

  // setting up a quick lambda to ensure the custom resource retains its props
  // this'd be whatever func you connected the signal to
  Callable myCallable = Callable.From((Resource dialogueResponse) => {
    GD.PrintT(
      dialogueResponse.Get("text"), 
      dialogueResponse.Get("choices")
    );
  });

  // doin' it (just setting up the signal to call our lambda)
  exampleNode.Connect("dialogue_generated", myCallable);
  exampleNode.Call("test_call");
}

and the output:

image

from there you can use something like dialogueResponse.Get("text").As<string>() (must be variant compat type) to convert to something c#'ll understand easily enough. Naturally conversion from dynamic to static can be a little sketchy but this should do as long as the underlying stuff stays the same.

There's some more info in these here: https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_variant.html#variant-compatible-types https://docs.godotengine.org/en/stable/tutorials/scripting/cross_language_scripting.html https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-get