KonsolScript:Tutorials:Basics

From GPWiki
Jump to: navigation, search

KonsolScript Basics

Imagine a programming language plus a game development library packed into one. That is KonsolScript.

A first KonsolScript program

Here's a traditional example of the "Hello World!" program using KonsolScript.

// HelloWorld - A traditional HelloWorld program in KonsolScript
function main() {
  Screen:Show()
  while(lastpress EQ nochar) {
    Screen:PrintString("Hello World")
    Screen:Render()
  }
}

The code above is simple "Hello World" program. This program will run without error. It will show a window with message saying, "Hello World".


KonsolScript's runtime environment is packed with a standard header called "console". This second "Hello World" sample showcases its use.

// HelloUniverse - A not-so traditional HelloWorld program in KonsolScript using a header
#include "console"
function main() {
  write("Hello World")
  end()
}

Compiling

Make sure you have the latest KonsolScript runtime environment. Get it at KonsolScript's website.

Pick one of the two samples above then do the following (choose your platform):

  • Windows - Copy then paste it on Notepad. Save it given the filename "HelloWorld.ks". Right-click the file then choose Execute.
  • GNU/Linux - Copy then paste it on your editor like gEdit. Type "quixie HelloWorld.ks" on the terminal.

This should produce a file called "HelloWorld.konsl". This is your compiled HelloWorld Program. This will also be automatically executed.

Running

A compiled KONSL file ends with KS - On Windows, simply double-click on the file to execute.

On GNU/Linux, it is suggested to make shell script file to execute it.

#!/bin/sh
quixie HelloWorld.ks

Copy the code above and edit as needed. Then save it as run_ks. Then you can simply double-click run_ks to run HelloWorld.ks

Closer Look

Let's try to go through each part of the program, step-by-step.


// HelloWorld - A traditional HelloWorld program in KonsolScript

This is a comment. It is a way of, literally, explaining what you meant when you wrote your codes. You can write a one-line comment to your code using "//".

You can also make a paragraph-like comment using Multiline comment style. Write it between "/*" and "*/".


function main() {
}

This is our main function called main. Functions are group of code that aim to solve a certain problem. Functions contain logic, which we call algorithm. But usually, beginners tend to write random codes which makes no sense and no logic at all -- try not to be one of them. ^_^

The codes are to be written inside the curly-braces.


Screen:Show()

Remember when I said KonsolScript is like a game engine library? Screen:Show() is actually a command that makes a window visible. This window will be our viewing screen.

The command is Show(), to be accessed from Screen class using a colon (:).


while(/* ...  */) {
}

Here we have a looping command, while. This helps us prolong the execution of the program.


lastpress EQ nochar

Inside the while command, we placed a condition. A condition helps the computer to decide if the codes inside the while loop should be executed. Conditions are also helpful to achieve Artificial Intelligence.

lastpress and nochar, by the way, are built-in variables. lastpress corresponds to the last pressed key in the keyboard. While nochar represents that "no key is being pressed".

The EQ corresponds to "EQual to". (Imagine using ET instead :P). This is a conditional operator, which tries to equate lastpress and nochar.


Screen:PrintString("Hello World")

The command above prints out a message "Hello World" to the primary buffer (which you can't see, yet). Yes, KonsolScript handles a double buffer for you.


Screen:Render()

Lastly, we used a command the displays the buffer to the window (the one showed by Screen:Show()).


There you go, a very basic program in KonsolScript!

Last Words

If there are errors when you execute it, you'll want to do some debugging. Have fun with that! ^_^