Jump to content

Global variables / var available to all modules


Ninjadoodle
 Share

Recommended Posts

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!

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...