![anne](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-07%2F7398441503681_e91f3064a1cb51221ed8_192.jpg&w=96&q=75)
🍂
Posts tagged with :fallen_leaf:
![anne](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-07%2F7398441503681_e91f3064a1cb51221ed8_192.jpg&w=96&q=75)
![Mourinhom6](/_next/image?url=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F880c5786ade5f753763973b77a4c370f.jpg%3Fs%3D192%26d%3Dhttps%253A%252F%252Fa.slack-edge.com%252Fdf10d%252Fimg%252Favatars%252Fava_0019-192.png&w=96&q=75)
![RobinH-S](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-08-09%2F7541196659495_a8c81e819bf8dbe3809d_192.png&w=96&q=75)
![AkulSaju-U07CPR9AK8B](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-08-21%2F7621475385329_d28b21910c3ce5730a1d_192.jpg&w=96&q=75)
![Tanmayi](/_next/image?url=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb16b56ef49aba8c6b0e1c9f88b5bb5d4.jpg%3Fs%3D192%26d%3Dhttps%253A%252F%252Fa.slack-edge.com%252Fdf10d%252Fimg%252Favatars%252Fava_0022-192.png&w=96&q=75)
![YeGao](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-08-27%2F7635512019397_bfc06a153c4266849881_192.png&w=96&q=75)
![Anson-U07FPDRVCD8](/_next/image?url=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F2d2de7e3ff008e7944da1d0b6b1273e0.jpg%3Fs%3D192%26d%3Dhttps%253A%252F%252Fa.slack-edge.com%252Fdf10d%252Fimg%252Favatars%252Fava_0017-192.png&w=96&q=75)
![Tylar](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-08-01%2F7493870356199_a0c9b15031b33c3cd996_192.jpg&w=96&q=75)
![Jasmine](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-07%2F7398034333025_ac58feb5d165e2532801_192.jpg&w=96&q=75)
![PonderSlime](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-06-29%2F7352530192818_090d1ca605a70673e6b9_192.jpg&w=96&q=75)
![sahitid](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-09-04%2F7676991935589_2517590ecb86fc29e122_192.png&w=96&q=75)
![sahitid](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-09-04%2F7676991935589_2517590ecb86fc29e122_192.png&w=96&q=75)
![TechGuruhacks](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-06-28%2F7369466657392_02de6cd2d9a5e689e3b8_192.jpg&w=96&q=75)
![toonnongaeoy](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-11-23%2F4416166821874_c5a5a9d84397e6bc5af4_192.jpg&w=96&q=75)
![toonnongaeoy](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-11-23%2F4416166821874_c5a5a9d84397e6bc5af4_192.jpg&w=96&q=75)
![Peiprjs](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-05-22%2F5312328065921_c88e163fcaaa58aa8bbc_192.png&w=96&q=75)
![DevIos](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-01%2F7343809880055_ec3ffa29ec1f8be7e628_192.jpg&w=96&q=75)
![adelly13](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-03-17%2F3244580475287_021a0ac91fc930953e37_192.jpg&w=96&q=75)
![adelly13](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-03-17%2F3244580475287_021a0ac91fc930953e37_192.jpg&w=96&q=75)
![JosephDong](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-01-19%2F4671254214178_746d4a35365ae3b422c5_192.png&w=96&q=75)
![Omay](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-11-01%2F6130885221619_d3eee3c35e93a13c22ff_192.jpg&w=96&q=75)
![m04](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-11-11%2F6178769559445_55b844da7f488c369dd7_192.jpg&w=96&q=75)
![kayley](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-01-14%2F6474305106788_c94e8e55ce98d2b486d3_192.jpg&w=96&q=75)
![Snow](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-27%2F7482146300374_769fb33a7a8564269286_192.jpg&w=96&q=75)
![JosephDong](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-01-19%2F4671254214178_746d4a35365ae3b422c5_192.png&w=96&q=75)
![ced](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-10-02%2F4156802062694_de907d4531f5b440522a_192.png&w=96&q=75)
setInterval(() => {
your players move forward, ➡️
your enemies decide who to badger next, :threat:
some of your projectiles are just barely dodged, :flug:
and others smash satisfyingly into their targets :bonk:
}, 1000 / 60);
at each of these intervals, the server decides which of these occur, and which ... do not.
the difference is quite literally life and death for the denizens of your online realm:
if a player's input makes it to the server just after this update has occurred, they might just not move out of the way of the oncoming missile in time.
after one of these updates when the server has just finished making these vital decisions, it blasts relevant portions of them out to everyone who's connected to your server. (there's no point in updating you about something if it's happening on the other side of the map!)
meanwhile, in the player's web browser, these regular updates from the server are gradually applied, so the server's periodic contemplation is rendered as a seamless sequence of fluid movements...
unless those updates aren't really so regular at all ...
the contract that the server has with the client -- that the server will give the client evenly spaced out updates at regular-enough intervals so that the client can smoothly move between them -- isn't actually the same as the contract that setInterval
provides.
all setInterval(update, 1000)
means is that there will be ABOUT one thousand milliseconds between each of your updates, usually more. so if the actual interval ends up being 1009ms one frame, the next update won't be about 991ms later so that updates happen at regular one second intervals, it will be more like 1007ms later, so that now you're a total of 16 ms away from the contract you had with the client. this is known as drift.
at worst, this passes on all of the problems of using setInterval
for animation onto your game. but what if you're doing something fancier than just moving between the updates you get from the server as you get them? there's always going to be a random delay between what the server sends and what you get, anyway. it might make more sense to push the updates you get from the server onto a stack, and exactly as often as the server updates, pop the next set of positions off of that stack.
... except that if you're popping using one setInterval
on the client, and the server is pushing out those simulation ticks using a setInterval
of its own ... and each has a different drift ... they're quickly going to fall out of sync. if the server is drifting a lot, your stack might gradually accumulate thousands of positions that you just can't keep up with ... and if the server is drifting less than you are, you might run out of positions to show the player!
suffice it to say, it's super important to have a setInterval
that updates without drift. I threw together an implementation of one this morning. I call it a `tick`, since it works well for those simulation ticks I was talking about before. To test it, I went ahead and also made a drift visualizer. Here's a screenshot of it! The grey lines are spaced out at 0.5s intervals, and the first row of blue marks is made by setInterval
, while the other row of blue marks are made by my own tick
function. as you can see, they stick to the "wall clock time" a lot better than Interval
does, even when you leave it running for several minutes!
here's a screenshot! I'll post the source in the thread :)
fun fact: everything that moves is a CSS animation 😂
I didn't want to pollute the JS event loop, although now that I think about it, it might've been better to so that I can see how well my ticker keeps itself together in the presence of some event loop pressure.![kayley](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-01-14%2F6474305106788_c94e8e55ce98d2b486d3_192.jpg&w=96&q=75)
![sampoder](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-09-06%2F7695296280244_d32589532d8468f0a234_192.png&w=96&q=75)
![caleb](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-20%2F7439507332679_b489f2a281f679fadc50_192.png&w=96&q=75)
![rajanagarwal](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2023-04-27%2F5194849055457_b30e7c5183da9b1d9acf_192.png&w=96&q=75)
![JohnLins](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2024-07-11%2F7398159902199_4b4d119e8b47f59b421b_192.jpg&w=96&q=75)
![blakedfreeman](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2020-10-09%2F1417905114899_dd232216c28021b15df7_192.png&w=96&q=75)
![christina695](/_next/image?url=https%3A%2F%2Favatars.slack-edge.com%2F2022-02-20%2F3134741640900_e2d0cd8db59585dc51ac_192.png&w=96&q=75)