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

Exporting resource whose _init takes a parameter results in resource being null in scene startup #67059

Closed
mavsm opened this issue Oct 8, 2022 · 7 comments

Comments

@mavsm
Copy link

mavsm commented Oct 8, 2022

Godot version

v4.0.beta2.official [f8745f2]

System information

windows 10

Issue description

When attempting to export a resource whose _init function takes a parameter, that resource will be null when running the scene.
An error is also thrown when running the scene; _create_instance: Error constructing a GDScriptInstance.

Im not sure what the expected behaviour should be, but at least the error thrown in this case should be more descriptive, as currently it doesn't even point to a given resource being the issue, its stack trace leading to gdscript.cpp:183.

Steps to reproduce

  1. Open the reproduction project
  2. In the only node in the scene, see that its Res resource is exported and set.
  3. Run the scene with F6, check the resource again in Remote.
  4. See the error reported by the debugger as soon as the scene began.

Minimal reproduction project

ResourceExportTest.zip

@Regnaris
Copy link

Can confirm similar problem for 3.5.1 stable. In my case I was using custom resources in tool script and after reloading editor every resource becomes null for tool scripts.

Seems related to #38222 and somewhat related to #37907

Maybe we need some warning about using _init with arguments, because it can lead to unexpected behavior.

@lemilonkh
Copy link
Contributor

lemilonkh commented Mar 5, 2023

Just encountered this in 4.0 stable. Was really confusing to debug, as the error message in the debugger is just _create_instance: Error constructing a GDScriptInstance. Only figured it out by reading the C++ source and realising that it might be related to my custom _init function that was taking parameters. Would be good to print a more descriptive error message here, since the behavior of having resources that seem valid in the editor suddenly being suddenly null when you run the project is very confusing (I just thought I had hit a bug with Godot 4's custom resource typed array exports initially until trying more types of exports).

Could there be a warning in the GDScript editor instead that classes that extend resource aren't supposed to have constructor arguments in _init?

I have opted to create a static function instance() that creates an instance of the custom resource for now, since that preserves the compact nature of the constructor somewhat. Constructor arguments would be the more intuitive syntax here though in my opinion.

@AThousandShips
Copy link
Member

The class is instantiated in the scene loader, so the function would need default values to work correctly if the _init function should indeed be used.

@AThousandShips
Copy link
Member

AThousandShips commented Mar 6, 2023

I am honestly not sure how nodes and resources with _init functions that cannot be called with zero arguments can be made to work, as they need to be created with no arguments for example when loading and creating scenes, I don't think there's a feasible way to make the engine able to instantiate such types properly, how would it know what arguments to use?

This would be a lot easier to do if there was overloading for the _init function but that is (currently) not possible in GDScript.

Relevant proposals: godotengine/godot-proposals#1513 and godotengine/godot-proposals#5899

@qsrenniks
Copy link

It would be best to at least warn the user that a resource requires a default constructor.

@AThousandShips
Copy link
Member

Yes, as the related proposal godotengine/godot-proposals#5899 does

@Calinou
Copy link
Member

Calinou commented Jun 19, 2023

Consolidating with godotengine/godot-proposals#5899.

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

7 participants