godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

DirAccess::remove_absolute(const String &p_path) Not working.

Open Fristender opened this issue 4 months ago • 0 comments

Godot version

4.5.dev (ca452113d430cb96de409a297ff5b52389f1c9d9)

godot-cpp version

4.5.dev (e53489bbbe9a655197a41b2ed94b7f5e29ed80d2)

System information

Windows 11, Intel i9 14900K

Issue description

Using DirAccess::remove_absolute(const String &p_path) doesn't delete a .tres ShaderMaterial file.

Steps to reproduce

I've created a plugin repo: https://github.com/Fristender/MatLinkScript. The relevant code is in Matlink.cpp:

//...
void MatLink::create_material(const String &shader_path, const String &new_material_path) {
    UtilityFunctions::print("MatLink::create_material - Starting with shader: ", shader_path);
    UtilityFunctions::print("MatLink::create_material - Output path: ", new_material_path);
    
    // Delete existing material file if it exists to ensure clean overwrite
    if (FileAccess::file_exists(new_material_path)) {
        UtilityFunctions::print("MatLink::create_material - Existing material file found, deleting: ", new_material_path);
        Error delete_error = DirAccess::remove_absolute(new_material_path);
        if (delete_error != OK) {
            UtilityFunctions::print("MatLink::create_material - Warning: Failed to delete existing material file: ", new_material_path, " Error code: ", delete_error);
        } else {
            UtilityFunctions::print("MatLink::create_material - Successfully deleted existing material file");
        }
    }
//...

This code for creating a material first deletes any existing materials with the same file name and file path. You can edit and run a script by double-clicking the .mlnk files in res://MatLinkScripts/, which will open an editor in the top left panel. Here are my findings with the minimal reproduction project below: Scenario 1:

  1. You run res://MatLinkScripts/Create_Material.mlnk first
  2. The shader for the material res://Materials/WeirdShape_Material.tres is set to res://Shaders/text_shader.gdshader, as expected
  3. You want to change the shader to res://Shaders/visual_shader.tres and assign res://Textures/wood_door_01_BaseColor.png to the BaseColor, so you run res://MatLinkScripts/Reassign_Shader.mlnk.
  4. You re-open res://Materials/WeirdShape_Material.tres in the inspector.
  5. You discover that the shader is still res://Shaders/text_shader.gdshader and that there isn't a BaseColor parameter. The fact that the shader isn't changed to res://Shaders/visual_shader.tres is unexpected.
  6. You look at res://Meshes/WeirdShape.fbx and confirm that the shader and shader parameters indeed haven't changed.

Scenario 2: (Re-import res://Meshes/WeirdShape.fbx and uncheck "External Override" for "Material" and delete res://Materials/WeirdShape_Material.tres if you just ran Scenario 1.)

  1. You run res://MatLinkScripts/Reassign_Shader.mlnk first.
  2. The shader for res://Materials/WeirdShape_Material.tres becomes res://Shaders/visual_shader.tres and the BaseColor shader parameter is res://Textures/wood_door_01_BaseColor.png, as expected.
  3. You look at res://Meshes/WeirdShape.fbx and confirm that the shader and shader parameter is working as expected.

I think that the shader not being changed in Scenario 1 is due to erroneous file deletion.

Minimal reproduction project

Github upload always fails. https://gofile.io/d/rVWiFl

Fristender avatar Aug 17 '25 12:08 Fristender