KonsolScript:Tutorials:Basics
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. [edit] KonsolScript BasicsImagine a programming language plus a game development library packed into one. That is KonsolScript. [edit] A first KonsolScript programHere'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".
// HelloUniverse - A not-so traditional HelloWorld program in KonsolScript using a header
#include "console"
function main() {
write("Hello World")
end()
}
[edit] CompilingMake 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):
This should produce a file called "HelloWorld.konsl". This is your compiled HelloWorld Program. This will also be automatically executed.
[edit] RunningA compiled KONSL file ends with KS - On Windows, simply double-click on the file to execute. On GNU/Linux, drag the file to the Quixie.desktop.
[edit] Closer LookLet'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()).
[edit] Last WordsIf there are errors when you execute it, you'll want to do some debugging. Have fun with that! ^_^
|


