Skip to content
eaxs edited this page Dec 30, 2011 · 2 revisions

The K2 scripting framework is designed to isolate each class and method into labels that follow a specific naming pattern. You can see that in the code example in the article Introduction to modules

ExecScript K2 Array.new "my_array"

To give you a better understanding of what is going on behind the scene when you call the K2 script, take a look at the line of code above and then we’ll take it apart.


ExecScript K2

The first part simply executes the K2 script.

Array.new

The second part tells the K2 script to jump to the label of the method “new” of the class “Array”.

"my_array"

The final part is the value of the the “Array.new” parameter.


As mentioned at the beginning, the K2 framework separates classes and methods with labels (lines starting with @). And you should know that from your previous modding experience that you can jump to any label within the same script by using the “goto” command. And this is exactly what happens in the K2 script. Here is a simplified code example of how the line of code above is processed in the script:

@K2::__construct{
        // Initializes the K2 core class and then jumps to the K2::__exec{ label
    #@# "K2::__exec{";
    
@K2::__exec{
        // Here the script decides to which class label to jump
        // In this example, it check if the param "Array.new" is given and then jumps to the Array constructor below
        if #StringLength(|#GetScriptParam(Array.new)|#)# "#@# \"Array::__construct{\"";
    #@# "K2::__destruct{";

...

@Array::__construct{
        // Runs some class setup code and then jumps to the exec function below
    #@# "Array::__exec{";
    
@Array::__exec{
        // The exec function decides to which method label to jump
        // In this example, it check if the Array.new param has a value and if so, it jumps to the Array::new{ label below 
        if #StringLength(|#GetScriptParam(Array.new)|#)# "#@# \"Array::new{\"";
    #@# "Array::__destruct{";
    
@Array::new{
        // The script has reached its final destination
        // Here goes all the code of the "Array.new" method 
    #@# "Array::__destruct{";
    
@Array::__destruct{
    // Magic function which destructs the class
#@# }


@}

If the example above wasn’t clear enough, the routing basically goes like this:

  1. Call K2 script
  2. Jump to module label
  3. Jump to method label of the modul
  4. Jump to end of script when done

Clone this wiki locally