Error loading resource files in game build in Godot 4

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rogerdv

I have the following code to load some resources in my game (which are items, and abilities):

 func load_items(path:String):	
	var dir =
	if dir:
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir():				
				var item=load(path+file_name)
				if item==null:
					# Load failed
					print("FILE ERROR: cant load item resource ",path+file_name)
			file_name = dir.get_next()

It worked fine while running the project in editor, but when I build and run the game I get this errors:

FILE ERROR: cant load item resource res://game_data/items/weapons/swords/hq_broadsword.tres.remap

ERROR: No loader found for resource: res://game_data/items/weapons/swords/rapier.tres.remap.

at: _load (core/io/resource_loader.cpp:230)

FILE ERROR: cant load item resource res://game_data/items/weapons/swords/rapier.tres.remap

ERROR: No loader found for resource: res://game_data/items/weapons/swords/white_sw.tres.remap.

at: _load (core/io/resource_loader.cpp:230)

FILE ERROR: cant load item resource res://game_data/items/weapons/swords/white_sw.tres.remap

ERROR: No loader found for resource: res://game_data/items/weapons/fist.tres.remap.

Any idea about whats happening here?

:bust_in_silhouette: Reply From: AlexTheRegent

Looks like your files ends with.remap and those files are probably not resource files. Did you added them in Project → Export → Platform → Filter to export non-resource files as *.remap?

Update: turns out this error is related to the next Godot issue: On export, every `.tres` file is renamed to `.tres.remap` · Issue #66014 · godotengine/godot · GitHub. This is more of a feature that a bug. Link contains various workarounds. In this particular case we decided to use next approach: On export, every `.tres` file is renamed to `.tres.remap` · Issue #66014 · godotengine/godot · GitHub, i.e. during loading added next check to filename:

  if '.tres.remap' in file_name: # <---- NEW
    file_name = file_name.trim_suffix('.remap') # <---- NEW

Files are .tres. I have no idea why the .remap was added when exported the project. I created them in the editor, items and abilities extends Resource.

rogerdv | 2023-03-29 18:55

can you try to add *.remap to export to see if it makes difference?

AlexTheRegent | 2023-03-29 19:21

That doesnt solves the problem neither.

rogerdv | 2023-03-29 22:22

whats about files paths? are they all in lowercase?
do you have warnings when you launch game from editor?

AlexTheRegent | 2023-03-30 17:26

Paths are in lowercase, no spaces, no weird characters. I have no warnings when running in the editor, the game runs perfectly, I can use the items/abilities, which means the resource files are being loaded correctly. The only choice I see is to add a check to see if the game is running as a build and not in editor and try to remove the extra .remap from the path.

rogerdv | 2023-03-31 00:55

interesting… what will happen if you add whole folder to export as non-resource, i.e. res://game_data/*? will your error remain the same?

AlexTheRegent | 2023-03-31 17:03

Tried that and get the same error.

rogerdv | 2023-03-31 18:52

how do you load your files? can you share this part of code? also you can add me in discord (AlexTheRegent#8015) for faster communication or we can continue here if you like.

AlexTheRegent | 2023-03-31 19:17

The loading code is the first snippet of code in the question. No more than that, I just scan the directory recursively, and use load() on each file name, then instantiate it. I have sent a friendship request on discord.

rogerdv | 2023-04-01 13:55

Did you guys figure it out? I seem to be having the exact same problem.

Wardius | 2023-04-18 11:53

Yes. The solution was to remove the .remap from file name.

var item=load(filepath.trim_suffix(‘.remap’))

Thanks to Alex for giving me the tip.

rogerdv | 2023-04-18 17:52

Thank you so much! It now works perfectly :slight_smile:

Wardius | 2023-04-19 07:16