Including chai script file into another


#1

Hi,
first of all I really enjoy ChaiScript and find it very easy to use. I would like to know if chaiscript can include different script files. I want to organize my logic and avoid boilerplate code. Here is example of what I want to achieve:

file A.chai
class A { def A() { } }
file B.chai
class B { var a; def B() { a = A(); } }
Thanks in advance.


#2

Certainly, you can use the ‘use’ function to include another ChaiScript file.

See the doc here: https://github.com/ChaiScript/ChaiScript/blob/develop/cheatsheet.md#evaluation

-Jason


#3

With this solution I need to know files I need to include for script, am I right? I tested today chai.exe from bin directory and example from first post just works. I guess I should shed more light on what I want to achieve.

Each file has a definition of class in chai script language, still there are 2 types of classes. First type are classes wrapped in my core engine by C++ class and each C++ class instance creates chai script class instance. I wanted to create base class in chai script language for this purpose and use inheritance, but let’s drop this for now, this is a different issue. Second type are simple classes that are not wrapped in C++. They should be used only in other chai scripts, but still, defined in different text files.

This is my first contact with embedded script languages so sorry if I misunderstood something or can’t explain properly what I want to do.


#4

I’m not entirely sure I understand what you are asking, but if you want one file to include another, you will need to know the name of the file, yes. There are many different ways you could organize this code, however.


#5

Sorry, I didn’t understand your first answer. I thought I need to use “use” function in my C++ code. Basically I just had to add it… in chaiscript file :). So for all halfbrains like me, scripts from first post should look like this:
file A.chai

class A {
    def A() {
        print("I am A");
    }
}

file B.chai

use("A.chai")

class B {
var a;
    def B() {
        a = A();
    }
}

Thanks for help!