-
Notifications
You must be signed in to change notification settings - Fork 18
JavaScript
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.
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.
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.
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.
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 !
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.'
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))
}
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
- First steps
- Rooms and Exits
- Items
- Templates
- Items and rooms again
- More items
- Locks
- Commands
- Complex mechanisms
- Uploading
QuestJS Basics
- General
- Settings
- Attributes for items
- Attributes for rooms
- Attributes for exits
- Naming Items and Rooms
- Restrictions, Messages and Reactions
- Creating objects on the fly
- String Functions
- Random Functions
- Array/List Functions
- The
respondfunction - Other Functions
The Text Processor
Commands
- Introduction
- Basic commands (from the tutorial)
- Complex commands
- Example of creating a command (implementing SHOOT GUN AT HENRY)
- More on commands
- Shortcut for commands
- Modifying existing commands
- Custom parser types
- Note on command results
- Meta-Commands
- Neutral language (including alternatives to "you")
- The parser
- Command matching
- Vari-verbs (for verbs that are almost synonyms)
Templates for Items
- Introduction
- Takeable
- Openable
- Container and surface
- Locks and keys
- Wearable
- Furniture
- Button and Switch
- Readable
- Edible
- Vessel (handling liquids)
- Components
- Countable
- Consultable
- Rope
- Construction
- Backscene (walls, etc.)
- Merchandise (including how to create a shop)
- Shiftable (can be pushed from one room to another)
See also:
- Custom templates (and alternatives)
Handing NPCs
- Introduction
- Attributes
- Allowing the player to give commands
- Conversations
- Simple TALK TO
- SAY
- ASK and TELL
- Dynamic conversations with TALK TO
- TALK and DISCUSS
- Following an agenda
- Reactions
- Giving
- Followers
- Visibility
- Changing the player point-of-view
The User Experience (UI)
The main screen
- Basics
- Printing Text Functions
- Special Text Effects
- Output effects (including pausing)
- Hyperlinks
- User Input
The Side Panes
Multi-media (sounds, images, maps, etc.)
- Images
- Sounds
- Youtube Video (Contribution by KV)
- Adding a map
- Node-based maps
- Image-based maps
- Hex maps
- Adding a playing board
- Roulette!... in a grid
Dialogue boxes
- Character Creation
- Other example dialogs [See also "User Input"]
Other Elements
- Toolbar (status bar across the top)
- Custom UI Elements
Role-playing Games
- Introduction
- Getting started
- Items
- Characters (and Monsters!)
- Spawning Monsters and Items)
- Systema Naturae
- Who, When and How NPCs Attack
- Attributes for characters
- Attacking and guarding
- Communicating monsters
- Skills and Spells
- Limiting Magic
- Effects
- The Attack Object
- [Extra utility functions](https://github.com/ThePix/QuestJS/wiki/RPG-Library-%E2%80%90-Extra Functions)
- Randomly Generated Dungeon
- Quests for Quest
- User Interface
Web Basics
- HTML (the basic elements of a web page)
- CSS (how to style web pages)
- SVG (scalable vector graphics)
- Colours
- JavaScript
- Regular Expressions
How-to
Time
- Events (and Turnscripts)
- Date and Time (including custom calendars)
- Timed Events (i.e., real time, not game time)
Items
- Phone a Friend
- Using the USE verb
- Display Verbs
- Change Listeners
- Ensembles (grouping items)
- How to spit
Locations
- Large, open areas
- Region,s with sky, walls, etc.
- Dynamic Room Descriptions
- Transit system (lifts/elevators, buses, trains, simple vehicles)
- Rooms split into multiple locations
- Create rooms on the fly
- Handling weather
Exits
- Alternative Directions (eg, port and starboard)
- Destinations, Not Directions
Meta
- Customise Help
- Provide hints
- Include Achievements
- Add comments to your code
-
End The Game (
io.finish)
Meta: About The Whole Game
- Translate from Quest 5
- Authoring Several Games at Once
- Chaining Several Games Together
- Competition Entry
- Walk-throughs
- Unit testing
- Debugging (trouble-shooting)
Releasing Your Game
Reference
- The Language File
- List of settings
- Scope
- The Output Queue
- Security
- Implementation notes (initialisation order, data structures)
- Files
- Code guidelines
- Save/load
- UNDO
- The editor
- The Cloak of Darkness
- Versions
- Quest 6 or QuestJS
- The other Folders
- Choose your own adventure