How to change default file loading location in a game?

I am trying to load a game inside another webpage. The game get loaded to a different index.html file.
Because of this the game is not loading correctly.

When I checked I found that,

cc.assetManager.loadBundle

in “main.js” is trying to load files from wrong location.

Is there a way to set root location or a path of asset manager?

or

Is there a way to configure root location for the game ?

You can customize your own main.js using build template, refer: https://docs.cocos.com/creator/manual/en/publish/custom-project-build-template.html.

In your main.js,you can specify a full path of bundle to load, for example:

cc.assetManager.loadBundle('http://yourserver.com/game/main');

refer https://docs.cocos.com/creator/manual/en/asset-manager/bundle.html#loading-the-built-in-asset-bundle

@SantyWang thank you for your answer.
Based on your answer I tried following modification;

At this time I have relative paths to the bundles.

I have modified following lines in main.js to set the paths.

   var RESOURCES = window.gamePath + "assets/resources"; 
   var INTERNAL = window.gamePath + "assets/internal";
   var MAIN = window.gamePath + "assets/main";

I know the path is correct because I use the same path to load scripts here…

loadScript(window.gamePath + (debug ? 'cocos2d-js.js' : 'cocos2d-js-min.js'), function () {`

When I the new game I am still getting errors like;

GET http://localhost:8888/seven/assets/internal/config.json 404 (Not Found)
GET http://localhost:8888/seven/assets/internal/index.js 
GET http://localhost:8888/seven/assets/resources/config.json
...

I am puzzle by the following script in main.js. It is loading scripts and I haven’t modified it.
Is this the cause of this error?

cc.assetManager.loadScript(settings.jsList.map(function (x) { return 'src/' + x;}), cb);

Can you please provide some assistance to get the assets loading

GET http://localhost:8888/seven/assets/internal/config.json 404 (Not Found)
GET http://localhost:8888/seven/assets/internal/index.js 
GET http://localhost:8888/seven/assets/resources/config.json

Are you sure this is the right path?

You should change it as following

cc.assetManager.loadScript(settings.jsList.map(function (x) { return window.gamePath + 'src/' + x;}), cb);

Are you sure this is the right path?

The path listed below is wrong.

http://localhost:8888/seven/assets/ ...

But “cocos2d-js.js” file does get loaded correctly with following.

loadScript(window.gamePath + (debug ? 'cocos2d-js.js' : 'cocos2d-js-min.js'), function () {

This works. So window.gamePath is correct.

But for some reason when it comes to c.assetManager.loadBundle the path is failing!

Then I tried following

var RESOURCES = window.gamePath  + "ZZassets/resources"; 
var INTERNAL = window.gamePath  + "ZZassets/internal";
var MAIN = window.gamePath  + "ZZassets/main";

“ZZassets” is clearly invalid path.
But still the loader is trying to load assets from, previous location and I get the same error;

GET http://localhost:8888/seven/assets/internal/config.json 404 (Not Found)
GET http://localhost:8888/seven/assets/internal/index.js 
GET http://localhost:8888/seven/assets/resources/config.json
...

I expected to get something like

GET http://localhost:8888/seven/ZZassets/internal/config.json 404 (Not Found)

But I do not. Seems like there is another place the loader is picking up this path.

Can you please provide some help on this ?

Have you check the MD5 Cache option

@SantyWang I have made the build without MD5 Cache option.
Do I have to build with that option ?

I don’t think so.

So what should be the right path?

The correct path should be;

http://localhost:8888/seven/game/web-mobile/assets/internal/config.json

using window.gamePath “cocos2d-js.js” getting loaded correctly. Problem is with the resources.

I think “seven” is the folder where index.html file of the page that load the game is located. As I explained before I am trying to integrate my game to run in external game server.

My index.html file only has

<div id="main" class="content" >
  <canvas id="GameCanvas" oncontextmenu="event.preventDefault()" tabindex="0"></canvas>
</div>

This page get integrated into server’s index.html page.
Files inside “seven/game/web-mobile” get integrated to the web page.

I have successfully get the game loaded on this server with the previous version of Cocos Creator. With the introduction of cc.assetManager.loadBundle this is failing.

Can you please provide some help on this ?

Hi guys, can you provide any help on this?

Can anyone provide some advice on how to solve this issue?

so window.gamePath is http://localhost:8888/seven/game/web-mobile/?

window.gamePath is relative folder path. For example
…/…/Games/seven/blackjack/web-mobile/

This path is correct. And with this path I was able to load the script “cocos2d-js.js”

But it does not seems to work with cc.assetManager.loadBundle

Even if this path was incorrect, based on the test I have done before (change folder to ZZassets) it should print the invalid path in the error message.

Can you provide some help on this?

You can try the absolute path http://localhost:8888/seven/game/web-mobile/

Using the absolute path here has no effect either.
It is still using the old path.

There seems to be another way this path is being picked up.

Can you please provide some assistance to resolve this?

Can anyone provide some advice on this ?

Could you provide a simple demo to help us to reproduce this issue ?

Please download the files from here
https://drive.google.com/file/d/1REjOGhs0AkgHSNDObiEbrOuPIuN66vvL/view?usp=sharing

I am not 100% sure this simulate the exact conditions since we are running our game in external company game server.
But this also have similar problem.

“ResourceTest_example_game_files” folder has the application I build

“ResourceServer” folder I have created a very simple web server and copied the build folder in there.
the node server can be found here;

Or you can use any other server.

What I have done is;

In the root folder of the web server I have index.html file with an iFrame in it.

I am loading the game into the iFrame with the path;

<iframe src="./games/testgame/web-mobile/index.html" height="100%" width="100%" ></iframe>

Inside web-mobile folder I created folder called “Resoruces” and moved assets and src folders into it.

Now the game does not work.

I need a way to inform main.js file, the new location of the resources.

web-mobile.zip (6.3 MB)
I only changed these two files,

and it work
image

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.