Log In  


Here's a snippet I made to launch webpages in the player's browser from within a pico8 cartridge. It only works in html exports, because it uses the GPIO pins to send the url to a custom html template.

Example (player perspective)

For an example of this in action, play https://pancelor.itch.io/make-ten-deluxe and click "info" on the title screen. A paper will pop up with some clickable URLs on them, which will load new tabs when clicked.

Setup

  1. folder config, open the "plates" subfolder, copy the default template (TODO: is there a default template, or did I make it myself by exporting a cart and then undoing pico8's ##js_file## / ##label_file## replacement?)
  2. Find var pico8_gpio = new Array(128); (it's near the top, around line 30)
  3. Replace it with this:
var pico8_gpio = new Proxy(new Array(128),{
    // pancelor's gpio-url, https://www.lexaloffle.com/bbs/?tid=149864
    set(arr, pin, value) {
        // console.log(typeof pin, typeof value); // string number 0.o
        pin = parseInt(pin,10);
        arr[pin] = value;

        // write url chars to pins 1,2...
        // then set pin 0 to ord("u") to open the url
        if (pin==0) {
            var code = String.fromCharCode(value);
            if (code=="u") {
                // read URL (and reset pins to undefined)
                var url = "";
                for (var ii = 1; ii < 128; ii++) {
                    var state = arr[ii];
                    if (state==undefined) break;
                    url += String.fromCharCode(state);
                    arr[ii] = undefined;
                }
                arr[0] = undefined

                // open
                if (!url.startsWith("http")) url = "https://"+url;
                console.log("opening gpio url:",url);
                window.open(url,'_blank');
            } else {
                console.log("unknown gpio command:",code,arr[0]);
            }
        }
    }
});
  1. Save your new template as plates/gpio_url.html
  2. Inside your cart, use this function to open a URL in the player's browser:
function open_external(url)
  assert(#url<=127)
  poke(0x5f81,ord(url,1,#url))
  ?"\^!5f80u" --must be last
end
  1. Export your cart with export -f mygame.html -p gpio_url

Example cart

-- export -f test.html -p gpio_url

function _update()
 if btnp(4) and not launched then
  launched=true
  open_external("pancelor.com")
 end
end

function _draw()
 cls(launched and 2 or 1)
 print(launched and "done" or "press z",54,64,7)
end

function open_external(url)
 poke(0x5f81,ord(url,1,#url))
 ?"\^!5f80u" --must be last
end

License

CC BY 4.0. Just leave the "// pancelor's gpio-url <link>" comment in the code itself and you can use this for anything. (I think CC BY normally means you need to give more credit than just leaving a code comment, e.g. a shoutout in the game's description or something, but in this case I'm not bothered about it.)

5


This has a lot of potential for game guides, author pages, etc. but also potentially for (completely transparent and easily-manipulated*) submission of game data (high scores anyone?) via GET data.

*Obviously anyone can modify a URL to change the data sent, but perhaps in low-stakes environments (among friends) or for data where manipulation doesn't matter (small amounts of custom sprite or map data??) this could be used. Certainly more complete solutions could be crafted for those scenarios that don't involve piggybacking on this, but interesting to think about the possibilities visiting a custom URL opens up.



[Please log in to post a comment]