ncase / trust

An interactive guide to the game theory of cooperation
Creative Commons Zero v1.0 Universal
5.76k stars 917 forks source link

How to pass slide id with slideshow/goto ? #89

Open SamsadSajid opened 5 years ago

SamsadSajid commented 5 years ago

There are a couple of events with the slides and one of them is slideshow/goto. The event is described in js/core/Slideshow.js at line 119

So, I have a button on a slide and when I click the button I want to go to the 3rd slide.

I am adding the button in a slide like this:

self.add({
    id:"intro_button2", type:"Button", x:270, y:25, size:"short",
    text_id:"intro_button2",
    message:"slideshow/goto",
});

Now how do I mention the slide id (which is 3) so that when I click the button it takes me to the 3rd slide?

L1Q commented 1 year ago

You can't do this with message.

As you can see in js/core/Button.js lines 81 and 82, the button logic will call publish with a single argument of the message name (line 82). It can also call your custom callback (line 81).

What you want instead of message, is to pass an onclick with a function that calls publish with an extra argument.

You can combine such a function from js/core/SlideSelect.js with a button from js/slides/1_Slides_OneOff.js:

self.add({
    id:"intro_button2", type:"Button", x:270, y:25, size:"short",
    text_id:"intro_button2",
    onclick:function(){
        publish("slideshow/goto", ["tournament"]);
    }
});

Note: the slide id is not a number, it's a string provided during a SLIDES.push. Many slides don't even have an id for slideshow/goto to work with.