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

Can't read resource after export #87494

Closed
Misaka-Tang opened this issue Jan 23, 2024 · 4 comments
Closed

Can't read resource after export #87494

Misaka-Tang opened this issue Jan 23, 2024 · 4 comments

Comments

@Misaka-Tang
Copy link

Tested versions

4.2 stable

System information

MacOS 14.2.1

Issue description

I am new guy with using godot and everything all fine when I was develop in IDE to get resource for my sprite node. I am going to change the sprite frame variable when click button. Here are code:

		if item.get_extension()=='tres':
			var petRes = load('res://pets/'+item)
			var petBtn = Button.new()
			petBtn.text = str(item.get_file())
			petBtn.pressed.connect(self._button_pressed.bind(petBtn.text))
			petPool.add_child(petBtn)
			petList[petBtn.text] = petRes

I have tried load(), preload(), ResourceLoader.load() everything fine when test in editor. but after exported the game can't find resource via dir: res:// I don't know why and how can I do?

Steps to reproduce

const PETPATH = 'res://pets'
# dict to store a button and spriteFrame
var petList = {}
@onready var fileDia = $settingPanel/addNewPet/filedialog

# Called when the node enters the scene tree for the first time.
func _ready():
	
	get_tree().root.set_transparent_background(true)
	pet = get_node("Area2D/pet")
	parent = get_node("Area2D")
	setPanel = get_node("settingPanel")
	petPool = setPanel.get_child(2)

	# create a button for each pet option
	for item in DirAccess.get_files_at(PETPATH):
		if item.get_extension()=='tres':
			var petRes = load('res://pets/'+item)
			var petBtn = Button.new()
			petBtn.text = str(item.get_file())
			petBtn.pressed.connect(self._button_pressed.bind(petBtn.text))
			petPool.add_child(petBtn)
			petList[petBtn.text] = petRes

Minimal reproduction project (MRP)

/

@dalexeev
Copy link
Member

dalexeev commented Jan 23, 2024

FileAccess docs:

Note: To access project resources once exported, it is recommended to use ResourceLoader instead of FileAccess, as some files are converted to engine-specific formats and their original source files might not be present in the exported PCK package.

DirAccess docs:

Note: Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources.

See also:

@Misaka-Tang
Copy link
Author

Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources.

Tanks for guidance. I see, but I store my assets by add in project directly instead of coding if I'm misunderstanding.
Here's snapshot, I add resource .tres in this directory.
image
What's meaning of using ResourceLoader to access imported resource?

@dalexeev
Copy link
Member

The documentation suggests that you should use ResourceLoader.exists() instead of DirAccess.file_exists() to check if a resource exists, but there is no equivalent method for getting the file list (see #59334).

As a workaround, you can check .remap files (see #76823) or register your resources in other place (enum, array, text file, etc.).

@Misaka-Tang
Copy link
Author

Thx for #66014 let me know that .tres files are changed after export, which means when in IDE, they were cat.tres but become car.tres.remap after exporting in my case. As for developing I can modify the path contains .remap. But I have no idea why my resource will change name after export? Doing nothing in script.

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

2 participants