Sandboxes - Handling Infinite Loops

As many of you know, I’m working on a service called RBXMod. To demonstrate its capabilities, I’m planning on making a persistent game world that runs purely on RBXMod and create a pretty interface for it on Roblox. I planning on making a game similar to Screeps, but using Lua instead of JavaScript. (For those unfamiliar with Screeps, it is a persistent MMO RTS whose main feature is that there is almost no way to control your units except by programming them.)

However, I see myself running into an issue. Screeps runs user made scripts in the game loop, allowing malicious or accidental infinite loops to run. I’m not aware of how the Screeps developers handle this (I’ve searched and couldn’t find any information), but I’ve thought of a couple ways to handle it:

  • Create a new thread / context for every script and kill any scripts that don’t finish within a time limit
  • Parsing Lua, inserting checkpoints (this is how CodePen solved the issue), and killing long-running scripts
  • Running scripts asynchronously (another thread / context again), having authors be responsible for the resources they use. (Robux / online payments for additional resources)

I’m leaning towards the last option, but I’d run into the issue of synchronization: how do scripts know what game step is currently running and when the next will be? I’d like to make the API approachable for those learning Lua.

How would you handle infinite loops? Be imaginative, a lot is possible on RBXMod that isn’t on Roblox.

1 Like

If you’re using some sort of interpreter such as Rerubi or LBI, you can check for infinite loops by checking how often a JMP instruction is hit within it and check how long the script has been running for.

To check the game ticks you can probably use something similar, and every instruction or something like that increment a counter.

2 Likes

Ah, yeah, that would be better method than what CodePen does. I think Space Engineers does this for their C# scripts in Programmable Blocks. I’d like to run the scripts in LuaJIT. I’m not familiar with all the details of editing instructions in there, but I’m sure I could figure it out! (If I remember correctly, some loops don’t need to use the JMP bytecode instruction since they have custom instructions in Lua 5.1)

Personally I’m not a big fan of overhead. One of the reasons I’m hesitant to go down this route is that it adds overhead to every if statement, for loop, while loop, repeat loop, and’s / or’s, and any other possible conditional logic branch. This greatly pains my programmer soul, but I’ll do it if need be. :slight_smile: