Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Problem with objects comparison in StoryInit vs Story

edited May 2016 in Help! with 1.x
I've distilled my problem to a simple .twee example:
:: StoryTitle
Title

:: StoryInit
<<set $var1 = {}>>
<<set $var2 = {
	var1: $var1,
}>>
<<set $checkA = ($var1 == $var2.var1)>>

:: Start
<<set $checkB = ($var1 == $var2.var1)>>
<<print $checkA>> /% true %/
<<print $checkB>> /% false %/

What am I doing here is assigning one object ($var1) to a property of another object ($var2.var1).
Then I compare them in StoryInit passage ($checkA) and Story passage ($checkB).
And I'm getting different results for both: $checkA is true and $checkB is false.
Is something happens between init and main phases that changes objects somehow? Or what am I doing wrong?

Comments

  • This will be the capsule explanation, as I lack time.

    Each time a new moment is added to the story history the current variable store is cloned to serve as the basis of the new moment. The newly cloned objects are equivalent, but not equal, to their originals. Furthermore, no attempt is made to preserve referential relationships.
  • There are two issues with what you are trying to do:

    1. Confusing an object reference value with the value of the actual object.

    When you do something like <<set $var1 = {}>> what you are doing is creating an object (stored somewhere) and then assigning a reference to that object's stored location within the $var1 variable. A real world example is the difference between your House (the object) and the Street Address of your house (the object reference).

    When you do something like ($var1 == $var2.var1) you are comparing if the object references (street addresses) are the same, not if the objects are the same value. (same house)

    Comparing if two objects have the same value is not simple, there are many ways to do so with each one being more precise (with less edge cases but more complexity) than the previous. Which you use depends a lot on the nature of objects being compared.

    2. The story format's History sub-system.

    Each time the Reader navigates between passages a copy of all the story's current variables is made and this copy is what is made available to the new Passage. This means that the object that the object reference stored in your variable points to is not the same object, it is a new object that has the same value as the previous one.

    The real world example would be that your street address is the same but someone has replaces your old house with a new one that looks exactly the same (both outside and inside) and that all the items you had within your house have been replaced with exact duplicates as well.

    This is why you can't compare object references across passages.
  • This will be the capsule explanation, as I lack time.

    Each time a new moment is added to the story history the current variable store is cloned to serve as the basis of the new moment. The newly cloned objects are equivalent, but not equal, to their originals. Furthermore, no attempt is made to preserve referential relationships.

    Got it, thank you.
    greyelf wrote: »
    There are two issues with what you are trying to do:

    1. Confusing an object reference value with the value of the actual object.
    ...

    2. The story format's History sub-system.
    ...

    Thank you for full answer. Is there any object-related Twine docs you can recommend to read?

  • cheshire wrote: »
    Is there any object-related Twine docs you can recommend to read?
    It is the Story Format (not Twine) that defines how everything works, so the SugarCube documentation would be a good place to start.
    Most of the object-oriented related functionality is standard Javascript so understanding that would help, although there are SugarCube specific nuances like how Story History works.
Sign In or Register to comment.