Can I modify simple C types (int, bool, etc) from ChaiScript, without functions?


#1

I’ve tried these options (individually), but none changes my settings::some_setting in C/C++ code:

chai.add(chaiscript::var(std::ref(settings::some_setting)), "some_setting");
chai.add(chaiscript::var(&settings::some_setting), "some_setting");
chai.add(chaiscript::var(settings::some_setting), "some_setting");

i.e. after adding the variable, I do:

// in chai
some_setting = true;

// in my source
if (some_setting) { printf("...something..."); 
// ^no output, so some_setting is false

I also tried setting my variables manually after every .eval() I perform, like this:

settings::some_setting = chai.eval<bool>("some_setting");

But that doesn’t work either!

Am I doing it wrong?


#2

I’m confused as to what exactly settings is.

Take this simple example (which works fine):

#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>

int main()
{
  bool some_setting = false;

  chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
  chai.add(chaiscript::var(&some_setting), "some_setting");
  chai.eval("some_setting = true");

  if (some_setting) { std::cout << "It was set!\n"; }
}

Can you clarify what your “settings” is? Is it a namespace? Struct with static variables?


#3

It is just this:

namespace settings {
    bool something = true;
};

A neat way of accessing settings without having to prefix variables, like you’d do in C (settings::something is better than setting_something imo)

Edit: I tried your example in a clean project and it works. It seems ChaiScript is not the issue. Sorry for bothering and thanks for the help!


#4

No problem, glad you got it figured out.