0 votes
by (2.3k points)
edited by

Further to the earlier answer here:

http://twinery.org/questions/40354/inventory-conundrum-sugar-cube-2-21

EDITED.

Choose a Character Class

<<nobr>>
<<set $choices to [
"Forward Observer",
"Technologist",
"Man-At-Arms",
"Insider",
"Rover",
"Yeoman",
]>>

<<set $pcClass.classDescription to []>>

<<for _i to 0; _i lt $choices.length; _i++>>

<<capture _i>>
<<linkreplace "$choices[_i]">>
	<<set $pcClass.classDescription.push($choices[_i])>>
	<<if $pcClass.classDescription.length gte 1>><<goto "Custom Character Creation 2">><</if>>
<</linkreplace>>
<</capture>>
<br>
<</for>>
<</nobr>>

I have altered the code for one choice only correctly I think, but the following part I must have to match the inventory code:

$playerClass.classDescription

Using this, instead of just <<$playerClass.push>> and also  <<$playerClass.length>> is causing errors, saying it is a bad evaluation/cannot read etc.

I'm not sure of the intricacies of this one, as normally the variable doesn't have the extra <<.classDescription>> but I need it to have this otherwise it will not match the inventory code etc.

What wizardry am I miscasting here?

1 Answer

0 votes
by (23.6k points)

In your above example you are first setting up $pcClass.classDescription then get confused and refer to it as $playerClass.classDescription. That might be your problem. Also since this would refer to an array, you'd have to say:

$pcClass.classDescription[0]
$pcClass.classDescription[1]

 

by (2.3k points)

I've just edited it.

For setting the array like this or close to this??:

Choose a Character Class

<<nobr>>
<<set $choices to [
pcClass.classDescription [0]: "Class 1",
pcClass.classDescription [1]: "Class 2",
]>>


<<set $pcClass.classDescription to []>>

<<for _i to 0; _i lt $choices.length; _i++>>

<<capture _i>>
<<linkreplace "$choices[_i]">>
	<<set $pcClass.classDescription.push($choices[_i])>>
	<<if $pcClass.classDescription.length gte 1>><<goto "Custom Character Creation 2">><</if>>
<</linkreplace>>
<</capture>>
<br>
<</for>>
<</nobr>>

???

by (23.6k points)

No  -when I said:

$pcClass.classDescription[0]
$pcClass.classDescription[1]

That's just for printing the end result.

Your initial setup should have worked. It was just that you later used the wrong name.

by (2.3k points)
edited by

Right, I'm a bit confused, maybe I wasn't clear enough.

I have to use the:

$pcClass.classDescription

In order to match my inventory code.

I cannot get the current method to tie in with that:

Choose a Character Class

<<nobr>>
<<set $choices to [
"Class1",
"Class2",
]>>

<<set $pcClass to []>>

<<for _i to 0; _i lt $choices.length; _i++>>

<<capture _i>>
<<linkreplace "$choices[_i]">>
	<<set $pcClass.push($choices[_i])>>
	<<if $pcClass.length gte 1>><<goto "Custom Character Creation 2">><</if>>
<</linkreplace>>
<</capture>>
<br>
<</for>>
<</nobr>>

 

by (23.6k points)

StoryInit:

<<set $pcClass = {
classDescription: [],
}>>

<<set $choices to [
"Class1",
"Class2",
]>>

In the passage:

Choose a Character Class

<<nobr>>

<<for _i to 0; _i lt $choices.length; _i++>>

<<capture _i>>
<<linkreplace "$choices[_i]">>
	<<set $pcClass.classDescription.push($choices[_i])>>
	<<if $pcClass.classDescription.length gte 1>><<goto "Custom Character Creation 2">><</if>>
<</linkreplace>>
<</capture>>
<br>
<</for>>
<</nobr>>

Using the result later:

<<if $pcClass.classDescription[0] == "Class1">>
Do something
<</if>>

Or if you have more than one class:

<<if $pcClass.classDescription.includes("Class1")>>
Do something
<</if>>

All you did wrong in your first post is that you wrote

$playerClass.classDescription

in the end, which is something that doesn't exist with your setup.

by (2.3k points)
Oh wow! Excellent, thanks for that!

I wasn't using the Story Init! That and the inner workings make it much clearer to my fuddled brain! LOL.

Once again Idling is anything but Idling! :)
...