r/godot • u/McCyberroy • 1d ago
help me Sfx Caching System
To reduce memory usage of preload() and to reduce overhead by constantly loading sfx from disc using load(), I was tasked by programming lead to develop a sfx caching system.
While I kinda understand the purpose and functionality of a cache, I'm unsure if I'm approaching this the right way and would appreciate some opinions/tips.
Here's the current architectural attempt:
- class Sfx
- static func play(parent: Node, stream: String)
- subclass Library
- a bunch of const: String holding stream file paths
- subclass Cache
- static var stream_path: Array[String]
- static var stream_file: Array[AudioStream]
- a bunch of static func for internal logic
Architecture and naming were chosen with syntax in mind. So to play a sfx from anywhere, you'd write Sfx.play(self, Sfx.Library.SFX_SOMETHING)
Cache is operating on 2 Arrays simultaneously. Sfx.Cache.stream_path[0] is the String file path of Sfx.Cache.stream_file[0].
When Sfx.play() is called, it'll be checked whether or not the supplied String file path is inside Cache.stream_path and if it is not, it'll be cached, which basically looks like this: stream_path.append(stream_to_cache) stream_file.append(load(stream_to_cache))
As none of all this exists as an instance in any scene tree, I can't use timers inside Sfx to do cache flushes every once in a while, so I did set up a max cache size and just delete the oldest index when it reaches capacity.
I could set up a timer elsewhere, but I hesitate bc it'd violate LoD.
1
u/megalate 1d ago
I would delete them based on last usage and not just time since it was cashed. I would put a time stamp when they are created/used and overwrite the oldest one when it's full.
4
u/MattsPowers Godot Regular 1d ago
Your programming lead knows that load() is the same as ResourceLoader.load() which is already caching?