godot icon indicating copy to clipboard operation
godot copied to clipboard

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

Open mavsm opened this issue 3 years ago • 4 comments

Godot version

v4.0.beta2.official [f8745f2f7]

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

mavsm avatar Oct 08 '22 00:10 mavsm

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.

Regnaris avatar Oct 20 '22 02:10 Regnaris

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.

lemilonkh avatar Mar 05 '23 23:03 lemilonkh

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 avatar Mar 06 '23 15:03 AThousandShips

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: https://github.com/godotengine/godot-proposals/issues/1513 and https://github.com/godotengine/godot-proposals/issues/5899

AThousandShips avatar Mar 06 '23 15:03 AThousandShips

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

qsrenniks avatar Apr 21 '23 07:04 qsrenniks

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

AThousandShips avatar Apr 21 '23 07:04 AThousandShips

Consolidating with https://github.com/godotengine/godot-proposals/issues/5899.

Calinou avatar Jun 19 '23 11:06 Calinou