INTRODUCTION
Let’s say I have options screen with sound on/off, music on/off and notifications on/off checkboxes. I store these values in UserDefault database as booleans.
LONG QUESTION
Is it a good practice, to create the separate class, eg. GlobalOptions, which will wrap around these few settings? Then I don’t have to - every time I want to access options - write the key and the default values. Now I can take music value in the MainMenuScene and show a little icon indicating the music on/off state.
TL;DR QUESTIONS
How do you managing your UserDefault/global options in your games?
ADDITIONAL THOUGHTS
I think, that if there is need to reach out for them in more than one scene, it is useful to at least make the keys globally available.
Totally.It’s just how OO should be applied. That way you have a great flexibility. If you want to change the storage backend(E.g. sqlite or a cloud), you just have to adapt your GlobalOptions class without touching the usage of that class.
You could also just use the UserDefault singleton object and use the keys directly, but this would not be good coding practice, as you would need to change every occurrence in your code, once your key changes. It’s a mess and offers no flexibility.
Through a game settings/managment class, just like you implemented.
You can also use a database backend like sqlite or even read/write from/to a cloud.
I could also just use UserDefault and pull out the keys to the separate file (eg. Keys.h). Then there would be no need to use the wrapper class. Or combine these two solutions: have a wrapper class and a file with keys.
Also what makes me wonder is your managment class. Did you implemented it as a singleton?
This would only make sense, if the keys would be used by other classes as well. The keys should be handled by the class, which is using them.
It’s not flexible at all.
If you want to use encryption or other fancy stuff, you would have to duplicate the code at every direct UserDefault call occurrence.
By wrapping it into a management class, you would just need to implement it there, without ever touching any other existing code.
Change it in one place, instead of duplicating the same code all over the place.
This does not give any benefits, if the wrapper is the only class using the keys. Just use code you need and when you need it. Keep code hot, not cold.
Yes, it’s implemented as a Singleton using the same implementation as the cocos2d-x Singletons.
I might switch to an C++11 implementation using a unique pointer in the future.