Skip to content

JavaScript

ThePix edited this page Feb 25, 2020 · 27 revisions

JavaScript has some important differences if you are used to ASL, the coding language for Quest 5.

ASL is a somewhat cut down language, and as it runs on top of the Quest player, you do not have access to everything, so is a little limited. One of the biggest limitations is that you cannot pass parameters to a script or return a value (well, you can, using delegates, but the editor does not support it, so it is a bad idea).

JavaScript is a fully-fledge programming language, and as everything is written in JavaScript, you have access to everything. When you convert from ASL to JavaScript, you will tend to encounter the downside of JavaScript, because your existing code will not be designed to make use of the features only in JavaScript.

The world, w

In ASL the global world of the code contains all the objects in the game. If there is an object called "table", you can just refer to it as table.

In JavaScript the global world includes every built in function, elements of the web page, etc. It is therefore necessary to have a sealed off section that is the game world. This is called "w". To access an object called "table", you have to go via w, so must use w.table or `w["table"] to get it.

Declare variables

In JavaScript you have to declare a variable before you use. If it is going to change, use let, if it is not going to change, useconst.

const height = obj.getHeight()  // height cannot change
let n                           // n can change
n = height + 4                  // so now n changes
const list = []                 // list cannot change...
list.push(n)                    // but its contents can

If you are unsure if a variable will change, use let. There is also an older keyword, var, which is much like let.

End a line with a semi-colon... or not

I started putting a semi-colon at the end of each line, but they are optional, so lately I have not bothered.

If you have two statements on the same line, then you do need a semi-colon to separate them, but really this is to be avoided anyway.

You will find a lot of web pages by people insisting semi-colons should be used. There are some really weird uses of JavaScript that people cite (eg here, but the solution is removing a line break; adding a semi-colon does not solve the issue.

So please yourself.

Testing equality and inequality

I you want to compare two variables to see if they are equal, in ASL you use an equals sign. For not equal, use less than then greater than:

if (height = 12) {

if (height <> 12) {

Most modern programming languages use two equals signs to differentiate from when you are assigning a value, and an exclamation mark and equals sign for "not equals". JavaScript is the same, but will first try to "coerce" the types, which can give some unexpected behaviour. It is safer to use three equals signed:

if (height === 12) {

if (height !== 12) {

For Boolean operations:

and     &&
or      ||
not     !

Strings

As in ASL, a string can be denoted by double quotes, but you can also use singles - as long as they are the same at both ends. This allows you to include quotes in your string.

s = "Strings with double quotes are cool, aren't they?"
s = '"No!" she said emphatically.'

Get and Has functions

ASL has a bunch of functions for checking if an object has a certain attribute or to get its value.

if (HasString(table, "alias")) {
  msg(GetString(table, "alias"))
}

In JavaScript you can just access it directly. If you are not worried about what type the attribute is, you just want to know if it exists:

if (w.table.alias)) {
  msg(w.table.alias))
}

If you need to ensure the type of the attribute, use typeof:

if (typeof w.table.alias === 'string')) {
  msg(w.table.alias))
}

In ASL you would probably not use GetString as you can use the . operator there too (the function exists for the visual editor), however it is common to use GetBoolean as that handles the case where the attribute does not exist.

if (GetBoolean(table, "smashed")) {
  msg(GetString(table, "alias"))
}

Not a problem in JavaScript, as a missing attribute is the same as one set to false inside an if statement.

if (w.table.smashed)) {
  msg(w.table.alias))
}

Functions

As JavaScript is object-orientated, several functions are part of the object, rather than floating free. The means the name of the function goes after the object it is associated with, separated by a dot.

Split(mystring, "!") mystring.split("!")

Join(myarray, "!") myarray.join("!")

Replace(mystring, "!", "@") mystring.replace("!", "@")

StartsWith(mystring, "+") mystring.startsWith("+")

EndsWith(mystring, "+") mystring.endsWith("+")


The name changes too for some:

Mid(mystring, 5) mystring.substring(5)

Mid(mystring, 5, 2) mystring.substr(5, 2)

ListCount(ary) ary.length


### Dictionaries and lists

Dictionaries and lists are built in to JavaScript, making them easier to use, but somewhat different to ASL. Here is the ASL to create a list and a dictionary, in each case adding three items, then printing the first.

myList = NewStringList() list add(mylist, "one") list add(mylist, "two") list add(mylist, "three") msg(StringListItem(mylist, 0)

myDictionary = NewStringDictionary() dictionary add(myDictionary, "first", "one") dictionary add(myDictionary, "second", "two") dictionary add(myDictionary, "third", "three") msg(StringDictionaryItem(myDictionary, "first")

In JavaScript:

const myList = [] mylist.push("one") mylist.push("two") mylist.push("three") msg(mylist[0])

const myDictionary = {} myDictionary["first"] = "one" myDictionary["second"] = "two" myDictionary["third"] = "three" msg(myDictionary["first"])

Or even quicker:

const myList = ["one", "two", "three"] msg(mylist[0])

const myDictionary = { first:"one", second:"two", third:"three", } msg(myDictionary.first)


JavaScript dictionaries are very useful, and pervade Quest 6 extensively, so it is important to understand them.

### Loops

JavaScript `while` loops are just like ASL.

The `for` looks are a little different. Here is the ASL version; `i` is the variable inside the loop. It will start with a value of 3, go up to a value of 7, in steps of 2 (the last parameter defaults to 1 if missing).

for (i, 3, 7, 2) { msg(i) }


In JavaScript it looks like this, which is more complicated, but more flexible:

for (let i = 3; i <= 7; i += 2) { msg(i) }


The `for` statement has three parts:

let i = 3 // Initialisation: declare a variable i and set to 3 i <= 7 // Condition: continue while i is 7 or less i += 2 // Step: after each iteration add 2


ASL has `foreach` for iterating over a list or dictionary.

foreach (s, myList) { msg(s) } foreach (key, myDictionary) { msg(StringDictionaryItem(myDictionary, key)) }


JavaScript use `for`, but in a different format. Note that you have to use `of` for a list and `in` for a dictionary.

for (let s of myList) { msg(s) } for (let key in myDictionary) { msg(myDictionary[key]) }







### Others

Convert ASL to JS

HasString(myobj, "attname") myobj.attname

Replace(mystring, "!", "@") mystring.replace("!", "@")

StartsWith(mystring, "+") mystring.startsWith("+")

EndsWith(mystring, "+") mystring.endsWith("+")

Mid(mystring, 5) mystring.substring(5)

Mid(mystring, 5, 2) mystring.substr(5, 2)

Split(mystring, "!") mystring.split("!")

Join(myarray, "!") myarray.join("!")

ListCount(ary) ary.length

n = 3 n === 3

not n !n

not n = 3 n !== 3

and &&

or ||

let

abs(n) Math.abs(n) also pow

StringListItem(ary, 1) ary[1]

Tutorial

QuestJS Basics

The Text Processor

Commands

Templates for Items

See also:

Handing NPCs

The User Experience (UI)

The main screen

The Side Panes

Multi-media (sounds, images, maps, etc.)

Dialogue boxes

Other Elements

Role-playing Games

Web Basics

How-to

Time

Items

Locations

Exits

Meta

Meta: About The Whole Game

Releasing Your Game

Reference

Clone this wiki locally