Dynamically loaded assets are not exporting

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

Hi, I’m working on a card game, with a certain amount of cards that can change as the development process progresses.
All the card images are in a forlder inside the project, I made a card object which I instance and store dinamically as many times as images I have, and inject the image at creation time using the load() method.

Everything works fine on the editor, but once I Export, the game doesn’t seem to find the images, except the one I manually added to the card object scene as a working reference.

this makes me think that the engine isn’t aware that it should export these resources, I added the .jpg extension an the path/folder/to/images/ to the exporter filters, and made sure that the path to the res folders is correct. how can I export the images to be recognized?

As a side note: when I collect the files using “.jpg.import" in my code’s conditional instead of ".jpg”, it finds the files but can’t iterpret them, which I guess must be normal.

here’s also a bit of the relevant parts of my code. Thank you in advance for your help, I have tried a bunch of things and nothing seems to work.

Collecting the files:

func list_files_in_directory(path:String,extension:String)->Array:
var files:Array = []
var dir:Directory = Directory.new()
dir.open(path)
dir.list_dir_begin()
var safecounter:int = 0
while true and safecounter<500:
	var file:String = dir.get_next()
	if file == "":
		print("safecounter: =" + String(safecounter))
		break
	elif not file.begins_with("."):
		if file.ends_with(extension):
			files.append(file)
	safecounter+=1

dir.list_dir_end()

return files

Loading the data from the filePaths:

func CreateCards()->Array:	
	var tempcardspath:Array = list_files_in_directory(cardsDir,"jpg")#jpg.import
	print(tempcardspath)
	var tempCards:Array = []
	for tempcard in tempcardspath:
		var thisCard:String = tempcard
		var card:Card  = cardObj.instance()
		var imagePath:String = cardsDir+tempcard
		var cardTexture:Texture = load(imagePath)
		var cardID:String = (thisCard.split("_",false,1)[1]).split(".",false,1)[0]
		card.cardID = cardID
		card.cardImage = cardTexture
		tempCards.append(card)
	#emit_signal("card_created")
	return tempCards

paths to card object and to the images:

var cardsDir:String = "res://Art/Cards/MainDeck/"
onready var cardObj:PackedScene = preload("res://Scenes/Card.tscn")

I’m having the same issue as well (godot 3.2.1), I’d be interested to know if you solve it.

Panagiotis Halatsako | 2020-04-24 22:37

:bust_in_silhouette: Reply From: Animatect

OK, Finally found a solution in here:
https://github.com/godotengine/godot/issues/14562

if you do go this way, you’ll probably have to handle part of the code executed in editor and part when exported, for this, you can ise something like:

var isOnBuild:bool = false

if OS.has_feature("standalone"):
	print("Running an exported build.")
	isOnBuild = true
else:
	print("Running from the editor.")

Finally, on the function that collects the paths

if isOnBuild:# exported version
			if file.ends_with('.import'):  
				var file_name = file.replace('.import', '')
				if file_name.ends_with(fileextension):
					file_array.append(file_name)
		else:# in editor version
			if file.ends_with(fileextension):  
				file_array.append(file)

Hope this helps!

That’s what I found as well, although the standalone bit is genious :slight_smile: I found it very late (local time) so I went to get some zees.

From what I can understand, plain load does not have this ability, but you will need ResourceLoader.load to load the resource afterwards.

Panagiotis Halatsako | 2020-04-25 10:19

Ok, taht’s very good to know! I’ll check with ResourceLoader.load to see if it’s possible to skip all the management

Animatect | 2020-04-26 21:04