Godot Version
4.3
Question
I’m using FileAccess to replace content in SVG files to customize cosmetic items before the game starts. Doesn’t work after exporting to html5 though. Are there any alternatives? Here’s the relevant code in my autoload script:
# Load SVG file as text
var svg_path = "res://Assets/Sprites/Spinners/%s.svg" % spinnerName
var file = FileAccess.open(svg_path, FileAccess.READ) # Correctly instantiate FileAccess
if file:
var svg_text = file.get_as_text()
file.close()
# Replace colors for colorMC0
for i in range(len(old_colors_mc0)):
svg_text = svg_text.replace(old_colors_mc0[i], new_colors_mc0[i])
# Replace colors for colorMC1
for i in range(len(old_colors_mc1)):
svg_text = svg_text.replace(old_colors_mc1[i], new_colors_mc1[i])
# Create an ImageTexture from the modified SVG text
var svg_image = Image.new()
svg_image.load_svg_from_string(svg_text) # Load SVG from string
var image_texture = ImageTexture.create_from_image(svg_image) # Create ImageTexture from Image
# Set the image texture for the Sprite2D
var spinner = get_node("/root/Game/Player/Player/Spinner")
if spinner:
print("found spinner node")
spinner.texture = image_texture
print(svg_text)
if spinner.texture == image_texture:
print("texture applied")
var ap3 = get_node("/root/Game/Player/Player/AnimationPlayer3")
var animation = ap3.get_animation("spinner_%s" % spinnerName)
var texture_track_index = animation.add_track(Animation.TYPE_VALUE)
animation.track_set_path(texture_track_index, ".")
animation.track_insert_key(texture_track_index, 0, image_texture)