0 votes
by (160 points)
Hiya,

I'm using Twine 2.3.1 with Sugarcube 2.28.2. I am quite new to writing and very new to coding but I am hoping someone can help! I have looked on the internet and either it can't be done or i'm being silly.

I am writing an adventure story which has multiple characters, lets say Bob and Bill with each character has traits such as luck.

So lets set bob and bills luck as 7 and 5.

<<set $BobLuck = 7>> <<set $BillLuck = 5>>

I know that Bob's luck is higher so I want to increase his luck so I use a link to make the decision.

[[Add Luck to Bob|AddTrait][$person="Bob";$action="Luck"]]

Now on the trait page and I have a massive list of <<IF>> statements which is hard to read, code and just really messy. there has to be a simpler way.

What I want to do is along the lines of this:

<<set "$" + $person $action = "$" + $person $action + 1>>

I have tried loads of variations of the above but I can't get it to work. If anyone could help me, I would be eternally grateful as it would massively cut down the writing I would have to do!

Many Thanks,

Lana.x

1 Answer

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

You could change your project to use JavaScript Generic Objects to store both the traits for each person as well as all the people in your project.

1. Using Generic Objects to define your people.

/* Initialise the Generic Object being used to store the people. */
<<set $npcs to {}>>

/* Define the generic object being used to store Bob's properties (traits).

	With quoted property names and code formating.
*/
<<set $npcs.Bob to {
	"Luck": 7,
	"Strength": 6
}>>

/* Define the generic object being used to store Bill's properties (traits).

	Without quoted property names, with code formating.
*/
<<set $npcs.Bill to {
	Luck: 5,
	Strength: 8
}>>

/* Define the generic object being used to store Jane's properties (traits).

	Without quoted property names or code formating.
*/
<<set $npcs.Jane to {Luck: 5, Strength: 8}>>

notes:
a. The text between the /* and */ markings is knows as a comment, you can safely remove both the text and the markings.

b. The open & close curly braces { } represent a generic object literal value, the one being assigned to the $npcs story variable represents an empty object.

c. A Generic Object can be assigned one or more 'properties', a property consists of three parts.

  • The Property's Name, this is the value before the colon and this value is generally a String without space characters. Property Names are also knows as Keys.
  • The colon, uses seperate the Property Name from the Property Value.
  • The Property' Value, which can be any valid JavaScript data-type.

d. JavaScript supports using Property Names without quotes when declaring the properties of a Generic Object.

2. Using different 'notation' to access the properties of an Object.

There are two different notations you can use to reference an object's property.

a. Dot Notation, this is what is being used to define each of the people above and looks like the following.

$npcs.Bob.Luck

b. Bracket Notation, this is when you use a Name String value to reference a property,

$npcs["Bob"]["Luck"]


You can use Bracket Notation to solve your issue of being able to generically access both a person and a trait in your AddTrait Passage.

<<set $person to "Bob", $action to "Luck">>

/* Output the person's name, the trait's name, and the trait's value. */
person: $person trait: $action value <<= $npcs[$person][$action] >>

 

by (160 points)
Thank you so much!

Lana.x
...