Godot: C# Unable to write to Dictionary, prints error

Created on 24 Sep 2018  路  5Comments  路  Source: godotengine/godot

Godot version: 3.0.6.stable.mono.official.8314054

OS/device including version: Ubuntu 17.10 artful
Kernal: x86_64 Linux 4.13.0-46-generic

Issue description: An error was printed when I put an entry into a dictionary, and the entry is not set.
The error message is very inconsistent, sometimes it puts Method/Function Failed, returning: Variant() and other times it puts special characters like 

Steps to reproduce:

  • Make a new Project
  • Make a new Scene and add a script to the main node
  • Write in the Ready function:
Godot.Dictionary<int, TestHolder> dict = new Godot.Dictionary<int, TestHolder>();
dict[0] = new TestHolder();
GD.Print(dict[0]);
  • Make a new class
public struct TestHolder
{

}
  • This issue still persists even if you change TestHolder to a class or add fields to it.

Minimal reproduction project: Project.zip

enhancement mono

Most helpful comment

A Godot.Dictionary can only store Variant supported types. It's the same for API methods that receive Variant; the parameter type in C# is System.Object and the user must make sure not to pass something with an invalid type.
This will be changed soon however, and you will be able to store anything where a Variant is expected, so this won't stay as a problem for too long.

I'm surprised about the error messages you mention though, since there is a custom message for this error: https://github.com/godotengine/godot/blob/399910ddd82e84f078ebefb3ed2c51d2ef989235/modules/mono/mono_gd/gd_mono_marshal.cpp#L680-L682

All 5 comments

A Godot.Dictionary can only store Variant supported types. It's the same for API methods that receive Variant; the parameter type in C# is System.Object and the user must make sure not to pass something with an invalid type.
This will be changed soon however, and you will be able to store anything where a Variant is expected, so this won't stay as a problem for too long.

I'm surprised about the error messages you mention though, since there is a custom message for this error: https://github.com/godotengine/godot/blob/399910ddd82e84f078ebefb3ed2c51d2ef989235/modules/mono/mono_gd/gd_mono_marshal.cpp#L680-L682

Hey, it's my brother! Neikeq is correct that it should throw a different error.

@neikeq Assuming we can get the correct error to be thrown, it might be a good idea if the error was a bit more user-friendly. Something like: "The type " + type.type_class->get_name() + " cannot be used with Godot.Collections.Dictionary, only built-in Godot types can be used".

@neikeq looking at what you posted, would there be a reason for DEBUG_ENABLED to not be enabled preventing that error showing up https://github.com/godotengine/godot/blob/5bd0ff003fdd82cf84a557251633ebb83bad4fd5/core/error_macros.h#L93 and like you said line 682 triggers based on the error message they see, but for w/e reason err explain isn't showing anything which would only happen if debug enabled wasn't set right?

Ah, yes. I guess that's the reason. I thought he would be running from the editor.

I updated the minimal reproduction project to Godot 3.2.3: TestProjectBut3.2.3.zip

And I discovered that the correct error message now shows up:

Screenshot from 2020-10-06 15-29-21

I think this issue is resolved now, the error message when trying to perform this invalid operation is now descriptive.

Closing as fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Spooner picture Spooner  路  3Comments

testman42 picture testman42  路  3Comments

RebelliousX picture RebelliousX  路  3Comments

n-pigeon picture n-pigeon  路  3Comments

blurymind picture blurymind  路  3Comments