-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Custom Objects created from C# have the wrong Script resource attached. #48828
Comments
We handle things in a different way for scripts instances from C# rather than from the engine side. I'll have to think how to solve this. |
@neikeq, yeah, seems like a tricky one. |
Oh, possible duplicate of #38191 |
I think this is happening because we're not setting a path for the godot/modules/mono/csharp_script.cpp Lines 2949 to 2980 in 729461b
Reason we're not setting is likely because back then we didn't have a way to get the path for a class, but now we do: godot/modules/mono/csharp_script.h Line 386 in 729461b
godot/modules/mono/csharp_script.h Lines 69 to 72 in 729461b
Not sure if having two |
@neikeq Yeah, I realize that this issue is probably really to get the TODO here done: godot/modules/mono/csharp_script.cpp Lines 2954 to 2955 in 729461b
The CSharpScript object is not free of state. Scripts are Godot Objects and can hold runtime data like with SetMeta and AddUserSignal . Because of that, using a cached version is the "correct" solution to ensure that all objects with that script share and hot-reload that state.
|
I've been meaning to look into the pull request here: #44879 that adds a script server to see if that can be used to complete the TODO. Feel free to beat me to it. |
@neikeq Looking at this again, I realize it wasn't clear in my comment before, but I was responding to your remark here
When I said "The CSharpScript object is not free of state. Scripts are Godot Objects and can hold runtime data like with I would also make a note that having the new script take over the path would just shift the problem. Issues like #38191 would suddenly only happen if the script loaded twice, making for a nasty bug that would trigger randomly and blow away saved resource data willy nillily. |
@neikeq After digging into the code a bit, it looks like RES ResourceFormatLoaderCSharpScript::load(const String &p_path, const String &p_original_path, Error *r_error) {
...
script->set_path(p_original_path);
... As such, I think your temporary fix of setting script path would work fine. Loading scripts from |
Can't reproduce in Godot 4.0 RC 3, but can still reproduce in Godot 3.5.1 so moving to the 3.x milestone. |
Godot version:
v3.3.1-stable_mono_win64
OS/device including version:
Windows 10 64-bit
Issue description:
Any Object declared in C#, then instantiated with
new MyObject()
has an invalid script attached.ResourceLoader uses a resource's
resource_path
as a kind of "cache key" to avoid reloading things. EachScript
resource that is saved to "res://" is usually cached by the ResourceLoader using the script's file path as itsresource_path
. This ensures that the script is only loaded once. The "Minimal reproduction project" below is fairly small and reveals all in the following snippet:The above will result in:
In other words, when any object is created using the form
new CustomNode()
, the script that gets attached is not the one cached by ResourceLoader. Instead it is some detached script object.This has major implications when saving and loading. For example, were I to set both nodes' owners to the main scene node and then pack and save it to 'user://Main.tscn', we get the following tscn:
The issue can be seen by comparing the first
sub_resource
to the firstext_resource
. The desired save result is theext_resource
, which has a path that can be used to load the script and then the object. The firstsub_resource
fails as a "Sub Resource" as it should have below it all the information needed to re-create the object. Indeed, if we try to re-load this scene, we get an empty and invalidCSharpScript
object that contains no real info.Hopefully that clearly highlights the issue with not having the cached version of the script attached.
Steps to reproduce:
OR
Object
, like aNode2D
orResource
.Minimal reproduction project:
https://github.com/cgbeutler/godot_bug_csharp_missing_script_resource_path
The text was updated successfully, but these errors were encountered: