Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | sygi |
I am building an inventory of items which I’d like to be available globally as a singleton. I created an Item class inheriting from Resource, and InventoryResource as:
extends Resource
class_name InventoryResource
export var items: Dictionary = {}
where I’m planning to put the items available by the user. I will be trying to read the inventory from disk and if it’s not there creating a new one.
Apart from this, I would like to have a couple of functions for interacting with the items (adding items, emitting signals). I see two options of doing this:
-
add the logic to the InventoryResource class. This way the logic will be living close the the data (good), but I’ll need to have the loading code in a separate singleton class (as resources can’t be autoloaded) or having the loading logic repeated in every place that needs inventory access. I also don’t like the fact that I’ll need to explicitly load the inventory everywhere and am a bit worried whether the inventory logic will be stored (and reloaded) together with data or not and if this will create multiple copies of the inventory in memory.
-
Keep InventoryResource data only and put the logic (including loading logic) into a separate, autoloaded class. This way I have a single way for accessing the inventory, but the inventory logic is kept separate from the data.
Do you have any opinion on which option is preferable? Is there some better way to do this that I missed?
I saw this related question, but it’s on how to technically make it work, and I wonder more about the code design.