Question about variables in CocosCreator

Hi i notice that some of the component scripts in CocosCreator declares a variable at the top of the scripts… when they want to include other scripts



My question is… is this var global? like is that variable can be recognize to other scripts too?

No it’s not, we use Node module + gulp + browserify in Creator to bundle all js scripts. So every script will be finally wrapped into an anonymous function.

1 Like

In general also, that cannot be global. Since, you’re doing “require” in that file, so it’s available to that file only.

@pandamicro and @catch_up
i have another question how can we make a singleton scripts of our own? like GameManager.instance.doSomething()…

thanks!..

is like we will make a node with the Singleton script and every time we want to access that script we will find that node and take the singleton script?

You can have a module script which defines your singleton object, then require it in other modules.

so i cant just call via class name?

var enemy = EnemyFactory.makeEnemy();

or

var enemy = EnemyFactory.getInstance().makeEnemy();

You may need to first require EnemyFactory before being able to use it

var EnemyFactory = require('path/to/EnemyFactory');

@pandamicro
how about game objects that are watching the game state…
something like game manager or something…
what i understand is that require is only making an instance of a script for the script that requires it…
meaning instances will be different from other scripts with the same require component…

i need something like a singleton that is the same instance for all…
like a static variable

The require will return the same object.

You can take a look at node.js module documentation: https://nodejs.org/api/modules.html

Singleton is a concept unrelated to cocos2d-x!
Try search on google for javascript, on how to use singleton object written in one file been used in other file.

Game code structures is like:

Many approaches:

Better approach for big games:
Make game controller as a scene. After splash screen, menu screen comes and after than your game controller scene will come when actually the game will start. This will take care of all your scores and everything and will be in existence until game over and until player returned to menu screen!! In case if pause screen, we never remove the game controller as we want it’s state to be maintained. So, make Pause screen as another layer in Game Controlller scene or make another scene(less preferred) which is pushed over the scene stack without replacing the game controller scene.

Another approach:
Game Controller class which will come in existence after splash screen and be in existence until player quits the game app. And you can call menu layer, game level layers and whatever you want.

In both case, Game Controller takes care of the score in the game for while game is being played.

In 2nd approach, you already have main game controller scene which will always store game score in a variable you defined. (1) But in 1st approach, you need to see, how can you pass the score value from this game controller scene to Menu scene. Just check it, I forgot, been a while. (2) If you can’t pass arguments from one seen to another then you need singleton.Otherwise, unnecessarily don’t use singleton. (3) Also, if going for singleton, you can try going for global variables.

:smile:

2 Likes

can you show me how to make a Global variable? hehe may need it some how…
i understand your approach… is there a way for us to make a singleton objects like singletons of cc objects…

To create global variables, you need to write them in script tag. And after that they would be available across all the files. Will suggest to put all the global variables in global object rather than stray global variables lying in script tag, so that you would easily identify like global.something Find script tag somewhere in index.html

Note: You can have simple constants.js file having all the game constants which can be accessed anywhere but it can’t work same as global variable. So, may be you can give a try at installing some external library. Or else, yes, you would have to go with wither singleton kind of thing or need to see how to pass arguments from one scene to other.

hehe sorry for the trouble…
i ve been used to unity coding standards… thanks for the help :smile: if someone could make a tutorial about best coding practices for CocosCreator, i think every one will be happy :laughing:

What are the Unity coding standards?

Also, it’s general javascript for which you can learn concepts/standards. It’s not cocos or cocosCreator!

Unity gives you high level of abstraction while writing code along with drag and drop which writes the code on your behalf. So you can drag and drop text box and directly get it’s id and update its value when you need to update score. This is not a standard!! It’s pure abstraction on how actually framework works. But with cocos, you’ll really understand what goes in game programming!

no what i mean by standards is like the use of singletons… interfaces… and abstract classes…
definitely js can do that… but i dont know how cocos creator loads our scripts… like where is it starting and how do it start to load the scripts… like what scripts do he load firsts something like that hehe… i am no pro with this stuffs im just a new bie in game dev world in general not just in cocos2d hehe :blush: