Help with Chaiscript_Basic


#1

I have been using v6 for a while (after some help from you getting converted).

I would like to us “use” in my scripts, to include several utility files containing generic code.

I believe, therefore, I need to use “Chaiscript_Basic” rather than “Chaiscript” - but I can’t find an example that uses chaiscript_basic, so I’m struggling a bit converting by code

At the moment I have
std::unique_ptrchaiscript::ChaiScript create_chaiscript()
{
auto chai = std::unique_ptrchaiscript::ChaiScript(new chaiscript::ChaiScript());
chai->add(createChaiscriptBindings());
auto scriptFileName = cocos2d::FileUtils::getInstance()->fullPathForFilename(“World1.chai”);
chai->eval_file(scriptFileName);
return chai;
}

I have tried changing it to use Chasicript_Basic - but have failed (I can’t get the constructor right).

Are there any examples of using Chaiscript_Basic I may be able to copy?


#2

If you are just trying to use “use”, you don’t have to use Chaiscript_Basic directly. Use chaiscript.hpp, which handles the Chaiscript_Basic stuff for you.

#include "chaiscript.hpp"
using namespace chaiscript;

// set up vectors of file paths for your situation
std::vector<string> module_paths({"c:\\my_modules_path1\\", c:\\my_modules_path2\\});

// this vector should include the path to your 'use' files
std::vector<string> use_paths({"c:\\my_use_path\\"});

// ***********  IMPORTANT!!! ***************
// note the trailing "\\" in the use paths.  They seem to be absolutely necessary.
// * this is on Windows, Visual Studio 2015.
// I presume linux may look different '/' ?
// **********************************************

std::vector<string> script_paths({"c:\\my_script_path\\"});

// not sure that this is necessary...I believe that the default value includes this option already
// I do know that this is working for me.
std::vector<Options> options({Options::External_Scripts});

// construct like this, passing the vectors of paths to the constructor.
// see chaiscript.hpp file for prototype of the constructor.
// it will construct the Chaiscript_Basic class with the Std_Lib::library(),
// and the paths and options passed in.

chaiscript::ChaiScript chai(module_paths, use_paths, script_paths, options);

Now when a script is executed, it can include stuff like:

use("my_use_file.c");

I don’t fully understand the difference between ‘modules’, ‘use’, and ‘script’ paths, but I know the above construct will find “my_use_file.c” in a path specified in the use_paths vector passed to the constructor.

I can put stuff like:

def do_something()
{
    auto x = 5;
    return x;
}

in a file in the ‘use’ path called “my_use_file.c”.

From a script I can do this:

use("my_use_file.c");
auto y = do_something();  // <-- this function is defined in the 'use' file

#3

Thank you so much - I managed to get it working with your help!

There doesn’t seem to be a ‘scripts_path’ argument in the constructor in the version I’m using - but omitting that and passing an empty string for the use_paths parameter got it working;

(the scripts are in my resources directory, which is why I think an empty string works - this is a C++ application for IOS but running on a Mac for testing - so absolute paths aren’t possible.)

Incidentally the options does seem to be necessary - without it I get an exception trying to use ‘use’.

Again- thanks for your help!