@derived/smart-loop
v1.2.0
Published
Zero-dependency loop utility for running JS up to 3 parallel update types at configurable rate.
Downloads
12
Readme
SmartLoop
Zero-dependency utility for running JS up to 3 parallel update methods at configurable rate.
📋 Changelog · 📢 Issues and requests
Motivation
Needed zero-dependency isomorphic game loop allowing running render and simulation updates at different configurable rate and support for deterministic lockstep networking.
Features
- Written in TypeScript (includes definitions)
- Created using ES6 generators
- Can be run in both browser or Node
- Electron and NWJS friendly
- Supports up to 3 separate update method types
render()
,update()
,fixedUpdate()
- Maximum 2 update methods when in Node
render()
- Maximum 2 update methods when in Node
- Exposes
render()
method build on the top ofrequestAnimationFrame()
- Uses
setImmediate()
andsetTimeout()
when in Node
Usage
const update = () => { /* render code */ }
const fixedUpdate = () => { /* physics code */ }
const loop = new SmartLoop({ rate: 60, update, fixedRate: 30, fixedUpdate })
loop.start()
setTimeout(() => loop.pause(), 2000)
setTimeout(() => loop.resume(), 3000)
References
Good reads
- High Resolution Time from w3.org
- Game timers: Issues and solutions.
- Fix Your Timestep! by Glenn Fiedler
- Game loop from Game Programming Patterns books
- A Modern C++ Game Loop Template (MIT) on Reddit
GDC Talks
- GDC - Networking for Physics by Glenn Fiedler
- 8 Frames in 16ms: Rollback Networking in Mortal Kombat and Injustice 2 by Michael Stallone
Libraries
- node-microtime (benchmark gist)