fly me to the moon

BASIC CODING

Coding in Kawari can be fun and easy. There are a lot of things which coding can open up as possibilities for your Ghost. Personalizing a birthday message, writing a branching conversation, creating a functional notepad, these are all things possible in Kawari.

To cover our bases, let's establish some core concepts.

Terminology

Here are some terms you may find useful to know. Don't worry about memorizing these or anything. Most of them are straightforward, and they should quickly become natural to you.

A variable is a container for an abstract value. In Kawari, our variables are called entries.
Entries are the building blocks of Kawari. They can hold multiple values and values of different types.
The values entries hold are known as definitions. The definition of an entry can normally be changed at any time.
A literal is a fixed value. Unlike a variable, this value is not abstract. Literals are commonly used as definitions for variables.
A string is an arbitrary sequence of characters contained either in a variable, or as a constant known as a string literal.
An integer is any whole number (positive or negative), or zero. 14 is an integer, and so is 15039, and 0 as well.

Feel free to circle back to this as needed.

Writing Practices

Traditionally, there's a few different conventions for writing variable names. This refers to how we use capitalization, what we use instead of spaces, and so on.

In this guide, I tend to use 'camel case'. Camel case is the act of capitalizing every new word in a variable name (e.g. exampleEntry), with the first word being capitalized or not.

An alternate convention I also use is 'snake case', where underscores are used as spaces and no capitalization is present. (e.g. example_entry)

I learned these in my programming classes, and never really shook them off.

There's many naming conventions for variables and which is best is a topic of contention among programmers everywhere. However, for the purposes of this guide, the 'best' convention is the one you are most comfortable using and serves your purposes best. Clarity of your entry names while also being useable by you is all that matters.

Basics


Now we will cover the basics of Kawari. This will cover how data, expressions, and basic code is handled in Kawari. First is...

Entries

Entries will make up the majority of what you write. They hold almost everything in your Ghost on some level. As entries can hold many different types of values, it's not difficult at all to write a new entry and definition.
The syntax for writing an entry definition is 'entryname : definition'.
example_1 : text1
This entry is named 'example' and has the definition 'text1'. Entries can be simple like this, or very long and complex.

A useful function of entries is that their definitions can be recalled. They can even be called inside of other entries. The syntax for writing an entry call is '${entryname}'.
example_2 : ${example_1}
This entry will find the definition for example_1 and substitute '${example_1}' with it. In this case, it would be replaced with 'text1'.

Arrays

Entries can have multiple definitions as well. Entries with multiple definitions are known as arrays. The definitions in an array can also be referred to as elements, and are delimited by commas unless otherwise specified.
animals_1 : dog, cat, bird
When an entry has multiple definitions, the entry call will randomly choose one of them. So, if you called animals_1, it would randomly choose from 'dog', 'cat', or 'bird'.

However, you can also specify an element by using its index. Every element in an array is internally numbered in order from 0, and this number is its index. In animals_1, the element at index 0 is 'dog'. The element at index 2 is 'bird'. Calling a specific element is known as an entry array call. The syntax to perform an array call is '$entryname[index]'.
example_3 : $animals_1[1]
This will call the element at index 1 of animals_1. In this case, it would be 'cat'.

Arrays are very useful, but a bit complex. We'll touch on arrays deeper in the advanced section.

Evaluations

Kawari has specific syntax to handle evaluating expressions. Expressions formatted as '$[expression]'. Note the square brackets. These expressions can be mathematical, such as:
addition : $[ 1 + 2 ]
subtraction : $[ 2 - 1 ]
multiplication : $[ 2 * 2 ]
division : $[ 4 / 2 ]
Try calling those as entries, for an example of what it would look like in dialogue. The spacing around the brackets isn't necessary, but it makes it more readable when coding. Evaluations can also evaluate if an expression is true or false, such as:
true_1 : $[ 1 = 1 ]
false_1 : $[ 1 = 2 ]
We can use anything we'd like in our evaluations, including strings. We can even use entry calls!
variable_1 : $[ ${pets2} = 'cats' ]
So, if the random entry pick is 'cats', that will be true. If it's not 'cats', it will be false. Neat, huh?

Also, keep in mind, anything that is not 0, an empty string, or 'false' in Kawari is considered true.

KIS

KIS stands for "Kawari Inline Script". KIS is the aspect of Kawari developed specifically for programming and not Ghost dialogue. It allows for more complex actions within a Ghost. The parentheses, (), are used to denote KIS when used with a $. Anything inside '$()' will be treated like code, which functions a bit differently to writing an entry definition normally.

Parentheses are used to define blocks, which are scripts read from top to bottom. The parentheses which signal that KIS has started function as defining a block as well. The echo function will send dialogue when inside of a code block but lines of code need to be ended with a semicolon (;). The semicolon tells the parser that a line of code has ended, and it should move onto the next. Example of a code block:
codeEntry1 : $(
 echo "\1\s[10]You know, I think this is pretty neat!";
 echo "\1\s[10]\n\n[half]Don't you think so, too?";
)
This will print the contents of the quotes into the balloon.

If/Else

Another introductory function is actually a pair of functions. if/else are logic based functions which go together. They will perform code based on if a certain condition is true or not. This is why evaluations become important. The if statement sets up the condition and what to do when it's true, such as:
codeEntry2 : if $[ ${pets2} = 'cats' ] $(echo "\0\s[0]Eugh, a weird feeling just came over me.")
If 'cats' is randomly picked when this is run, then the Sakura will say something.

Else is the other half, it lets us set up what to do when the if statement's condition isn't true. An if/else statement looks like this:
if $[ ${username} = "Developer" ] $(echo "\1\s[10]Hello, "${username}"! Patching those bugs?")
else $(echo "\1\s[10]Hello, "${username}"!")
So, if the user's name isn't "Developer", the Ghost will instead say hello without commenting on patching bugs. We can actually check for negative statements, as well. The operator != stands for "does not equal to".
if $[ ${pets2} != 'rats' ] $(echo "\1\s[10]Squeakity!")
If we wanted to check only for when something is not equal to another, that's how.

Other operators can be used in logical evaluations like this. Greater than (>) and less than (<), Greater than or equal to (>=) and less than or equal to (<=), plus (+), and minus (-) are all valid. Specific logical operators such as OR (||), AND (&&), and NOT (!) are available. NOT is interesting as it can negate other operators as well as true/false evaluations. This is how != means "does not equal to".

There are many functions for KIS, which you may find useful if you would like to get into more advanced coding.

jump

Index
Introduction
Glossary
Writing
Writing in Kawari
Writing with SakuraScript
Shells
Shell Overview
Coding
SHIORI Event Overview Basic Coding
Advanced Coding

credit

walkthrough by Okuajub
design by almost sweet - resources