I work on Twine 2, with story format Harlowe 1.2.3.
I wanted to prepare a few objects, e.g:
(set: $object1 to (datamap: "name", "Object 1", "value", 1))
(set: $object2 to (datamap: "name", "Object 2", "value", 2))
and then I want to create an object which is the reference to one of this previous objects
e.g.:
(set: $currentObject to $object1)
Now everything's ok, when I call : (print: $currentObject's name) the result is "Object 1", when I change: (set: $currentObject to $object2) and call again (print: $currentObject's name) the result is proper: "Object 2".
When I change one of the values of datamap (set: $currentObject' value to 5) and when I change the current object and back to previous one - everything seems to work fine. The problem is when I want to change more than one value of current object.
E.g.:
This works:
(set: $currentObject to $object1) ... (set: $currentObject's value to 5) ... now I can change current object, then back to object1 and the value is changed,
This don't work and I don't know why:
(set: $currentObject to $object1) ... (set: $currentObject's value to 5, $currentObject's name to "New Object's name")
and change object to $object2 and then I back to the object1 - it shows the old, unchanged values.
I don't know why, when a I change 1 value - everything's ok, when I want to change more - it don't work at all.
Comments
1. A 'copy' of the collection referenced by the specified variable is made.
2. The relevant element/property of the 'copy' is assigned the new value.
3. The specified variable is changed to reference the modified 'copy' collection.
This is easily demonstrated by the following test-case:
** For practical purposes I have simplified the technical information, this means that it is not 100% accurate but the end results are the same.
And again, the first "set" is ok, but the second change - is not.
Is there any posibility to change values of original object by reference object.
I'm affraid, in my case, the only option is to change $currentObject and if something has to be changed - check if $currectObject is object1 or object2 or... and then change the original object, but it will make my code much longer.
It is actually the same case because it does not matter which of the two (reference) variables ($map1 or $map1) you use to access the original collection object during the property update the end result is the same, after the update each of those reference variables will point to their own unique collection object.
Not that I know of, for the reasons I stated previously.
Maybe there's something weird with the datamap or rather the references in this script language, which I don't understand but e.g. in Java something like this works without any problems: Now I can swap objects and everything just works.
Generally, I just want to make a game with different locations:
Then I want to allow to change the current location
e.g. (main passage "LOCATION", which is the main game page) On every location I want to search for some supplies (search supplies passage): And now the problem occurs. When I change only the first value (water) - it's ok, but when I change both of the values something strange is happen with the $currentLocation variable - in the main passage (LOCATION) the both links are available, which means $currentLocation is neither $location1 nor $location2. When I change the location the only first values (water) are correct, the second changed value remains as original "0". That's really strange.
Obviously the first time I explained what is happen in Harlowe was not clear enough, so this time I will use more detailed descriptions as well as the same variable names as your example.
1. Initial creation of the datamap collection object and assignment of the two variables. At this point both $location1 and $currentLocation are referencing the same object.
2. Changing the value of the water property of a specified reference variable.
note: It does not matter which reference variable you use, the outcome is the same. The result of the above assignment is:
2a. The water property of the original collection object is updated.
2b. A 'copy' of the updated collection object is made, lets call this non-original for simplicity sake.
2c. The (non-specified) $location1 variable still references the original collection object.
2d. The (specified) $currentLocation variable now references the non-original collection object.
outcome: The two variables are no longer referencing the same object but all the properties of each of the two collection objects appear to have the same values.
3. Changing the value of the food property of a specified reference variable. The result of the above assignment is:
3a. The food property of the non-original collection object is updated.
3b. A 'copy' of the updated non-original collection object is made, lets call this other for simplicity sake.
3b. The (non-specified) $location1 variable still references the original collection object, which was not changed.
3d. The (specified) $currentLocation variable now references the other collection object.
outcome: The food property of each of the two collection objects, the original referenced by $location1 and the other referenced by $currentLocation, now have different values.
Now I understand how it works, more or less.
In my case (if we take my example from previous post), is the only way to change the values on currently visited location something like this (I've tested it and it should work now) ?
First passage (initialisation):
Passage: "NEXT"
It seems to me that now everything works as it should.
Is it the only way to obtain the same (or similar) results? To use $currentLocation more like a temporary datamap and only for checking/printing current locations values and then assign $currentLocation to the right location based on the name.
For now I have no idea how can I improve this or there is any chance to make it better.
Any suggestions?