Something weird happens my variable's reference when I move from one passage to the other

0 votes
asked Jan 9 by CeLezte (400 points)

Hi,

I come to you with another problem that I'm having and I'm not sure if this is something that Twine/SugarCube does or it is just me failing.

This problem is loosely tied to:

https://twinery.org/questions/3252/how-to-load-a-saved-game-and-restore-its-state-in-sugarcube2?show=3252#q3252 

But this time it's not about saving and loading.

I decided to use both uids and iuids to identify items in the players inventory.
Right now I'm working on a way to equip and unequip items. When I equip an item I simply set a variables inside the player's inventory called isEquipped and assign
the reference of the equipped item to a slot on the players clothing like this:

inventory_item.isEquipped = True
player.clothing.shirt = inventory_Item

this works flawlessly up to this point, since now I can run:

SugarCube.State.variables.player.inventory.items.find(function(item){return item === SugarCube.State.variables.player.clothing.shirt})


This means that:
player.clothing.shirt.iuid and inventory_item.iuid (iuid - unique inside current inventory) are now equal
as well as player.clothing.shirt.uid and inventory_item.uid (uid - is unique globally).
This is a behavior that I want. I also want player.clothing.shirt to always point to an item that is inside player.inventory.items.

So the last code snippet gives me the item back that I just assigned when I equipped it.

My problem starts when I move my character to the next passage or just simply refresh my inventory list.

For some reason player.clothing.shirt will have a brand new unique ID and player.clothing.shirt will not be possible to find inside tha player's inventory.

I would like to know if this is something that Twine/SugarCube does on its own or is there something that I'm doing wrong here?

Somehow my clothing.shirt gets reassigned to a different object. What is weird to me is that the clothing slot will reference something very different that I can't find anywhere in my game.

Could anyone help me figure this out?

I could of course bypass this problem easily by creating a new connection each time I move to a new passage, but I find that messy and I'd really like to know what is going on here under the hood.

1 Answer

0 votes
answered Jan 9 by CeLezte (400 points)
 
Best answer

Solved, thanks to ChapelIR:

Probably the fact that every new passage gets a clone of the variable store.Which breaks object references. It enables the back and forward history navigation features. So you generally get around it by passing around the object's identifier rather than the object itself. Messy but works.

Here's a big long thing about it: https://www.reddit.com/r/twinegames/comments/6i78da/problems_with_arrays_and_objects_sugarcube/

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...