Using radiobutton to trigger an action / make sentences appear

Hello, I’m new to the site and a beginner on Twine. I searched the forum but couldn’t find an exact answer to my question:
I want the choice of clicking this or that radiobutton to make different sentences appear in the same passage.
For example, with the famous Pie example :

What's my favourite pie?
* <<radiobutton "$pie" "blueberry" autocheck>> Blueberry?
* <<radiobutton "$pie" "cherry" autocheck>> Cherry?
* <<radiobutton "$pie" "coconut cream" autocheck>> Coconut cream?

If the choice is to tick blueberry, the sentence “Well done, it’s my favourite too!” is displayed
If the choice is cherry or coconut cream, the sentence “Not bad, but I prefer blueberry!” is displayed.

I’ve seen the solution with the “Check answer” button but I’d like the simple fact of choosing a box to trigger the action.

Thanks for your help

Hi there,

Because of how the <<radiobutton>> macro is set up, it is a bit complicated to have extra text popup on the page when clicking on an option (compared to other interactive macros like <<link>> or <<button>>

To do this, you will need to use an event listener to do so: aka have some JavaScript code check if there are any changes on the page from player interaction.

There is however a neat Custom macro by Maliface that doesn’t require you to mess with JavaScript and custom event listeners.

Thanks for your help, Manon, but I’m afraid it’s a bit too complicated for me. I thought there would be a simple solution, internal to Sugarcube. Would my idea be feasible using buttons rather than checkboxes?
if you click button 1 → the sentence A appears
if you click button 2 → sentence B appears
etc

Unfortunately, it would not work either for <<checkbox>> without the event listener.

You could use the <<button>> macro as a fake checkbox:

<<button "Click">>
      <<set $pie to "blueberry>>
      <<replace "#blueberry">>Well done, it’s my favourite too!<</replace>>
<</button>> Blueberry <span id="blueberry"></span>

The code you propose doesn’t seem to work, unfortunately.
I’ve found this code (on this forum or on another one, I don’t remember), which works in combining radiobuttons + a button.

What's my favorite pie?
* <<radiobutton "$pie" "blueberry" autocheck>> Blueberry?
* <<radiobutton "$pie" "cherry" autocheck>> Cherry?
* <<radiobutton "$pie" "coconut cream" autocheck>> Coconut cream?

<<button "Check Answers">>
	<<if $pie is "coconut cream">>
		<<replace "#result">>Correct<</replace>>
	<<else>>
		<<replace "#result">>Incorrect<</replace>>
	<</if>>
<</button>>

Result: @@#result;@@

The problem is, I don’t understand how it works, especially the “@@#” :grin:

Darn it , i forgot one character…
Here:

<<button "Click">>
      <<set $pie to "blueberry">>
      <<replace "#blueberry">>Well done, it’s my favourite too!<</replace>>
<</button>> Blueberry <span id="blueberry"></span>

Essentially both this code and the one involving a “check your answer” button, uses the <<replace>> macro - a macro that will replace the content of an element that you defined on the page (in the example above, it’s <span id="blueberry"></span>, in the one your provide, it’s @@#result;@@).
Note: @@ is the same as <span>, see the documentation.

(May I suggest this guide for SugarCube too :wink: it might help to get how SugarCube works in general)

Thanks A LOT, Manon.
I’ll definitely will dive into your guide. The only one I knew is the “official” one, on motoslave net, which I find not very educational for beginners.

The one on motoslave is a documentation, which is technically different from a guide.
A documentation for a program will be technical, telling as is what code does, using technical terms to each element. It may include warnings for users (like don't ever use this, this is in the code because otherwise it won't work, but don't use it!).
On the other hand, a guide will explain things in plainer terms (potentially being not technically correct for the sake of understanding), often have more examples (plain or interactive).
When you get the hangs of the basic terms, the documentation is the place to go to look up stuff :slight_smile:

If you are looking for other resources for Twine:

1 Like

If you want to use event listeners, you can do it like this:

What's my favourite pie?
* <label><<radiobutton "$pie" "blueberry" autocheck>> Blueberry?</label>
* <label><<radiobutton "$pie" "cherry" autocheck>> Cherry?</label>
* <label><<radiobutton "$pie" "coconut cream" autocheck>> Coconut cream?</label>

<span id="response"></span>

<<done>>
	<<script>>
		$("#radiobutton-pie-0").on("click", function (event) {
			$("#response").text("Well done, it’s my favourite too!");
		});
		$("#radiobutton-pie-1").on("click", function (event) {
			$("#response").text("Not bad, but I prefer blueberry!");
		});
		$("#radiobutton-pie-2").on("click", function (event) {
			$("#response").text("Not bad, but I prefer blueberry!");
		});
	<</script>>
<</done>>

Those "#radiobutton-pie-0", "#radiobutton-pie-1" and so on are the ids of the radiobutton elements. You can find what they are by right-clicking on the radiobutton and selecting “inspect element”. You’ll see something like this:

<input id="radiobutton-pie-0" name="radiobutton-pie" type="radio" tabindex="0" class="macro-radiobutton">

Usually they’ll be radiobutton-[variable]-[number], and they’ll be numbered from the top starting from 0.

Thanks a lot too @svlin
That’s exactly what I was looking for !
I guess this is some java script and not only sugarcube?
I’ll use it for my project, but will definitely have a look at the guides that @manonamora mentionned.

It is jquery, which is essentially a flavour of JavaScript.

2 Likes

I’m glad it helped! For the record, I didn’t mention it in my reply since I didn’t want to complicate matters, but if you want one button to give one unique message and all the others to give the same message, you can use this instead, which will save you the trouble of copy-pasting the same text multiple times:

<<done>>
	<<script>>
		$("#radiobutton-pie-0").on("click", function (event) {
			$("#pie").text("Well done, it’s my favourite too!");
		});
		$("input[name='radiobutton-pie'][id!='radiobutton-pie-0']").on("click", function (event) {
			$("#pie").text("Not bad, but I prefer blueberry!");
		});
	<</script>>
<</done>>
1 Like

Thanks for the clarification.
Oh, just one last detail :wink:
I’d like to insert a link to a new passage in one of the sentences.
I wrote this

$("#radiobutton-herosdejadis-2").on("click", function (event) {
			$("#response").text("Yes, good answer! You can [[continue]]");

but that’s not working.
Maybe that’s because the jquery syntax for the links is something else than [] ???

Yes, if you want links in the output you need to use the wiki method instead:

$("#radiobutton-herosdejadis-2").on("click", function (event) {
    $("#response").wiki("Yes, good answer! You can [[continue]]");

Yes !!!
It seems so simple and obvious once you know how :grin:

Thank you very much.
Sugarcube is already a bit difficult for me, and Jquery is just VERY difficult

1 Like