Memory Overhead of Lua Coroutines
Lately I’ve been working on adding NPCs to Sovereign Engine. Sovereign uses Lua 5.4 for server-side scripting, and after playing around with behavior scripts for a while, I’ve settled on a design I like based around Lua coroutines. Essentially, each entity behavior is defined by a Lua script which attaches to an entity (such as an NPC) when it is loaded and creates a new coroutine for that entity. For example, the behavior script for a wandering NPC looks something like this: