Cross-language C# class typing in GDScript with Godot 4?

Godot Version

4.2

Question

Godot newbie here. I am experimenting with a project that involves godot-ink, a narrative add-on that is built on C#, but I am writing my Godot 4 project in GDScript. The add-on fortunately supports GDScript fallback methods, and I’ve been using those methods on my project quite successfully thus far.

For example I’ve been able to load an Ink story into my project, and then call its methods from within GDscript, like this:

var story: Object = load("res://MyInkStory.ink")
var latest_text: String = story.ContinueMaximally()

This works great! But I’ve had one problem-- I’m trying to use static typing as often as I can, and I keep getting the following warning when I reference the C# object like this:

The method “ContinueMaximally()” is not present on the inferred type “Object” (but may be present on a subtype).
UNSAFE_METHOD_ACCESS

I’m aware I can ignore warnings, but ideally I’d like to find a way to enable type safety when I’m calling C# methods from GDScript.

I found some historical references online for people attempting type safety within Godot 3 for C# methods… but I tried it, and although it doesn’t break anything in Godot 4, I still receive similar UNSAFE_METHOD_ACCESS warnings:

const InkStory: CSharpScript = preload("res://addons/GodotInk/Src/InkStory.cs")
var story: InkStory = load("res://MyInkStory.ink")
var latest_text: String = story.ContinueMaximally()

The method “ContinueMaximally()” is not present on the inferred type “res://addons/GodotInk/Src/InkStory.cs” (but may be present on a subtype).
UNSAFE_METHOD_ACCESS

Does anyone have suggestions for this specifically with regards to Godot 4? If not, I’ll have to ignore the errors, but as I’m still learning I’d love to adhere to best practices :smiley:

1 Like

There is a pure GDScript implementation of Ink, if you want to avoid cross-language quirks.

You should be able to type it properly (as an InkStory) when using Godot >= 4.1 and GodotInk >= 1.1.0… That is, if I didn’t have fat fingers and didn’t make a typo in the code here. I’ll fix that for the next release.

Although, I’m not sure that there isn’t an actual issue with Godot here. Since, even properly typed, the same warnings show:


Even when, the editor actually autocompletes fine:
image

Try using the actual type for story on line 4, rather than just var. To me that just looks like its having trouble infering the type properly.

@Exerion from what I’ve read inkgd doesn’t fully work with Godot 4+ yet, and I’ve read that the performance is superior with godot-ink-- so I’m attempting to have my cake and eat it too :smiley:

@paulloz ! It’s fantastic to speak to you directly-- I would have inquired about this issue on your Github but didn’t think creating an Issue for my lack of Godot knowledge would be appropriate :slight_smile:

I tried to replicate your example, but I got a different error (not a warning):

Did I perhaps mis-configure something in my project? LMK if there’s something obvious-- as I said, newbie Godot programmer here.

Even assuming I followed your example perfectly, though, with you receiving the same warnings as me even with the type assigned correctly, that doesn’t bode well. I’m guessing should just Ignore the warnings and chalk this up as a Known Issue with godot-ink, unless anyone else has other suggestions!

You’re always welcome to open issues over there. Worst thing that can happen is it getting closed if it’s not something I can sort out.

As I was saying above, my screenshot is after I fixed a typo in the plugin’s code. It won’t work without it. I need to push it and make a release, but won’t be able to for a few days.

1 Like

Thanks @paulloz for the clarity and typo fix! I marked your response as the solution.

For reference, the fact the warning is still raised when the variable is properly typed is related to Make GDScriptAnalyzer aware of properties from other languages by TitanNano · Pull Request #85703 · godotengine/godot · GitHub

This is now fixed on the main branch of the repository. I’ll ping this thread one more time when this is released on the asset library.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.