+1 vote
by (2.2k points)
edited by

Using Sugarcube 2

I really struggled shortening this to put it into a title, but I gave up, so here's my long af description

I want to a variable to be called $name and I want to use this many times, but I want this variable to be set by the user through a simple selection. I have two names I want to be available, Adam and Eve. Throughout the story I want to be able to call upon whatever name they chose with a simple $name, instead of using things like (however you put it in sugarcube, I put it in harlowe as that's what I'm most familiar with) 

(if: $name is "Adam")[Adam is your name]
(if: $name is "Eve")[Eve is your name]

or 

(if: $nameAdam is true)[blah blah]
(if: $nameEve is true)[blah blah]

I don't yet know how to put that in via sugarcube as I'm learning things as I need them or think of them. I don't want to assign separate variables for what they chose like $nameAdam or $nameEve because then I'll have to have if statements every time I want to say their name and that's annoying, but I'll do it if necessary.

I also want to assign another variable based on what they didn't choose. I want when Adam is chosen by the player, Eve will be given to the narrator and vice versa.

Also, I want some text to show up after the player makes a decision between the two names. So it's in the same passage. Would a text box, button, check box, radio button, or something else be best for something like this? I'm thinking a button would make this easier, but what do I know?

1 Answer

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

Seems like you're way overthinking this. Try something like: 

::StoryInit
<<set $name to ''>>
<<set $otherName to ''>>

::somepassage
Choose your name: 

[[Adam|nextpassage][$name to 'Adam'; $otherName to 'Eve']]

[[Eve|nextpassage][$name to 'Eve'; $otherName to 'Adam']]

::nextpassage
Your name is $name. The other name is $otherName. 

It's possible I'm misunderstanding you, but that's really all there is to it. Note that I wrote this code from memory and didn't test it. 

Also, I want some text to show up after the player makes a decision between the two names. So it's in the same passage. Would a text box, button, check box, radio button, or something else be best for something like this? I'm thinking a button would make this easier, but what do I know?

I'm not quite sure what you mean, or how your selection works. Can you provide an example of what this might look like? 

by (2.2k points)
edited by
Well, I was thinking there would be a button on the first passage that has the names in them, and then clicking on one would set the variables as well as show text based on that choice as well as (hopefull) making it so you couldn't choose the other button or the buttons would go away entirely. Let's see, harlowe would look like this I think

(say this is a button that says Adam)[this text would show up if you clicked on this button]
by (63.1k points)

Something like this should do what you want:

Choose you name:

<<button 'Adam'>>
	<<set $name to 'Adam'>>
	<<set $otherName to 'Eve'>>
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>

<<button 'Eve'>>
	<<set $name to 'Eve'>>
	<<set $otherName to 'Adam'>>
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>

@@#name-output;@@

 

by (2.2k points)
Is there a way to make this work with the transitions? Oh! And yes, it works perfect! Thank you so much. I just want a transition to work, would I do it by adding a t8n after the '#name-output'?
by (63.1k points)

The stupid/easy way is to just abuse the <<timed>> macro for the transition.

Choose you name:

<<button 'Adam'>>
	<<set $name to 'Adam'>>
	<<set $otherName to 'Eve'>>
	<<replace '#name-output'>>\
		<<timed 10ms t8n>>\
			You chose the name $name.  The other name is $otherName.
		
			[[Continue|next passage]]\
		<</timed>>\
	<</replace>>
<</button>>

<<button 'Eve'>>
	<<set $name to 'Eve'>>
	<<set $otherName to 'Adam'>>
	<<replace '#name-output'>>\
		<<timed 10ms t8n>>\
			You chose the name $name.  The other name is $otherName.
		
			[[Continue|next passage]]\
		<</timed>>\
	<</replace>>
<</button>>

@@#name-output;@@

That's what I usually do.  It's not efficient or elegant, but it works.

by (2.2k points)
Well, I wanted to use the typed transition but it doesn't work with the time macro
by (2.2k points)
edited by
Also, is there a way that only one button can be chosen and then the other button can no longer be chosen? And is there a way to get the buttons to be next to each other?
by (63.1k points)

Typed transition:  I'm not super familiar with typed.js, but from playing around a bit, there doesn't appear to be any simple way get the typing to play nice with <<replace>>.  Or with any sort of late additions like this.  Someone else might be able to help with this.  Sorry.

Choose one or the other button:  Wrap the <<button .....>>....<</button>> code in something, like @@#eve-button; ... @@ then have clicking on one button replace the other with nothing.  Example:

Choose you name:

@@#adam-button;<<button 'Adam'>>
	<<set $name to 'Adam'>>
	<<set $otherName to 'Eve'>>
	<<replace '#eve-button'>><</replace>> /% the empty replace %/
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>@@

@@#eve-button;<<button 'Eve'>>
	<<set $name to 'Eve'>>
	<<set $otherName to 'Adam'>>
	<<replace '#adam-button'>><</replace>>
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>@@

@@#name-output;@@

Buttons on the same line:  Just put them on the same line:

Choose you name:

@@#adam-button;<<button 'Adam'>>
	<<set $name to 'Adam'>>
	<<set $otherName to 'Eve'>>
	<<replace '#eve-button'>><</replace>>
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>@@ @@#eve-button;<<button 'Eve'>>
	<<set $name to 'Eve'>>
	<<set $otherName to 'Adam'>>
	<<replace '#adam-button'>><</replace>>
	<<replace '#name-output'>>\
		You chose the name $name.  The other name is $otherName.
		
		[[Continue|next passage]]\
	<</replace>>
<</button>>@@

@@#name-output;@@

You can use CSS/style rules to generate more space if needed.  For example, we can add a bit of margin to the eve button:

Choose you name:

[...]
	<</replace>>
<</button>>@@ @@#eve-button;margin-left:1.5em;<<button 'Eve'>>
	<<set $name to 'Eve'>>
[...]

You can also use \ to ignore line breaks instead of putting the first closing <</button>> on the same line as the next opening <<button....>>.  E.g.:

Choose you name:

[...]
	<</replace>>
<</button>>@@\
@@#eve-button;<<button 'Eve'>>
	<<set $name to 'Eve'>>
[...]

 

by (159k points)

The space between the two buttons is caused by the line-breaks you have between then, simply use Line Continuations you remove them. You can also wrap the buttons within a Custom Style with an ID and then use the <<replace>> macro to clear that area.

Choose your name:

@@#buttons;
<<button 'Adam'>>
	<<set $name to 'Adam'>>
	<<set $otherName to 'Eve'>>
	<<replace '#name-output'>>\
		<<timed 10ms t8n>>\
			You chose the name $name.  The other name is $otherName.
		
			[[Continue|next passage]]\
		<</timed>>\
	<</replace>>
	<<replace '#buttons'>><</replace>>
<</button>>\
\
<<button 'Eve'>>
	<<set $name to 'Eve'>>
	<<set $otherName to 'Adam'>>
	<<replace '#name-output'>>\
		<<timed 10ms t8n>>\
			You chose the name $name.  The other name is $otherName.
		
			[[Continue|next passage]]\
		<</timed>>\
	<</replace>>
	<<replace '#buttons'>><</replace>>
<</button>>
@@

@@#name-output;@@

note: I also corrected the grammar mistake of using "you name" instead of "your name"

by (63.1k points)
CHOOSE YOU NAME!!!!!!!!!! That's what I get for copying/pasting over and over.
by (159k points)
Cut-N-Paste: Type once, make the same mistake/error everywhere. LoL
by (2.2k points)
Thank you both. Perhaps about the typed transition there's a way to assign all text in the css to use a transition?
by (63.1k points)
That's not really the issue, I don't think. The issue seems to be that the beginning of the animation is tied to the rendering process. This means that text that is delayed for any reason (timing, interaction, etc) isn't typed because it essentially missed its window (i.e. it isn't rendered with the rest); the transition is treated as though its already over when the text hits the page, and I'm not sure how to arbitrarily restart it.

You seem like a cool person, but I don't use typed.js or the integration module in my own stuff, so I'm not really willing to take the time it'd require to read the code and figure out a work-around (and I'm only guessing at the issue anyway). That said, the code's author/the developer of sugarcube has been known to stop by and answer questions, and I know some other people here use it, and there are other talented devs around as well, so all hope isn't lost. You might need to create a new question specifically about using typed.js with <<timed>> and <<replace>> macros though, since this question's title isn't really attracting the kind of expertise you need.
by (2.2k points)
That's chill, I was only asking if was something known or not. Thank you for stopping by and being so helpful in the first place. I understand your explanation and that makes sense. I'll play around with it some more and do some more searching before I post another question about the typed transition.

Thanks so much for your help Chapel! You're like the second most awesome person ever!
...