require issue

I am very interested in cocos2d-x js binding. And want to develop a game using this technique.
How ever I have a question about js binding.
I want to build a powerful hotupdate feature for the game. And this feature can be achieved by add or replace some js files in game.
2dx js use requre to include other files.
But since we can’t change any initial files when the game is installed in a mobile device. All updated files can only be stored in a given place(aka Document Folder).
We can make the engine the choose updated file when possible by setting search path in c++. But can we do the same thing to requre command in js?

Well, we’ll export cc.FileUtils.setSearchPaths,getSearchPaths and cc.Director.setDesignResolution to JS soon. Maybe in the next release. :slight_smile:

Thanks, so in current version, I still can call setSearchPaths in c++ code and then require will work?
And I am suffering a problem from change the default hello.js file to main.js.
I just created a js file called main.js in the same directory of hello.js and added this file to my project(xcode). And changed hello.js to main.js in AppDelegate.cpp.
And then the program will encounter a file not found error. Do you have any clue on this problem?

James Chen wrote:

Well, we’ll export cc.FileUtils.setSearchPaths,getSearchPaths and cc.Director.setDesignResolution to JS soon. Maybe in the next release. :slight_smile:

After some test, I found that setSearchPaths is not working for js require(“somefile.js”).
Now I can only require(“jsb.js”), any other files that created by me can’t be found. what’s wrong with it?

  1. I found why it’s not working for require. Xcode by default try to treat .js file as kind of source file and will compile it rather than just copy it as resource.
  2. Still setSearchPaths wont work for require. and if I write code like { require(“test/sample.js”//require a file in some directory }. the code will never work well.
  3. First, I want to solve the problem 2). And that still can not meet my needs. The most advantage of using js binding is that we can treat js code as resources, and update them when needed. So if require() feature can use a mechanism as setSearchPaths, it will really be great. But it seems that require() feature is not a part of cocos2d-x js, it’s a part of spidermonkey. I dont known if it can be achieved.

hammer hammer wrote:

After some test, I found that setSearchPaths is not working for js require(“somefile.js”).
Now I can only require(“jsb.js”), any other files that created by me can’t be found. what’s wrong with it?

in XCode

Choose Build Phases
And Move All js file from Compile Sources to Copy Bundle Resources

Now we can use require(“xxxx.js”)

As Haibo Lin says, it’s necessary to move all js files from compile sources to copy bundle Resources.

I have another solution, copy all the js files on a folder and import this folder on Xcode as a folder reference. But sometimes I need to remove/add this folder, because xcode doesn’t copy all the js files (usually when I add new files, seems like xcode doesn’t detect folder references changes well)