Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# class not found after playing project #47459

Closed
KuroiRoy opened this issue Mar 29, 2021 · 9 comments
Closed

C# class not found after playing project #47459

KuroiRoy opened this issue Mar 29, 2021 · 9 comments

Comments

@KuroiRoy
Copy link

KuroiRoy commented Mar 29, 2021

Godot version:
I've built Godot myself and I'm currently on master branch bc29f4b

OS/device including version:
I'm on a Macbook x86 running Catalina and using Mono 6.12.0.114.

Issue description:
The Godot application is unable to find the class defined in my script.

I've been debugging the issue using CLion and I think my assembly is missing the AssemblyHasScripts attribute. I think that my class should be loaded in this function CSharpLanguage::lookup_scripts_in_assembly() (csharp_script.cpp line 1198). but it doesn't get through the if statement there. I've tried setting the attribute on my assembly myself by adding this to my csproj <AssemblyAttribute Include="Godot.AssemblyHasScripts" /> but that doesn't work.

After that it can't load my script in CSharpScript::reload() because the script was never added to the dotnet_script_lookup_map.

Steps to reproduce:
Create a new C# script in the Godot editor and write in some print statement.
Press Play current scene.
The error is visible in the Debugger. "Cannot instance script because the class 'NewScript' could not be found. Script: 'res://NewScript.cs'"

Minimal reproduction project:
My testing project: Master1.zip

@Calinou Calinou added this to the 4.0 milestone Mar 29, 2021
@Calinou
Copy link
Member

Calinou commented Mar 29, 2021

@KuroiRoy Can you reproduce this on 3.2.3 or 3.3rc6?

@akien-mga
Copy link
Member

I tested your project in 3.3 RC 6, it seems to fail building with these errors:

.mono/temp/obj/Debug/Master1.AssemblyInfo.cs(13,18): The type or namespace name 'AssemblyHasScriptsAttribute' does not exist in the namespace 'Godot' (are you missing an assembly reference?)
.mono/temp/obj/Debug/Master1.AssemblyInfo.cs(13,18): The type or namespace name 'AssemblyHasScripts' does not exist in the namespace 'Godot' (are you missing an assembly reference?)

This comes from this attribute in Master1.csproj:

    <AssemblyAttribute Include="Godot.AssemblyHasScripts" />

I guess it might be something used in the master branch and not 3.x, so it's probably something that can be debugged only with a master build.

@KuroiRoy
Copy link
Author

I'm sorry! I just found the issue where this is mentioned #46713
After adding this above my class it works [ScriptPathAttribute("res://NewScript.cs")]

Thank you @akien-mga for checking out the issue and @Calinou for your quick responses

@neikeq neikeq removed the archived label Mar 30, 2021
@neikeq neikeq reopened this Mar 30, 2021
@neikeq
Copy link
Contributor

neikeq commented Mar 30, 2021

Not fixed. You shouldn't add those attributes manually. They should be auto-generated.
I'll have a look to see what's happening.

@31
Copy link
Contributor

31 commented Mar 30, 2021

I don't see which version of the .NET SDK being used--is it possible this happened with .NET Core SDK 3.1, because master requires .NET SDK 5.0+ now that it depends on a source generator?

C# source generators just kinda don't do anything if you have them set up in the csproj and build the project with an old SDK. (At least, that's been my experience with mine. I suppose maybe you can grab a newer compiler from a nuget package and make it work with an old .NET SDK.)

Maybe the source generator should add some kind of sentinel class/attribute to show it ran, then the Godot tooling can detect it and tell the user (and add some info that'll make it easier to diagnose these issues)?

@KuroiRoy
Copy link
Author

How would I know which .NET SDK is being used?
From the build output I can tell it's using Microsoft Build Engine 16.6.0+5ff7b0c9e for .NET Core which depends on .NETFramework 4.7.2.

@31
Copy link
Contributor

31 commented Mar 31, 2021

You can open a terminal in the project folder and run dotnet --info for info about what you have installed and what's being used in that particular folder. (In case you have a global.json.)

But, 16.6.0+5ff7b0c9e for .NET Core indicates you're using .NET Core, not .NET Framework. That particular version probably means 3.1.302 based on the table at https://dotnet.microsoft.com/download/dotnet/3.1. (That's also an old version even as far as 3.1 versions go, need to update!) You can get .NET SDK 5.0 from https://dotnet.microsoft.com/download.

Posting your logs would help reduce the guesswork, if you can.

Also feel free to wait for @neikeq, I'm not completely sure I'm right about needing a 5.0 version. 😅

@neikeq
Copy link
Contributor

neikeq commented Mar 31, 2021

Yes, you do need .NET 5 Sdk. I think the MSBuild version needed (the one that comes with this Sdk version) is 16.8.0, but I'm not sure.

@KuroiRoy
Copy link
Author

KuroiRoy commented Apr 5, 2021

Alright that solves it. Thank you!
I'm on .NET 5 now with MSBuild 16.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants