You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Understanding LongEventHandler.QueueLongEvent() in RimWorld Modding
🎮 The Problem: UI Stutter During File Operations
When mods save data to disk during gameplay, there's a potential issue: UI stutter. Even on modern SSDs, writing large JSON files can cause a brief hiccup in the game's frame rate. On mechanical hard drives, this is much worse.
🔍 The RimWorld Solution: LongEventHandler.QueueLongEvent()
RimWorld provides a built-in solution for this exact problem:
LongEventHandler.QueueLongEvent(()=>{// Your file saving code hereSaveMyDataToJson();},"RICS_SavingData",// Text shown to usertrue,// Run asynchronouslynull,// Exception handlershowExtraUIInfo:false,forceHideUI:true);
🧠 How It Actually Works
The Magic: Deferred Execution
When you call QueueLongEvent(), RimWorld doesn't execute your code immediately. Instead:
Game continues running - Players keep playing normally
Your code gets queued - Added to RimWorld's internal task queue
RimWorld finds the right moment - Executes when it won't disrupt gameplay
Optional UI feedback - Can show progress bar if you want
The Key Insight
Even when the game is paused (in a dialog), there's still rendering happening:
Key Principle: JSON is only for persistence. All runtime operations use the in-memory Dictionary for performance.
🛡️ Why This Matters for Streamers
Streamers often have:
Chat commands that modify settings in real-time
Viewer interactions during gameplay
No tolerance for stutter (looks unprofessional)
Using QueueLongEvent() ensures:
Smooth gameplay even while saving
No dropped frames on stream
Professional appearance to viewers
🧪 Testing the Difference
You can test this yourself:
// Add to your mod for testing:[DebugAction("Test","Test Sync Save",allowedGameStates=AllowedGameStates.Playing)]publicstaticvoidTestSyncSave(){// Create a large datasetvarbigData=newStringBuilder();for(inti=0;i<10000;i++)bigData.AppendLine($"Line {i}: {newstring('X',1000)}");// ❌ Sync save - watch for stutterFile.WriteAllText("test_sync.json",bigData.ToString());Messages.Message("SYNC save done - did you stutter?",MessageTypeDefOf.NeutralEvent);}[DebugAction("Test","Test Async Save",allowedGameStates=AllowedGameStates.Playing)]publicstaticvoidTestAsyncSave(){varbigData=newStringBuilder();for(inti=0;i<10000;i++)bigData.AppendLine($"Line {i}: {newstring('X',1000)}");// ✅ Async save - smooth gameplayLongEventHandler.QueueLongEvent(()=>{File.WriteAllText("test_async.json",bigData.ToString());},"Saving test data...",true,null);Messages.Message("ASYNC save queued - should be smooth",MessageTypeDefOf.NeutralEvent);}
Q: Why not just use Task.Run() or threads? A: RimWorld's Unity engine has specific threading requirements. LongEventHandler is the approved, safe way to do background work.
Q: What about error handling? A:QueueLongEvent() has an exceptionHandler parameter for proper error handling in the game's context.
Q: When should I NOT use it? A: Don't use it for time-critical operations that must complete immediately (like processing a chat command that needs instant feedback).
This is why RICS saves data the way it does. It's not an oversight - it's a deliberate design choice for performance and user experience.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Understanding LongEventHandler.QueueLongEvent() in RimWorld Modding
🎮 The Problem: UI Stutter During File Operations
When mods save data to disk during gameplay, there's a potential issue: UI stutter. Even on modern SSDs, writing large JSON files can cause a brief hiccup in the game's frame rate. On mechanical hard drives, this is much worse.
🔍 The RimWorld Solution:
LongEventHandler.QueueLongEvent()RimWorld provides a built-in solution for this exact problem:
🧠 How It Actually Works
The Magic: Deferred Execution
When you call
QueueLongEvent(), RimWorld doesn't execute your code immediately. Instead:The Key Insight
Even when the game is paused (in a dialog), there's still rendering happening:
Direct
File.WriteAllText()= Immediate execution = Potential micro-stutterQueueLongEvent()= Deferred execution = Smooth gameplay📊 Real-World Example from RICS
Here's how RICS uses it:
🎯 When to Use It
QueueLongEvent()For🔄 RICS Data Flow Architecture
Understanding this helps explain why RICS is architected this way:
Key Principle: JSON is only for persistence. All runtime operations use the in-memory Dictionary for performance.
🛡️ Why This Matters for Streamers
Streamers often have:
Using
QueueLongEvent()ensures:🧪 Testing the Difference
You can test this yourself:
🎓 The Big Picture
Why RICS uses this architecture:
📚 Further Reading
💬 Discussion
Q: Why not just use
Task.Run()or threads?A: RimWorld's Unity engine has specific threading requirements.
LongEventHandleris the approved, safe way to do background work.Q: What about error handling?
A:
QueueLongEvent()has anexceptionHandlerparameter for proper error handling in the game's context.Q: When should I NOT use it?
A: Don't use it for time-critical operations that must complete immediately (like processing a chat command that needs instant feedback).
This is why RICS saves data the way it does. It's not an oversight - it's a deliberate design choice for performance and user experience.
Beta Was this translation helpful? Give feedback.
All reactions