Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive fiction on the r4rs/tc web repl? (cleaning the textarea and using read-line) #76

Open
kakafarm opened this issue Oct 26, 2024 · 2 comments

Comments

@kakafarm
Copy link

I am trying to write an interactive fiction game in Ribbit. Right now I am able to load arbitrary Scheme code as shown in the web repl examples, using this function after successfully fetching the source:

function feed_code(repl, text) {
    for (let i = 0; i < text.length; i++) {
        if (text.charCodeAt(i) == 10) {
            repl.dispatchEvent(
                new KeyboardEvent('keypress', {'keyCode': 13}),
            );
        } else {
            repl.value += text[i];
        };
    };
};

I am missing two things before I am able to use Ribbit for this game:

Clean the repl textarea after loading the source code:

When I repl.value = "", the repl stops working. No new user input makes the repl print a result.

Read the user input:

When I try to read user input using (read-line), the repl stops working and no new user input makes the repl print a result.

@kakafarm kakafarm changed the title Interactive fiction on the r4rs/tc web repl? Interactive fiction on the r4rs/tc web repl? (cleaning the textarea and using read-line) Oct 26, 2024
@leo-ard
Copy link
Collaborator

leo-ard commented Oct 28, 2024

@kakafarm I just added some examples files to make your quest of making a text-driven game easier! (see #77 for details)

In general, I think you shouldn't try to "hook" into the r4rs repl with JavaScript code (like you are showing with the feed_code function). This can lead to read/write problems and is not very portable to other hosts. Instead, I suggest that you compile the game scheme file to a JavaScript program using ribbit and enable the "web" capabilities. To showcase what I mean, I added a text-game.scm program to the examples. You can compile it like this:

$ ./rsc.exe -t js -l r4rs/tc -f+ js/web -o game.js examples/text-game.scm

Now, game.js can be loaded in any html file and will create a text-area with the running scheme program. I added an example "console.html" under host/js/console.html with #77 as well. See the comments inside text-game.scm for information about how to run the scheme progam in web-javascript or python :)

I made these examples quickly, if you encounter problems, don't hesitate to report them

@kakafarm
Copy link
Author

Thank you. Will post an update when there's something not very embarrassing, only a little.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants