expected system.random in system.runtime.dll

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Titan

Hello, I’m an experienced C# programmer new to Godot, I just installed Godot and the new .net 5.0
I am running into a strange error while testing some sample projects.
The “pong” sample use a System.Random to bounce the ball, it compile fine but crashes with the error bellow when doing a “new Random()” :

E 0:00:00.917   debug_send_unhandled_exception_error: System.TypeLoadException: Could not resolve type with token 01000010 from typeref (expected class 'System.Random' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  <Erreur C++>  Unhandled exception
  <Source C++>  modules/mono/mono_gd/gd_mono_utils.cpp:424 @ debug_send_unhandled_exception_error()
  • I tested Random in a console App in case there is an issue with my setup and it works as expected.
  • inside Godot, I also tested some other System.Runtime.dll classes to see if everything in System.Runtime.dll would make it crash, but I could instantiate URI, use Math etc inside Godot without issue.

It’s only the Random class inside Godot that is causing a crash so far.

Is Godot doing something special there ? did anyone ever had an issue like this ? I could not find anything helpful. I could work around that with another random library; I don’t even know if my first project will need any random at all, but this is really strange.

I have run into this exact same issue. Would be interested to know if anyone has managed to solve this or found a work around? The inbuilt Godot random number generator works for me in the meantime but I was hoping to reuse my code in other none Godot related projects.

Ethan Shearer | 2021-05-28 02:22

I did not solve this, I just don’t use System.Random.
I was worried that it would happen for other things but it is the only class that caused an issue so far. I was not aware that Godot have its own RNG You could fallback to preprocessor directives for crossengine dev.

Titan | 2021-05-29 09:31

:bust_in_silhouette: Reply From: Ethan Shearer

For me this turned out to be an issue with targeting net5.0. After changing my .csproj to Target Framework net472 or netcore3.2 System.Random seems to work correctly.

You mean this in .csproj <TargetFramework>net472</TargetFramework>?

fhgaha | 2022-06-14 09:12

Yes, although it was a while ago now so things may have changed. But I was getting errors with System.Random and changing .csproj from net5.0 to net472 seemed to solve the issue

Ethan Shearer | 2022-06-29 12:45