Storing Function in a .chai in C++


#1

Is there a way to store a function which is defined in a script in c++ and execute it? I want to reduce the ammout of string lookups.
Basically:
.Chai

def update()
{
    //do stuff
}

In C++

function r = script("update()").function
r(); //instead of calling script("update()") every frame

#2

Absolutely, this is the main feature that ChaiScript has that no one else has that I know of.

auto f = chai.eval<std::function<void ()>>("update");

f();

See also: https://github.com/ChaiScript/ChaiScript/blob/develop/cheatsheet.md#sharing-functions


#3

Works perfect. Thanks