Remapping resources in an EditorExportPlugin

Godot Version

4.3

Question

I have resources that I want to remap for different exports. For example, I want to remap “res://default.tscn” to “res://mobile.tscn” for mobile exports. I also want to remap audio and texture files.

I have some code that works fine for .tscn files, but it doesn’t work for other resources types, such as textures:

func _export_file(path: String, type: String, features: PackedStringArray) -> void:
	if (features.has("mobile")):

		# Works great for .tscn files
		if (path == "res://default.tscn"):
			add_file(path, FileAccess.get_file_as_bytes("res://mobile.tscn"), true)

		# Does not work for other resource types like this SVG or WAV file:
		if (path == "res://icon-default.svg"):
			add_file(path, FileAccess.get_file_as_bytes("res://icon-mobile.svg"), true)

		if (path == "res://audio-default.wav"):
			add_file(path, FileAccess.get_file_as_bytes("res://audio-mobile.wav"), true)

How should I write this so that it works for any resource type?

Ah, it seems that I should use _customize_resource for non-scene resources and _customize_scene for scenes. This is the full EditorExportPlugin that seems to be working…

@tool
extends EditorExportPlugin

var _res_features: PackedStringArray
var _scn_features: PackedStringArray

func _get_name() -> String:
	return "Resource Remaps Export Plugin"
	
func _get_customization_configuration_hash() -> int:
	return randi() # TODO: This is definitely the wrong way to do this...

func _begin_customize_resources (platform: EditorExportPlatform, features: PackedStringArray) -> bool:
	_res_features = features
	return true

func _end_customize_resources() -> void:
	_res_features.clear()

func _customize_resource(resource: Resource, path: String) -> Resource:
	if (_res_features.has("mobile")):
		if (path == "res://icon-default.svg"):
			return load("res://icon-mobile.svg")

		if (path == "res://audio-default.wav"):
			return load("res://audio-mobile.wav")

	return null

func _begin_customize_scenes(platform: EditorExportPlatform, features: PackedStringArray) -> bool:
	_scn_features = features
	return true

func _end_customize_scenes() -> void:
	_scn_features.clear()

func _customize_scene(scene:Node, path: String) -> Node:
	if (_scn_features.has("mobile")):
		if (path == "res://default.tscn"):
			# Use GEN_EDIT_STATE_INSTANCE because that's what
			# EditorExportPlatform::_export_customize uses to load a packed scene into a Node.
			return (load("res://mobile.tscn") as PackedScene).instantiate(PackedScene.GEN_EDIT_STATE_INSTANCE)
	return null

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.