Skip to content

derived-studio/smart-loop

Repository files navigation

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()
  • Exposes render() method build on the top of requestAnimationFrame()
  • Uses setImmediate() and setTimeout() 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

GDC Talks

Libraries