JaedanC / GoTeria

A Terraria inspired game built in the Godot Engine.
6 stars 0 forks source link

Can this work for Godot 4+ games with multiplayer and open world chunks? #4

Open WithinAmnesia opened 6 months ago

WithinAmnesia commented 6 months ago

Test chunks link: https://github.com/WithinAmnesia/ARPG/discussions/15

I'm trying to find a way to seamless load and unload chunks for a 2D multiplayer game project to make an open world with a working server using Godot 4.2.1.NET.

How can this work for multiplayer and what is needed for this to potentially work? What options can be used for chunk loading and unloading seamlessly in Godot 4.2.1.NET? Please give feedback.

JaedanC commented 6 months ago

One concept that I was particularly drawn to when considering GoTeria blocks model, is that you have a world say, 10000x6000 blocks, where for each block you need to store information about which block is there, and maybe some other information. In Terraria, this would look this Liquid information, cabling, potentially lighting, walls etc. And so if you needed 4 bytes per block, then that would be:

4 10000 6000 = ~225MB for one map

This is a lot of information. I noted that Terraria saves would an orders of magnitudes smaller than that. I figured they must be performing some compression. And I was lazy, so why write a compression algorithm from scratch when one already existed in Godot... PNG. RGBA information is a PNG can be boiled down to 4 bytes per pixel, and a lossless compression algorithm shrinks that down already. This was one of the ways I made my maps small. I encoded the information into PNGS. If I need more than 4 bytes per pixel, then I could always make two pngs. etc. I would think that sending PNGs over a network would be pretty straightfoward, so as long as you had a smart way of serialising your would into a PNG, then this method would be viable.

But, what I should note here is that I was not expecting that a chunk loading system, actually decreased performance. If you're happy with your program using 225MB of RAM, why serialise the map at all? It would only make sense in a multiplayer setting where you may want to sync chunks between two players and keep the network footprint low. Though, I've never dabbled in Multiplayer code writing, so this would be outside my skillset. The broader point here is make sure you don't preoptimise. Have good abstractions yes! Follow the single responsibility principle and make sure you can swap out your map "loading" code.

Consider the following: Should a sniper bullet in Terraria cause rapid chunk loading and syncing in multiplayer? How are you going to handle collisions that happen off screen in potentially unloaded chunks? Think about answering these questions in your code.

For me, I used PNGs and found too late in development that my chunk-loading pre-optimisation was not helping.

To answer your question if this would work in Godot 4? I don't know I haven't tried it. But code is code and the concepts translate easily,

WithinAmnesia commented 6 months ago

Here is an update since I last posted. https://jonathaneeckhout.itch.io/jdungeon For the goal right now it is easy to play this but imagine it has seamless chunk loading and unloading. As instead of the black world boundary a new chunk loads in seamlessly. What are your suggestions and thoughts?

Update: https://github.com/WithinAmnesia/ARPG/tree/ARPG-Infinite-Worlds Here is the 128x128 with 32x32 pixel tile chunk to test.

My initial testing seems to feel even faster combat with the 128x128 chunk. This 128x128 chunk has the same amount of entities as the 256x256 chunk. For I moved all of the entities over into the 128x128 chunk. This 128x128 chunk is a good test for using the Infinite-Worlds using the BinarySerializer for Godot 4.2+ from Theraot: https://gist.github.com/theraot/31515e28e2d8bfea33f6c6d5bcd852f6 . There needs to be some testing how to make the chunks seamlessly load and unload. https://gamedev.stackexchange.com/questions/209002/looking-for-help-godot-4-multiplayer-seamless-open-world-chunks

WithinAmnesia commented 6 months ago

One concept that I was particularly drawn to when considering GoTeria blocks model, is that you have a world say, 10000x6000 blocks, where for each block you need to store information about which block is there, and maybe some other information. In Terraria, this would look this Liquid information, cabling, potentially lighting, walls etc. And so if you needed 4 bytes per block, then that would be:

4 10000 6000 = ~225MB for one map

This is a lot of information. I noted that Terraria saves would an orders of magnitudes smaller than that. I figured they must be performing some compression. And I was lazy, so why write a compression algorithm from scratch when one already existed in Godot... PNG. RGBA information is a PNG can be boiled down to 4 bytes per pixel, and a lossless compression algorithm shrinks that down already. This was one of the ways I made my maps small. I encoded the information into PNGS. If I need more than 4 bytes per pixel, then I could always make two pngs. etc. I would think that sending PNGs over a network would be pretty straightfoward, so as long as you had a smart way of serialising your would into a PNG, then this method would be viable.

But, what I should note here is that I was not expecting that a chunk loading system, actually decreased performance. If you're happy with your program using 225MB of RAM, why serialise the map at all? It would only make sense in a multiplayer setting where you may want to sync chunks between two players and keep the network footprint low. Though, I've never dabbled in Multiplayer code writing, so this would be outside my skillset. The broader point here is make sure you don't preoptimise. Have good abstractions yes! Follow the single responsibility principle and make sure you can swap out your map "loading" code.

Consider the following: Should a sniper bullet in Terraria cause rapid chunk loading and syncing in multiplayer? How are you going to handle collisions that happen off screen in potentially unloaded chunks? Think about answering these questions in your code.

For me, I used PNGs and found too late in development that my chunk-loading pre-optimisation was not helping.

To answer your question if this would work in Godot 4? I don't know I haven't tried it. But code is code and the concepts translate easily,

Okay so you have a lot of skills related to this? You can look at these puzzles and have start seeing solution in your mind like where to pursue?

It is not easy to find people like yourself. I think that it's important that I work as hard as I can to be open minded and ask and reach out and try to find open minded people in the community. This puzzle will probably need an open minded community to solve. Any feed back or any thing you feel with this demo testing like as this is the test chunk. https://github.com/WithinAmnesia/ARPG/tree/ARPG-Infinite-Worlds Here is the 128x128 with 32x32 pixel tile chunk to test.

Can you look at Theraot's code and like he mentions serializing like its not easy for myself to best use this and like what other solutions should also be considered and pursued? " https://gist.github.com/theraot/31515e28e2d8bfea33f6c6d5bcd852f6 . There needs to be some testing how to make the chunks seamlessly load and unload. https://gamedev.stackexchange.com/questions/209002/looking-for-help-godot-4-multiplayer-seamless-open-world-chunks "

"I would think that sending PNGs over a network would be pretty straightfoward, so as long as you had a smart way of serialising your would into a PNG, then this method would be viable." This, you know things that most people do not myself included. Can you please take a look at the test 128x128 demo chunk and try and try to find any leads or ways to implement multiplayer seamless chunk loading and unloading? I have been trying to reach out and ask as many related skilled people that can be reasonably found and like I found some people and they have similar goals and they want this big puzzle solve with the multiplayer seamless chunk loading and unloading.

I really hope to work with you and like the open minded creative community to find the solutions to the 20+ year old massive puzzle that once solved everyone can make massive multiplayer games forever free and compete toe to toe with the for profit seeking priority legally and creatively limited Unity and Unreal engines world build tools of massive scale.(Unity sucks now lol they ruined it with greed lol, open source is the way to go lol, forever free).

Please give any insights or ideas or suggestions and it would help so much to test this infinite worlds chunk loading systems and I really hope we can find ways to solve this big puzzle. Any feed back is welcome. please help too it so hard to find talented people like yourself. What should we do solve this big puzzle lol?