Ninjadoodle Posted August 20, 2018 Share Posted August 20, 2018 Hi @enpu / Panda People I'm still a little bit confused on how to do this correctly. So far, when setting up a variable I wanted to be available in all modules, I just declared it globally in the main module. I understand that this is not ideal and can overwrite other important variables. What is the best way to setup up variables that are available to all modules, and what is the best place to put them (config ?). Thank you very much in advance! Quote Link to comment Share on other sites More sharing options...
pstrejczek Posted August 20, 2018 Share Posted August 20, 2018 @Ninjadoodle - I think it is a good idea to create helper classes. I usually do this myself. I put important variables and functions there (sometimes it is more than one class) and then create an instance of the class in the main module. I keep those classes in a separate module (or modules). This way you will achieve some kind of "static" class. Maybe not the best practice, but it works and helps in code organisation. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted August 21, 2018 Author Share Posted August 21, 2018 Hi @pstrejczek Thank you for the tips Regarding the helper classes, I'm actually doing something like that too, and find it really handy to reuse code like that. The question above is a little different tho. I'm just looking for a place to put a simple variable, that needs to hold state between levels/scenes. For example, I would normally just do a global - soundFX = false - problem is, that doing this is bad practice. I remember @enpu saying somewhere, that you could put it under config, so you don't overwrite something important (I can't find the right post tho). Thank you! Quote Link to comment Share on other sites More sharing options...
Wolfsbane Posted August 21, 2018 Share Posted August 21, 2018 The sokoban template has an example of storing the level number in the config file, if that's the sort of thing you're wanting..? Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted August 21, 2018 Share Posted August 21, 2018 Good and safe way would be to put all your custom variables inside object in game.config. For example game.config.data game.config = { data: { customString: 'test', customBoolean: true, customArray: [0, 1, 2, 3] } }; That would not overwrite anything important and you could access that from all modules. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted August 21, 2018 Author Share Posted August 21, 2018 Hi @enpu @Wolfsbane Thanks heaps guys, that's exactly what I was looking for. Just want to make sure I follow good practices so I don't run into problems later Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.