### 3. The Array Serializer (`runtime.js`)
Finally, update the `saveState()` method in `runtime.js` to serialize arrays
properly so the game persists inventories across reboots.
```javascript
// In runtime.js -> saveState()
saveState() {
const mem = this.vm.memory;
const newStateVars = Object.keys(mem).map(key => {
const val = mem[key];
const type = Array.isArray(val) ? 'ARR' : typeof val === 'number'
? 'NUM' : typeof val === 'boolean' ? 'BOOL' : 'STR';
// Serialize arrays securely using single quotes to bypass the
space-delimiter
const strVal = type === 'ARR' ? `'${JSON.stringify(val)}'` :
`"${val}"`;
return `VAR ${key} ${type} ${strVal}`;
}).join('
');
const originalFile = fs.readFileSync(this.filePath, 'utf-8');
const updatedFile = originalFile.replace(
/```fso-state
[\s\S]*?
```/,
`\`\`\`fso-state
${newStateVars}
\`\`\``
);
if (this.maxBackups > 0) this.rotateBackups();
fs.writeFileSync(this.filePath, updatedFile);
}
```
### The New Capability
With these patches, if Gemma writes:
```fso-code
LBL handle_dig
PULL "RUSTY_KEY"
```
Our engine automatically calculates that the key came from `room_beach` and
went into `INVENTORY`. It dynamically creates the arrays, manipulates the
memory, and saves it to the markdown file cleanly.
Are you ready to test compiling a game that explicitly asks the user to find a
key to open a locked door using the new `PULL` and `HAS` commands?