Annoraaq / grid-engine

A grid based movement engine compatible with Phaser3.
https://annoraaq.github.io/grid-engine/
Apache License 2.0
227 stars 19 forks source link

Dynamically load tilemap for open world games (chunk map loading) #281

Closed jonit-dev closed 1 year ago

jonit-dev commented 2 years ago

Hi everyone!

Thanks for this awesome plugin.

I've been creating a open world MMORPG here and I'm using this engine as my base grid system. It's working great so far.

However, I noticed that apparently there's no way for me to update the tilemap passed upon grid creation.

Is there a workaround to update the tilemap on the fly?

I need this because my plan is to fetch map information not at once, but progressively as needed (chunk based map loading).

Any ideas?

Annoraaq commented 2 years ago

Did you try just calling GridEngine.create(...) again?

It is a bit cumbersome to get all the character data and reassign it. But does it work?

jonit-dev commented 2 years ago

Hi Annoraaq, thanks for the quick reply.

I'll try to create it again. I didn't because this is an advanced technique that will be implemented later and I thought about this bottleneck that may arise.

I hope it doesn't crash, but I'll keep you updated.

jonit-dev commented 2 years ago

Let me know if there're new information regarding it.

Btw, here's the video :)

https://www.youtube.com/watch?v=MDoA2gNlTmA

Annoraaq commented 2 years ago

Great project! It is really interesting to see a way to use GridEngine with a server based game. Let us know if you hit any more obstacles so we can find ways to make GridEngine play well with server based games.

I see your concerns with the bottleneck but before rushing to a solution I think we should observe what the pain points are on a concrete example.

notchris commented 2 years ago

@jonit-dev Can you do something like calculate the current offset of the player from the next part of the world, then use putTilesAt in the corresponding area and remove them from the previous?

jonit-dev commented 2 years ago

That's a good idea. I will explore this approach

notchris commented 2 years ago

@jonit-dev Forgot to mention, there is an official phaser3 chuck example in the sandbox, have you seen that?

jonit-dev commented 2 years ago

@notchris Cool, I didn't know. I'll take a look.

For now, I'm taking a more straightforward approach of splitting the map into different zones and loading it only upon a transition. I'm using the compress-json library to save a copy of my map locally and I compare client vs server hashes to verify if I need to update my map or not.

It's a temporary solution until I implement the chunk-based one, which I expect to be more difficult.

Annoraaq commented 1 year ago

Can I close this issue?

a101010 commented 1 year ago

I'm certainly having this issue. I get exceptions if I try to call GridEngine.create() again. And if I try removing and adding a scene in phaser it doesn't seem to actually remove the old scene, so that doesn't help remove the GridEngine instance on the scene.

a101010 commented 1 year ago

I'll put together an example on github. @jonit-dev, I'm not sure I follow what you mean by zones instead of chunks, so if you can provide a git-hub link or more explanation, it would be valuable. And, @notchris, out of curiosity are you referring to https://phaser.io/news/2016/01/chunkmap or some other chunk tutiorial? That one doesn't look official, but I haven't found the official one yet.

a101010 commented 1 year ago

I've put up a repo at https://github.com/a101010/GridEngineSample Two branches, showing two ways of trying to solve the problem. main tries to just use stop and start, the branch removescene tries to remove and recreate the scene.

Annoraaq commented 1 year ago

Thanks for sharing. I will have a look.

Annoraaq commented 1 year ago

Ok so from what I can see in that example, the error message has the following cause.

First of all, it is not GridEngine.create that is giving you the error. It is not even called a second time because the crash happens before that:

When the player enters a door, you start the scene again, that is probably calling GridEngine's update method automatically (because it is attached to Phaser's update). It then crashes because at the time this happens, the tilemap does not have any layers anymore. GridEngine however, expects the given tilemap to have at least one layer.

While it would probably be a good thing to patch GridEngine in a way, that it does not simply crash (or maybe at least with a better error message), I am not sure if that would solve your problem, because GridEngine on a tile map without a layer does not seem to be very useful.

If you want to avoid GridEngine to crash because of an invalid tile map, you can deactivate it with

game.scene.scenePlugins.stop('gridEngine');

I was able to get rid of the error by calling this right before you call game.scene.start("world", initData);

I think you have to make sure that you deactivate GridEngine as long as your scene is in an "invalid" or transitional state. You should be able to start it again with game.scene.scenePlugins.start('gridEngine');. If that does not help, try calling gridEngine.create() again.

a101010 commented 1 year ago

When I try game.scene.scenePlugins.stop('gridEngine') I get an exception: Uncaught TypeError: Cannot read properties of undefined (reading 'stop') at Function. (world.js:236:33) at initialize.collideSpriteVsSprite (phaser.min.js:1:619441) at initialize.collideHandler (phaser.min.js:1:618810) at initialize.collideObjects (phaser.min.js:1:618284) at initialize.update (phaser.min.js:1:634473) at initialize.update (phaser.min.js:1:612533) at o.emit (phaser.min.js:1:8202) at initialize.step (phaser.min.js:1:225406) at initialize.update (phaser.min.js:1:490723) at initialize.step (phaser.min.js:1:793006)

Which is strange, because it looks like it's game.scene.scenePlugins that isn't defined. I can see a game.scene.game.plugins.scenePlugins, but it doesn't have a stop method defined.

I tried game.scene.plugins.stop('gridEngine'), which is defined, but I still get the same exception about tilemap layer not being defined: GridEngine.min.js:3 Uncaught TypeError: Cannot read properties of undefined (reading 'tilemapLayer') at G.getTileWidth (GridEngine.min.js:3:49003) at G.getTileSize (GridEngine.min.js:3:49344) at G.tilePosToPixelPos (GridEngine.min.js:3:49521) at ot.updatePixelPos (GridEngine.min.js:3:27462) at ot.updateGridChar (GridEngine.min.js:3:28918) at ot.update (GridEngine.min.js:3:27045) at bt.update (GridEngine.min.js:3:58766) at o.emit (phaser.min.js:1:8202) at initialize.step (phaser.min.js:1:225406) at initialize.update (phaser.min.js:1:490723)

Annoraaq commented 1 year ago

Sorry, I checked again and I get the same error. I don't know why I thought it worked for me 😄

I will investigate a little more why this happens and keep you updated here.

Annoraaq commented 1 year ago

A small update: I still could not find out why update is still called on GridEngine (because the scene is stopped and GridEngine is a scene plugin). However, I was able to fix it by fixing GridEngine. In any case I will provide a patch, because it will help in all cases with empty tilemaps and avoids an unnecessary crash of GridEngine. I will let you know here as soon as the patch is released.

Annoraaq commented 1 year ago

I fixed it with just released version 2.25.1.

Please let me know if that solves your issue already.

a101010 commented 1 year ago

Thanks! Will do! A101010

On Thu, Feb 2, 2023, 11:04 AM Johannes Baum @.***> wrote:

I fixed it with just released version 2.25.1.

Please let me know if that solves your issue already.

— Reply to this email directly, view it on GitHub https://github.com/Annoraaq/grid-engine/issues/281#issuecomment-1414074102, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWCQ35M3MPVPODJN3FGXYDWVPSJTANCNFSM5WAEMROQ . You are receiving this because you commented.Message ID: @.***>

a101010 commented 1 year ago

That solved that particular issue. I've got a new one which seems to be related to loading characters after the first iteration which I will open as a separate issue.

Annoraaq commented 1 year ago

Closing this, because I think it is fixed. Let me know if that is not the case.