How I'm planning to fix lag

The lag problem

Swordbattle has been a laggy mess ever since it was released.
And it’s only getting worse with every update.

Now here are the possible things I can do:

  1. Rewrite the game whollly
  2. Rewrite just multiplayer part
  3. Do small optimizations and hope it will fix.

So you know as i am the best and most efficient coder I picked… option 3.

I will be using this thread to talk about optimizations and how it will affect the game.

4 Likes

the first optimization

I will be posting more things here, about lag, but this is the first optimization I’m making.

And it actually might be game-changing.

expected improvements: ping, fps

In swordbattle, every single player is being updated 60 times a second.
Even for a computer, this is a LOT.
Also, a lot of unnecesary data is being sent over the network 60 times a second
Stuff like your skin, name, coins, don’t need to be refreshed tons of times a second.

The first optimization will improve this, by only updating players close to you.
That is, people who are far away from you won’t be updated.
Now for you, the player, you won’t notice anything, because you won’t see them!
But for the computer, this is a lot of strain taken off.

This will improve FPS, because players away from the screen won’t be shown.
It will also improve Ping, because less data is sent over the network causing less lag.

Now, if you ACTUALLY understand what this means, you might ask…
How will the minimap and leaderboard work, if people far away isnt being updated

Good question, i will have to rewrite how the leaderboard and minimap work, but essentially
If a player is far away from you, data about them will still be sent
It will just be much less data, and at a much slower rate.
Instead of sending it 60 times a second, maybe 2 or 3 times a second.

Side effects,
Since the minimap and leaderboard will be updated so slowly, it may look like the minimap is glitching
This is normal, and it can be fixed using interpolation, which is planned for later.

This is my first optimization and I will update here once it’s in the game!
Most of the code for this is finished and so It will be added tonight.
Hopefully this makes a dent in the lag issue.

3 Likes

The lag issue is actually caused by heroku host, their servers arent able to hold over 30 players. If you ever measured the player count rising, you can see the “TPS” starts going down and sometimes it even shuts down the server and restarts.

Around 4 days ago, there were 29 players and the TPS was 16. After a while I got disconnected and player count went to 8 (which means it wasnt only me), and the logs that I am able to see didnt show anything for it shutting down or restarting.

But I do think the first optimization would improve the FPS.

2 Likes

Heres some proof (current)

image

3 Likes

I have a few questions about this.

  1. Are you sure you reconnected to the US server? Sometimes if the sevrer load is too high it switched you into Europe server.

  2. The disconnection issue didn’t happen earlier, it was new after the update. Sure tps would go down but not bad enough to disconnect people. Do you think something changed with heroku or was it the update?

2 Likes

15+ tps is still playable imo after that is when it’s really glitchy

2 Likes
  1. I got disconnected (aka the disconnected screen) and had to reload, but I never connected to europe before.

  2. I am going to guess heroku problem because it always happens in my experience even though the ping is usually 75-100

2 Likes

Alright then

1 Like

wont it effect the minimap

1 Like

It will but it will still be functional, maybe a bit laggy

2 Likes

image

I just realized something stupid.
My socket.io-client (or what is running on everyone’s computer) is version 4.5.1
But… the socket.io what is running on the server is running a really old version from over a year ago.

It was always like this but fixing this might fix it

2 Likes

nice

1 Like

I think a big way to fix optimization would be to not send info to the server for leaderboards as soon as a player gets XP, instead saving it until they die or disconnect. Unless you already do that.

4 Likes

I already do that actually, thanks for the suggestion though!

3 Likes

my next optimization goal

next i’m trying to completely move away from socket.io (current networking library), to pure websockets, which is much more reliable and faster…

note that swordbattle was always built around socket.io, and getting rid of completely won’t be so easy. it might take a week or 2, but this will probably eliminate the disconnection issue (which is now much better after the revert) .

some of my other games (like spicywar) use pure websockets, and it’s worked really well.


now since I reverted the code due to the disconnection issue, the first optimization was actually removed. don’t worrry, since I have all the code for it safe, and once the transition to ws is complete, it will be added.

same goes to the new evols and some of the new statistics features, which will also be added back shortly after the transition. I will update u guys about this transition here.

4 Likes

progress

everything is going MUCH better than expected. today I was able to migrate everything and get swordbattle running locally, fully on websockets

i still need to do some testing on actual servers, and see how better it is compared to socket.io

hopefully update will be out during sometime this weekend


technical details

The way I did it was I created a new class that basically acts as a converter from socket.io to websockets. So when a place in the code does .emit it will take that and change the contents to a JSON string and send’s it.

To the main code there isn’t any difference between socket.io and what I made. All I had to do was rename the import directory and it worked.

I had to do this in the client and the server, and what was especially annoying was socket.io had support for multiple arguments when emitting, but I was able to solve it using the ...args array and rewriting some of the packet listeners.

5 Likes

image

Testing beta update on a private server (hosted on replit) in a school chromebook. Much better results.

Usually school wifi is pretty bad and 62 ping is not bad at all. Also is really smooth aswell.

Now usually the game gets laggier with more ACTUAL players, this server is just with bots.


I am postponing the update to sunday because that’s when the lowest amount of people play so I can quickly fix without affecting many players if something goes wrong.

4 Likes

nice

1 Like

what will be added in the new update besides fixing lag? also good job on that

5 Likes

wait but if each player is updated slowly will throwing swords that go off your screen still work

2 Likes