C# scripts not running
Opened this issue · 15 comments
I'm probably missing something obvious but when I attach a C# script to a node and run the project, it doesn't look like the script is actually being run. I tried adding GD.print statements in _Ready and tried calling Rotate on a root Spatial etc but so far I haven't see any effect.
Example code:
using Godot;
using System;
public class TopNode : Spatial
{
public override void _Ready()
{
//GD.print("hi");
//System.Console.WriteLine("hello");
Rotate(new Vector3(1,0,1), 1.1f);
}
}
Is the name of the file TopNode.cs?
No, the filename is lowercase
I changed the filename to TopNode.cs but still the same. So it's supposed to work just like that?
Can you share an example project? That would be the quickest way to find what's wrong.
Sure monotest.zip
The example project had no solution. I made Godot generate it and then I added the file and it's working as expected.
I noticed a few things that must be improved though regarding wise.
- Warnings (AFAIK Godot provides an API for this, so it's possible), for example:
- Node has a script attached to it but the C# script is not part of the C# project, or there is no C# project at all.
- Node has a script attached to it, but there is no class declaration in that file whose name matches the file name (similar to Unity).
There should be a way to add existing C# sources to the csproj from the editor. It should also prompt a list of existing C# sources after generating the solution, asking the user if he wants to add them to the csproj in case they were created before.
Ah sorry I removed those auto-generated files, assuming they'd be same everywhere.
I'm sure it did compile the .cs file before because I did get syntax errors for missing semicolon etc. I'll give it another try later today and attach the complete directory if it doesn't work
That's odd, after creating the .sln file, when I try to add the existing cs file (TopNode.cs, with the class name TopName) it didn't work. But when I created a new file through the add script dialogue, it worked...
A few more things:
- In the add script dialogue, selecting a file disables Class Name input box, but it's contents are apparently important because when it's empty, you get "Invalid Class Name" in red below.
- I can't seem to remove a c# script in the script editor, but I apparently can after I switch to 3D editor.
Also when I run the game, I'm also getting these errors about a script file I just removed:
drivers/unix/file_access_unix.cpp:273 - Method/Function Failed, returning: 0
modules/mono/csharp_script.cpp:1662 - Condition ' err ' is true. returned: err
modules/mono/csharp_script.cpp:1753 - Condition ' err != OK ' is true. returned: RES()
Failed loading resource: res://test.cs
editor/plugins/script_editor_plugin.cpp:673 - Condition ' !rel_script.is_valid() ' is true. Continuing..
There is no "test.cs" is any of the files according to grep, and the error goes away when you restart the editor.
That's odd, after creating the .sln file, when I try to add the existing cs file (TopNode.cs, with the class name TopName) it didn't work. But when I created a new file through the add script dialogue, it worked...
Yeah, need to get notified somehow when the script is added to a file so it can be added to the C# project. I will either check do it from Script::can_instance()
or add a callback to the Godot API.
In the add script dialogue, selecting a file disables Class Name input box, but it's contents are apparently important because when it's empty, you get "Invalid Class Name" in red below.
You should open an issue about this in Godot's repo. This dialog needs other improvements as well, e.g.:
- The
Built-in script
checkbox must be disabled for scripts like C# which do not support it. - Class name must be auto-typed as the file name changes.
- In the case of C# (and maybe other languages), it shouldn't allow to create scripts whose class name is not the same as the file name.
Also when I run the game, I'm also getting these errors about a script file I just removed
It tried to open the file and failed. Not sure if this is a bug, but if it is, then you should open an issue in the Godot repo because the Script API does not control when to load the scripts from the file system.
Sounds great, thanks. I'll file those issues later today
So just to be clear, in C#, does the class name has to match the filename in general? (disclaimer: never really used C#)
@tagcup No. Unlike Java (one public class per file, same name as file), C# has no such rule. You can create many public classes in the same file and they can have any name you choose. It's for this reason that Godot must have some kind of rule to be able to determine which one of those classes represents the script.
In order for a C# file to be usable as a Godot script, this file must declare a class with the same name and this class must derive from Godot.Object. You can find the same rule for MonoBehaviour scripts in Unity.