[Sugarcube2] I need help transferring and simplifying a piece of code into sugarcube

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: Twine 2
Story Format: Sugarcube 2.34.1

Hi!
I just started out and as such first played around with the twine default, Harlowe. Now I want to transfer my work into Sugarcube and everything’s going fine except when I wanted to transfer my Quest Journal system.

In Harlowe I have this.

(set: $quest1 to (dm: 
'Name', 'Princess in Trouble', 
'Description', 'The dragon has kidnapped the princess, the kingdom needs their hero!', 
'Objective', 'Save the princess', 
'Reward' , '$10 000 000'))
(set:$quest2 to (dm: 
'Name', 'Terrible Dragon', 
'Description', 'The dragon has kidnapped the princess, the kingdom needs their hero!', 'Objective', 
'Kill the dragon', 
'Reward', '$1 000 000'))
(set: $quest to (a: "(link-repeat: 'quest1')[(replace: ?display)[(print: $quest1)]]", "(link-repeat: 'quest2')[(replace: ?display)[(print: $quest2)]]"))

(align:"=><=")+(box:"X")[(text-style:"bold","emboss","expand")[Quest Journal]]

(if: $quest's length > 0)[\
(for: each _index, ...(range: 1, $quest's length))[(print: $quest's (_index))<br>]
]

|display>[
Description: This is the description 
Objective: This is the objective
Reward: This is the reward]

It worked perfectly, except when I transferred the code into sugarcube I realised the two formats print their datamaps differently, while in Harlowe it was in a nice column format, Sugarcube used arrows and commas which looked bad, so I changed the code to this:

<<silently>><<set $quest1 to {
	Name: 'Princess in Trouble', 
	Description: 'The dragon has kidnapped the princess, the kingdom needs their hero!', 
	Objective: 'Save the princess', 
	Reward: '$10 000 000'
}>>
<<set $quest2 to {
	Name: 'Terrible Dragon', 
	Description: 'The dragon has kidnapped the princess, the kingdom needs you hero!', 
	Objective: 'Kill the dragon', 
	Reward: '$1 000 000'
}>>
<<set $quest to [
"<<link 'quest1'>><<replace #des>><<=$quest1.Description>><</replace>><<replace #obj>><<=$quest1.Objective>><</replace>><<replace #rew>><<=$quest1.Reward>><</replace>><</link>>", "<<link 'quest2'>><<replace #des>><<=$quest2.Description>><</replace>><<replace #obj>><<=$quest2.Objective>><</replace>><<replace #rew>><<=$quest2.Reward>><</replace>><</link>>"]>>

<</silently>><h1 class="quest">Quest Journal</h1>

<<if $quest.length > 0 >>
	<<for _i to 0; _i lt $quest.length; _i++>><<print $quest[_i]>><</for>>
<</if>>

Description: @@#des;This is the description@@ 
Objective: @@#obj;This is the objective@@
Reward: @@#rew;This is the Reward@@

Which works fine but you’ll notice that the array looks ugly and is even more difficult to type out. I want to ask if there’s a better way to do this or if not, how could I make a widget to help things along?

Thanks!

I’d do it more like this in SugarCube:

<<nobr>>
	<<set $quest = []>>
	<<set $quest.push({
		Name: 'Princess in Trouble', 
		Description: 'The dragon has kidnapped the princess, the kingdom needs their hero!', 
		Objective: 'Save the princess', 
		Reward: '$10 000 000'
	})>>
	<<set $quest.push({
		Name: 'Terrible Dragon', 
		Description: 'The dragon has kidnapped the princess, the kingdom needs you hero!', 
		Objective: 'Kill the dragon', 
		Reward: '$1 000 000'
	})>>
	<h1 class="quest">Quest Journal</h1>
	<<if $quest.length > 0 >>
		<<for _i = 0; _i < $quest.length; _i++>>
			<<capture _i>>
				<<link `"Quest " + (_i + 1)`>>
					<<replace #des>><<=$quest[_i].Description>><</replace>>
					<<replace #obj>><<=$quest[_i].Objective>><</replace>>
					<<replace #rew>><<=$quest[_i].Reward>><</replace>>
				<</link>>
				<<if _i < $quest.length - 1>>
					| 
				<</if>>
			<</capture>>
		<</for>>
	<</if>>
<</nobr>>

''Description:'' @@#des; @@ 
''Objective:'' @@#obj; @@
''Reward:'' @@#rew; @@

That way, instead of having numerous variables ($quest1, $quest2, etc…) you just have one $quest variable, which is an array of objects. That way you can easily use a loop to display all of that data. Note that the use of the <<capture>> macro so that the links within that loop still work when the user clicks on them.

The <<nobr>> macro prevents unwanted line breaks for the content within it, though you can use the <br> HTML element if you still want to force a line break.

Just let me know if you have any questions on any of that.

Hope that helps! :slight_smile: