0 votes
by (160 points)
Hi, relatively new to coding and twine. I've learnt some things over videos, but some don't address the problems I have. I am working with Sugarcube 1.0.35.  on twine 2. So I placed a bunch of objects in an array, but I want to change the attributes of some of the objects as the story goes. How would I change it?

<<set $health = {
    "name" : "Health",
    "value" : "100"
}>>

<<set $stats = [$health, $endurance...etc.]>>

I tried doing something like this, but it didn't seem to work.

<<set $stats[0].value = 5>>  

Also in terms of  advice, is it a good idea to have stats as objects in an array? How would you go about it?

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

warning: The 1.0.35 version of SugarCube is no longer being developed and it is only included with the Twine 2.x application so that older projects can be imported successfully, you should be using the 2.x series of SugarCube instead.

note:
1. In your $health story variable the "value" property is a String value, but in the Array related example you are changing that property to a Number value. If you want to be able to do mathematics with the "value" property then it needs to be a Number from the start.

<<set $health = {
    "name" : "Health",
    "value" : 100
}>>

2. Your <<set $stats[0].value = 5>> macro call is changing the "value" property of Object currently contained within the 1st element of the $stats Array, but if one or more Passage Transitions have occured since your initialised the $stats Array then the "value" property of $heath variable will not be updated.

background:
Each time a Passage Transition occurs the current values of all known story variables are cloned. The original copy of the story variables is associated with the name of the last Passage and persisted as a Moment within the History System. The clone copy of the story variables is made available to the Passage about to be shown.

This cloning process breaks Object Reference integrity, which means that the story variables no longer references the same Objects as they did before. It also means that if two variables were referencing the same object before the Passage Transition then those two variables will be referencing different objects to each other after the Passage Transition.

In you example you have Object referenced by the $heath story variable also being referenced by the 1st element of the Array contained within the $stats story variable. So in the Passage that does the initialisation of the $stats array the following code would output the same values if you references the "value" property either via the $heath variable or via the array.

<<set $stats = [$health]>>

original: via health <<= $health.value>> equals via array: <<= $stats[0].value>>

<<set $stats[0].value = 5>>

changed: via health <<= $health.value>> equals via array: <<= $stats[0].value>>


However if the <<set $stats[0].value = 5>> macro call was placed in a later Passage than the one initialising the $stats array, then the "value" property accessed via the $heath variable would not equal the "value" property accessed via the Array. (note: the following example was written using TWEE Notation)

<<set $stats = [$health]>>

original: via health <<= $health.value>> equals via array: <<= $stats[0].value>>

[[Next Passsage]]

:: Next Passsage
original: via health <<= $health.value>> equals via array: <<= $stats[0].value>>

<<set $stats[0].value = 5>>

changed: via health <<= $health.value>> does not equal via array: <<= $stats[0].value>>


I would need to know more about how the $health and $stats variables are being used before I can state the best way for you to get around this issue. But if I was designing a simple stats structure I would do something like the following.

<<set $stats to {
	"Health": 100,
	"Endurance": 10
}>>

original: Health <<= $stats.Health>>

The monster hit you for 10 damage\
<<set $stats.Health to Math.clamp($stats.Health - 10, 0, 100)>>

changed: Health <<= $stats.Health>>

see: Math.clamp()

by (160 points)
Thanks for the warning. I'll be switching my work to sugarcube 2. I really appreciate your in depth explanation as to why what I'm doing isn't working. I think I will try your suggestion, because to be honest, I'm not sure what I'm doing.
...