Interfacing Java with Inform7 and Vorple

I have noticed that there are times when constructing extensions in the Inform7 “language” isn’t exactly practical or possible.
Is there a place to start reading and learning from examples relating to how to interface user-written java code to Inform7/Vorple?

Why use Java when you could use Javascript? But if you really want to use Java you could patch it through Javascript like you would for any other webpage.

Hi Dannii;

I’m thinking about writing some encoding/decoding routines for use in cryptography-related adventures. They don’t really lend themselves to being handled in the Inform7 language. Javascript should work just fine. I note that while it is possible in Inform7, there appears to be nothing on precisely how to interface to/from it in any of the documentation or postings.

I tend to learn by example, or at least reading manuals, so pointers to either would be greatly appreciated.

I think you can just include your own .js file and then call the functions in it like the other Vorple functions are. Juhana will be able to clear up exactly how.

This is one of the demos: …

Thanks, Dannii.
Odd; I seem to remember seeing this, now.
Hope Juhana can enlighten me further. The example is a bit brief for me to be sure I’ve got the idea.
Seems this would only work for web-based games, but not for ones run in the IDE.
It would be nice if Inform7 directly supported extensions in another language that was more appropriate to “number-crunching” and matrices.
That would open up a wealth of possibilities.

You could also write it in Inform 6, then include those functions directly into your I7 project.

The documentation for JavaScript interaction is in the main Vorple extension documentation.

The basic idea is this. You add your custom JavaScript to a file in the Materials folder:

function greet( name ) {
    alert( "Hello " + name );

…and call it from I7:

Release along with JavaScript "greeting.js".

When play begins:
    let current player be "Alice";
    execute JavaScript code "greet('[escaped current player]')".

This is Vorple-specific. If you would like the IDE to integrate other languages, you’ll have to file an Inform feature request.

Thanks, Dannii and Juhana.

I understand that one can call routines that act on the passed parameters, but most examples I’ve seen don’t show how to return info to Inform/Vorple for the mainline program to act upon. For instance, I would like to pass the cypher text and key entered by a user to the javascript and then get the decoded version back for use in the adventure. In a way; I’m trying to write an extension in the form of a Javascript.

Here is all I saw in the Vorple documentation, but is seems that it does not address passing back info to the caller. Am I looking at the wrong part of the doc? Is there more elsewhere?

[code]Chapter 5: Evaluating JavaScript expressions

The story file breaks out of the Z-Machine sandbox by having the web browser evaluate JavaScript expressions. An “execute JavaScript command” phrase is provided to do just this:

execute JavaScript command “alert(‘Hello World!’)”;
At the moment there are no safeguards against invalid or potentially malicious JavaScript. If an illegal JavaScript expression is evaluated, the browser will show an error message in the console and the interpreter will halt.

JavaScript expressions can also be postponed to be evaluated only after the turn has completed and all the text has been displayed to the reader.

queue JavaScript command “alert(‘Hello World!’)”;
The expressions are evaluated in the same order they were added to the queue and the queue is emptied right after evaluation.

Chapter 6: Escaping strings

When evaluating JavaScript expressions, quotation marks must often be exactly right. Inform formats quotes according to literary standards which doesn’t necessarily work together with JavaScript. Consider the following example:

To greet (name - text):
execute JavaScript command “alert( ‘Hello [name]!’ )”.
When play begins:
greet “William ‘Bill’ O’Malley”.
This leads to a JavaScript error because all single quotes (except the one in “O’Malley”) are converted to double quotes, which in turn leads to a JavaScript syntax error. Changing the string delimiters to single quotes wouldn’t help because there’s an unescaped single quote as well inside the string.

To escape text we can preface it with “escaped”:

To greet (name - text):
execute JavaScript command “alert( ‘Hello [escaped name]!’ )”.
Now the string can be safely used in the JavaScript expression.

By default newlines are removed. If we want to preserve them, or turn them into for example HTML line breaks:

To greet (name - text):
let safe name be escaped name using “\n” as line breaks;
execute JavaScript command “alert( ‘Hello [safe name]!’ )”.
As for using another language, like Java or PERL, Danii was correct. Javascript can do most, if not all, of what I need and it is already there. Inform6 may offer some potential too. Its been years since I’ve done anything serious with it, though. Actually; I’m not inclined to ask for more features until I can use all that is already there. I just want to learn how to use Inform/Vorple fully.

Passing data back to the story file is covered in chapter 7. There are more details in the JavaScript API: See the four methods starting from sendCommand().

Thanks, Juhana. I think I understand the mechanism now.
Having to pass info back through the user interface is a bit of a “kludge”, but I can understand that your choices were quite limited.
I wasn’t expecting it to be done this way and so I didn’t really understand the importance of the queue command instruction on my own.

I’ll play with it for a while, using something simple, and see how it works for what I have in mind.

I tried to put together something simple from the examples in the documentation, but am not getting anywhere.
The javascript I am using is captured as well in a comment in the Inform7 code.
I guess I’m making some rather silly error, but I can’t see it and can’t get Inform7 to compile this:

[code]“Parameter Passing 1” by Gary

Include Vorple by Juhana Leinonen.

Release along with the “Vorple” interpreter.
Release along with JavaScript “Caesar.js”.

[This whatI have as Caesar.js

// Caesar.js: Traditional Caesar Shift Cipher
var ret = ‘’;

function Caesar(str) {
var currentkey = 13 //Traditional Caesar Shift
for (var i = 0; i < str.length; i++) {
ret += String.fromCharCode((str.charCodeAt(i) + currentKey + 26) % 26);

Example Location is a room.

To encipher (item - text):
execute JavaScript command “Caesar( ‘[escaped item]’ )”.

When play begins:
encipher “This is a test”;
queue silent primary parser command “’__display_text '+ret”.

Displaying the return is an action out of world applying to one topic.
Understand “__display_text [return]” as displaying the return.

Carry out displaying the return:
now the ciphertext is the return understood;
say “The string is now [ciphertext]”.

I’m getting this error and don’t fully understand why:

[code]Problem. In the sentence ‘now the ciphertext is the return understood’ , I was expecting to read a condition, but instead found some text that I couldn’t understand - ‘ciphertext is the return understood’.
I was trying to match this phrase:

now (ciphertext is the return understood - a condition)
This was what I found out:

ciphertext is the return understood = something unrecognised
Problem. In the line ‘say “The string is now [ciphertext]”’ , I was expecting that ‘ciphertext’ would be something to ‘say’, but it didn’t look like any form of ‘say’ that I know. So I tried to read ‘ciphertext’ as a value of some kind (because it’s legal to say values), but couldn’t make sense of it that way either.
Sometimes this happens because punctuation has gone wrong - for instance, if you’ve omitted a semicolon or full stop at the end of the ‘say’ phrase.

I was trying to match one of these phrases:

  1. say “ciphertext - text”
  2. say “[ciphertext - number]”
  3. say “[ciphertext - unicode character]”
  4. say “[ciphertext - sayable value]”
    This was what I found out:

ciphertext = something unrecognised
Problem. I was unable to understand what you meant by the grammar token ‘return’ in the sentence ‘Understand “__display_text [return]” as displaying the return’ .
See the manual: 17.1 > 17.1. Understand

Could I ask for a bit of help to get me going?

I’m butting my nose into stuff I don’t understand, but I think you haven’t declared “ciphertext” as a temporary variable. Try saying “let ciphertext be a text” before “now ciphertext is the return understood.”


Don’t apologize. I’m scratching my head on this one and not getting anywhere.

I tried your suggestion and it seemed to help a bit. I have a funny feeling that the naming is now what is causing an issue.

Here is what I now get as an error:

[code]Problem. In the sentence ‘now the ciphertext is the return understood’ , I was expecting to read a condition, but instead found some text that I couldn’t understand - ‘ciphertext is the return understood’.
I was trying to match this phrase:

now (ciphertext is the return understood - a condition)
This was what I found out:

ciphertext is the return understood = something unrecognised
Problem. I was unable to understand what you meant by the grammar token ‘return’ in the sentence ‘Understand “__display_text [return]” as displaying the return’ .
See the manual: 17.1 > 17.1. Understand[/code]

Wait, is “return” a kind of value you’ve defined or that Vorple has? It looks like where you have “return” it should just be a text; what happens if you say

Understand "__display_text [text]" as displaying the return.

and replace “the return understood” with “the topic understood”? This is how you usually do commands applying to text strings in Inform, and it looks to me from a cursory scan of Vorple documentation that that’s how passing Vorple stuff back to Inform is supposed to happen too.

That got it to compile. Thanks, Matt.
Now I just need to get it to actually run the javascript and display the result.

I appreciate all the help I’ve had with this and hope this will help someone else get started.

Here is my first successful try at bi-directional data transfer with a Caesar cipher javascript:

And here is the result:

[code] Parameter Passing 1
An Interactive Fiction by Gary
Release 1 / Serial number 140611 / Inform 7 build 6L02 (I6/v6.33 lib 6/12N)
Vorple version 2.5

Example Location

The encrypted string in ROT13 should read, “guvf vf n grfg”.
The program has calculated it to be, “guvf vf n grfg”.[/code]

Is it possible to use jQuery to enhance the Vorple/Inform7 web interface?

Yes, jQuery is one of the included libraries.

I have a javascript that I would like to use in a vorple adventure, but it is very important that the end-user not be able to access the script to determine how it functions. Could someone suggest a way to ensure the script is “hidden”, given that the adventure is hosted on dropbox and the user is passed a link to the index or play.html?

I’ve even considered recoding it in Inform7 to have it compiled into the zblorb.

Appreciate any help on this one.

The browser has to be able to access the script in order to use it, so there’s no way to prevent the user from seeing it. If it’s really that important, run it through an obfuscator before you release it.