Having one objects property completely dependent on another object

Feel like I should know this but…I’m trying to associate the value of one object property to be equal (and dependent) on another objects property. I can set this initially, but as the players rating changes so should the friends. The first print command gives the correct result, the second one is not reflecting the change. What would be the preferred method for doing this short of always updating both at the same time? (using Sugarcube 2.3.14)

<<set $player2 ={name: "Jeff", age: 33, tinafriendship: 42}>>
<<set $tina ={name: "Tina", age: "22", playerrelationship: $player2.tinafriendship}>>

<<print $tina.playerrelationship>>

<<set $player2.tinafriendship +=1>>

<<print $tina.playerrelationship>>

This should work.

<<set $player2 = {name: "Jeff", age: 33, tinafriendship: 42} >>

<<set $tina = {name: "Tina", age: "22", get playerrelationship() { return $player2.tinafriendship; }} >>

<<print $tina.playerrelationship>>

<<set $player2.tinafriendship +=1>>

<<print $tina.playerrelationship>>
1 Like

If you’re intending to add methods/functions to an object that is being stored within a Story Variable then you should read the Non-generic object types (a.k.a. classes) section of the SugarCube documentation. As the variable persistence system of SugarCube needs help knowing how to handle your new custom object type.

2 Likes

Yes that does seem to be an issue Greyelf. Joakin’s script worked perfect while in that passage, but once leaving the passage it no longer would keep updating it.

Jeb, if $tina.playerrelationship should be the same as $player2.tinafriendship, there’s no need to keep track of the same thing twice. Juste select the one you want to keep and drop the other.