-
Notifications
You must be signed in to change notification settings - Fork 18
Code guidelines
I have adopted the following practices:
Every JavaScript page has this at the top:
"use strict"
This means the JavaScript interpreter will be in strict mode and will object to, for example, variables that are not declared. This is generally regarded as best practice.
<meta http-equiv="Content-Security-Policy" content="
default-src 'self';
style-src 'unsafe-inline' 'self' https://fonts.googleapis.com https://ajax.googleapis.com;
font-src 'self' https://fonts.gstatic.com;
script-src 'unsafe-inline' 'self' https://ajax.googleapis.com;
img-src 'self' https://ajax.googleapis.com;"
>
This means no eval
and no accessing other libraries. Of course, authors can update this if they require.
If you link to images on external sites (perhaps to get around the size limit on the textadventures web site), you will need to include the other sites in the last line.
I use two space indents. It is what I am used to! I like my code to be compact so I can see as much as possible in one screen.
I generally prefer to use // for comments, and only to use /* and */ when commenting out a block of code temporarily. Related, if I decide a block of code is not needed, I will block it off first and make sure it really is not used. If you find a commented out block of code, that is probably the reason.
Best practice is to use const
to declare any variable that does not change and let
if it does (the variable inside for/of and for/in loops should use const
, but I got in the habit of using let
from other for loops, so most do that). There should be no reason to use var
. Note that in strict mode, all variables must be declared before use.
Semi-colons are optional in JavaScript (except in the trivial case of for (;;)
loops), however, this seems to be something that a lot of people have strong opinions on one way or another, and I appreciate I am in something of a minority here. But being in a minority does not make you wrong!
Lint tools all seem to advocate them, and it is what I am used to, so I used them when I started this. I now take the view that if they are optional, they are not needed, so why bother? People say it is easier to read, but how can adding extra punctuation at the end of nearly every line make it easier to read? It is just adding visual noise.
Advocates of semi-colons will point to examples where JavaScript will erroneously assume a semi-colon where it should not. But it will do so regardless of how much you have peppered your code with the things elsewhere. The trick is not to add unneeded semicolons; the trick is to not break your line of code in stupid places. Keep a line of code on one line, and you should be fine - and it will be easier to read.
There are edge cases where a semi-colon can be necessary, but these all seem to be when a line starts with a (
, [
or {
and you have split a statement across two lines or the interpreter thinks you have. These are generally things you can avoid doing (if you use Immediately-invoked Function Expression, then be careful, but if you are using them, you probably know more JavaScript than I do, and you can work out when you need semi-colons for yourself).
If you do want to start a line with (
, [
or {
, put a semi-colon at the start of that line. That line is the problem, so it makes sense to give responsibility for fixing it to that line and not the next/previous line. It will be unpopular, but it is one semi-colon where it is needed, rather than tens of thousands where they are not.
I try to build in support for other languages, which means using a constant or function defined in lang-en.js, rather than hard-coded in the lib file.
The exception to this is error messages produced by bugs in the game. Quest 5 error messages are in English only (though most make little sense anyway), so there is precedent. I would like error messages to be as useful as possible, and that potentially means a lot of them, and to make them traceable, they should be unique. That is much easier if they are just in English. In theory the player will never see them; these are only reporting bugs.
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
respond
function - 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