Objects Referencing Each Other Creates Infinite Recursion

Twine Version: version 2.3.12
Story Format: Sugarcube 2.34.1

I’m trying to build a system from the ground up that plays a bit more like a text adventure. I’m using some object oriented methodology to create room objects that can reference each other to determine which ways the player can be. I ran into a problem though where once I have two rooms pointing to each other, this is creating an infinite loop/infinite recursion. My best guess is that somehow, the rooms aren’t storing references to one another but creating new instances of each other, or something like that. I’ll put some example code below. If anyone could help, it would be greatly appreciated.

<<set $roomCaveEntrance.ports.northwest = $roomRavine>>
<<set $roomRavine.ports.southeast = $roomCaveEntrance>>

The first line of code depicted works fine on its own. But once I have both, it crashes the entire game because of the looping involved. Any thoughts?

SugarCube stores the history of your game, so every turn it copies all your objects. I’d bet that’s the part that’s crashing, and I don’t believe there’s any way to turn it off (you can limit it to only one history entry but IIRC it still copies?).

Maybe store rooms in an object, and then store the name of the one you’re linking to? $rooms.Ravine.ports.southeast = "CaveEntrance"?

Are these values ever going to change? They sound like you’re setting up a room system, and those rooms don’t sound like generic names, so I’d assume you set it once and then never change the value?

If that’s the case you may want to avoid saving it as a story variable in the first place. Use the setup object (like setup.roomCaveEntrance) and assign it in your Javascript settings/passage so that it’s run every time you start the game.

Doing this will prevent bloating your save files and also prevent the recursion issue you’re having.