+1 vote

I don't know how to solve this structural issue I'm having!
I want to give a user the possibility to select and use, at runtime, a font selected in his file system.
I want to copy the .ttf font file itself inside the "user://" folder and then loading it as a resource (as a font data) at every startup.

I save the font, I load it as a Resource and everything works fine.
My problem is when I already have the font saved in the "user://" folder but now I'm selecting a new font that will override the old one.
A file.remove() gives Error 1 (generic), a dir.copy() gives Error 12 (can't open the file).
Not even the ResourceSaver.save() method worked.
I also know that the problem is probably linked to the fact that Resource loading is different from the file system API itself and that there are automated instances that manages the resources allocated at runtime, but I cannot find any documentation or clue on how to de-allocate the old font resource before overwriting on it in runtime.
I've tried loading the resource without caching and deleting, at runtime, every node that uses that resources (since the Resource itself should be de-allocated automatically as long as every node that contains a reference to it has been freed) but this did not change at all the errors I'm getting.

If I manually delete the file (when the game is not running) and try loading the new font, everything works, so the problem is surely related to the resources not being de-allocated after it has been loaded at startup.

I don't even have the alternative of the file system API since there is no way to read a .ttf file and getting the Resource through those methods.

Any help of any kind? Did you had a problem like this and found a trick or some sort of workaround? At this point I'm totally lost.

in Engine by (20 points)

2 Answers

0 votes
Best answer

I've just solved my problem and I feel so stupid. I forgot to de-reference one forgotten variable that was pointing to that resource in one of the many scripts.
Now the resource gets de-allocated just in time to let the file be overwritten.
I was trying to understand how many dependencies the font resource had but calling ResourceLoader.get_dependencies() always gave me an empty list. Also there is not a good debugging tool for the resource managing monitoring.

by (20 points)
0 votes

HI,
this is just a guess. Have tried to use a softlink to the font? I assume that godot will lock the original file and not the link when opening the font.

#for windows
OS.execute("mklink", ["mySoftLink.ttf", "system/fonts/realFont.ttf"], true)

I havent tried but would like to hear if it works.

by (4,066 points)

Thanks for the answer, but it's not the solution I was thinking about.
I want to make the file usage in the game independent from the font existence on the file system while also keeping everything platform agnostic. At that point I could just change, save and load only the font path, so that any font file would never be overwritten at runtime.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.