Loading an ArrayMesh from a file, and then modifying it but not wanting the modifications to be saved to the file I loaded from

Godot Version

4.3

Question

I am loading a flat mesh from a file, deforming it and using the deformed mesh in my scene. The problem is that the saved ArrayMesh (.res) is being deformed despite me not calling for the file to be overwritten. Does anyone know how I can avoid this?

Important note: All of this is happening in tool scripts so that I can use the editor to my advantage during development of the project.

Thank you for your time!

TLDR; setting the cache mode to CACHE_MODE_IGNORE fixed the problem.

I think I figured it out – if it turns out that I am wrong, I will update this thread since I will definitely know before it times out. Sorry for answering my own question (again). Hopefully this can help someone in the future.

I made the following change to the line where I load the mesh:

# previous
var mesh: LeafPetalSurface = load("res://meshes/LeafPetalMesh.res")


# current
var mesh: LeafPetalSurface = ResourceLoader.load("res://meshes/LeafPetalMesh.res", "", ResourceLoader.CacheMode.CACHE_MODE_IGNORE )

You may want to duplicate the resource before modifying it? Ignoring cache may not always produce this result

var mesh: LeafPetalSurface = load("res://meshes/LeafPetalMesh.res").duplicate()
1 Like

Interesting; thank you for the suggestion! I tried it out and this also resulted in the saved mesh being deformed. :thinking:

I don’t well-understand the cache – if I may ask you to elaborate: why do you say that it may not always produce this result?

Thank you for your time!! :slight_smile:

Wait… I think maybe I made a mistake; this also has fixed the problem! Sorry for the error in my previous reply.

Ignoring the cache isn’t intended to produce a duplicate, it often (maybe always) does because it will read from disk instead of the faster in-memory cache.

Using .duplicate() sounds like it’s exactly what you want, and explictly telling Godot that you want a duplicate so you can freely modify a separate clone of the resource. It should also be slightly faster as it won’t read from disk over and over again.

1 Like

Thank you so much for the detailed reply! That makes a ton of sense.

I really appreciate you taking the time to teach me why it seemed to work before, and why exactly .duplicate is the better way to go!

Cheers!! :smiley:

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