When I try to load this resource into a variable in another script like so:
var floor_tile_def: TileDefinition = (preload("res://assets/definitions/tiles/floor.tres") as TileDefinition)
It is not being set correctly (floor_tile_def is null). If I remove the typecasting I get an error saying: Trying to assign value of type 'Resource' to a variable of type 'tile_definition.gd'
If I make the var of type Resource, it appears to load something from the file but I am not sure how to actually extract the data from it. What is the correct way to load custom resources?
And see if this fixes it, or changes the error that you’re getting?
Also, your TileDefinition script looks fine to me, but double check that there are definitely no error messages showing up in the editor for that class, preventing it from being fully loaded and registered as a Resource? One thing that I would expect to cause an error like this would be if the script wasn’t fully loading due to parsing errors.
Changing it to a const causes it to give the error: Parser Error: Assigned value for constant "FLOOR_TILE_DEF" isn't a constant expression.
(I changed it to uppercase to satisfy gdlint)
The brackets are there because in the actual file it is wrapped over a few lines to keep the line length down. I tested it without and the behavior is the same.
It looks like this might be a bug related to custom resource types? I found a GitHub issue with someone else reporting a similar issue to this, who was at least able to work around the error in one case by using load instead of preload:
I am not sure that this is the same issue. The errors they are getting are different and I am definitely only loading each file once. I also didn’t mention this in the OP but someone on discord suggested using load instead of preload but making that change resulted in the same behavior.
Took a bit of fiddling around since I was using the constructor in a few places but it seems like making this change results in it working as expected. What causes this to be the case? Is it a bug or is this expected behavior?
Can you give me an example of what you mean? It is failing as soon as I try to load the file. Or are you saying that I need to load it into a Resource and create an instance of TileDefinition from the base Resource?
Also sorry if multi-posting like this is not the right way to reply to multiple posts at once, I am new and have not used a traditional forum before.
The Engine did call _init() under the hood, but since you are overriding it and expecting parameters (3 in your case), it will cause it to have error when preload trying to initialize it on your game with no parameter
E 0:00:00:0527 _create_instance: Error constructing a GDScriptInstance.
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> modules/gdscript/gdscript.cpp:188 @ _create_instance()
Interesting, is preload effectively manually setting the all the values after calling TileDefinition.new()?
Also are those errors exposed when running the original version after launching the editor in debug mode? It probably would have been helpful if that was surfaced normally.
If i recall correctly, the resource will be initialized first in memory with parameterless _init and then the value will be restored back according what is written in that .tres file