KonsolScript:Tutorials:ClassesAndFunctions

From GPWiki
Jump to: navigation, search

Classes and Functions

Before creating any KonsolScript program, here's is one very important information that you should know.

KonsolScript Classes

Classes in KonsolScript differ in what you already know in other Object-Oriented Programming languages, like C++/Java.

In KonsolScript, a lot of built-in functionalities are already provided to easily create a 2D game. To help us remember at least some of those, these functions were categorized in what KonsolScript Developers call as class or types. But the word type is more known to programmer's lingoes as variable.

So, simply imagine KonsolScript classes as Namespace. Meaning, you cannot access any built-in KonsolScript function without typing the name of the Class it belongs.

KonsolScript currently has these Classes.

  • Array Class to be used if you want to declare Array and/or set its value;
  • Draw for drawing Rectangle, Circle and Line;
  • Entity Class for handling an Entity for the use of Map class;
  • File for accessing files for Read, Write or both;
  • Image for loading and blitting image files;
  • Key Class for binding with Keyboard;
  • Konsol Class to be used for general functions like showing message box;
  • Layer Class for handling Layers of 2D maps for use of Map class;
  • Map Class for handling 2D maps into the engine itself;
  • Math for manipulating numbers and numeric values;
  • Mouse for showing and hiding mouse cursor;
  • Net for creating a networked game/program;
  • Screen for displaying messages to screen;
  • Sound for loading and playing WAV audio files.
  • SpriteSet Class for handling an image files for use of Entity class;
  • String for manipulating String variables;
  • TileSet Class for handling image files for use of Map class;
  • Time Class to have access to the PC clock;
  • Var Class to be used if you want to declare variable and/or set its value.

Syntax is also, somewhat, different. We use a colon to access the function.

KonsolScript_Class:KonsolScript_Function([some_parameter/s])

Given the syntax above, we can sample out the Screen Class' Show function.

  Screen:Show()


KonsolScript Functions

All built-in functions of KonsolScript are in a void-like format.

//a C/C++ way of using a function
  #include <stdlib.h>
  int x;
  x = rand();

In other language, like C/C++, a non-void function is accesed thru an equal sign.


//a KonsolScript way of using a function
  Var:Number x;
  Math:Random(0,10,x)

In KonsolScript, a non-void function is accesed by providing a variable as the last parameter of the function.

If you hate doing like so, we can make a work around on this. We will write a user-defined function-wrapper. How? Read on.

User-Defined Functions

A user-defined function is your way to make your program more organized. Look at the sample below.

#include "console"

Var:String fname,mname,lname;
Var:Number submit,flag;

function main() {
  clearScreen()
  if (submit == 1) {
    flag=0;
    if (fname == "") { writeLine("No first name!") flag=1; }
    if (mname == "") { writeLine("No middle name!") flag=1; }
    if (lname == "") { writeLine("No last name!") flag=1; }
    if (flag == 0) {
      writeLine("Hello, " + fname + " " + mname + " " + lname + "!")
    } else {
      write("First Name : ") readString(fname)
      write("Middle Name: ") readString(mname)
      write("Last Name  : ") readString(lname)
      submit = 1;
      main()
    }
  } else {
    write("First Name : ") readString(fname)
    write("Middle Name: ") readString(mname)
    write("Last Name  : ") readString(lname)
    submit = 1;
    main()
  }
  end()
}


The program above will run without problem but we could optimize it to be more organized.

#include "console"

Var:String fname,mname,lname;
Var:Number submit,flag;

function main() {
  clearScreen()
  if (submit == 1) {
    flag=0;
    if (fname == "") { writeLine("No first name!") flag=1; }
    if (mname == "") { writeLine("No middle name!") flag=1; }
    if (lname == "") { writeLine("No last name!") flag=1; }
    if (flag == 0) {
      writeLine("Hello, " + fname + " " + mname + " " + lname + "!")
    } else {
      getData()
    }
  } else {
    getData()
  }
  end()
}

function getData() {
  write("First Name : ") readString(fname)
  write("Middle Name: ") readString(mname)
  write("Last Name  : ") readString(lname)
  submit = 1;
  main()
}

The sample above showcases a user-defined function which was a part of the code. But instead of repeatedly typing the whole code, we simply have to call the getData function.

Or course, the above samples on user-defined functions are not practical. But there surely are some cases where this simply happens. This is just to sample out the use of a user-defined function. Another is by making a user-defined function-wrapping one.

function main() {
  Var:Number y;
  y = Math_Random(0,10);
}

//a user-defined function-wrapper for Math:Random
function Math_Random(Number nMin, Number nMax):Number {
  Var:Number x;
  Math:Random(nMin,nMax,x)
  return x;
}

The Math_Random contains a code that calls out the KonsolScript's built-in Random function of Math class. In function main, it seemed as if Math_Random is a built-in function that can be used in a non-void format.