ChaiScript Tutorial (5.5) – Global Variables in ChaiScript

Besides calling ChaiScript functions from C++. You can also access global variables from C++.

Global Variables in ChaiScript

Unlike most languages, where weather a variable is global or not is defined by if the variable exists in the root scope. Such as the following in C++:

int globalVariable = 0;
{
	int localVariable = 1;
}

ChaiScript uses the global keyword to specify if a variable is global or not. Any variable declared with global is considered as a global variable. Even if it exists in a if statement.  Ex:

global globalVar = 0
var localVar = 1 //technically a local variable
{
	global anotherGlobalVar = 2 //You can still access this after the scope
	var aLocalVar = 3 //Your normal local variable
}
puts(anotherGlobalVar) // this is valid

 

Accessing Global Variables in C++

To set the value of a global variable from C++. The add_global method can be used. Pass in the variable you want to be added wrapped by chaiscript::var to ChaisCript and it’s name. Then add_global will check if the variable exists. If not, then add the value to ChaiScript. Else throw an error. There also exists an const varience. Where the vlaue added is a constant in ChaiScript(But not required to be const in C++). For Example:

chai.add_global(chaiscript::var("Martin"),"NAME");
chai.add_global_const(chaiscript::const_var(3.14), "PI");

To modify a global. Use the set_global method. It will set the value nomatter if the value exises in ChaiScript or not. (It’s add_global without checking.)

Note that variables added by this method is copied by value.  To share a value between ChaiScript and C++. The variable has to be passed through std::ref (or needs to be a refence to the object)or pass a pointer of it. Ex:

float meaning = 42.f;
chai.add_global(chaiscript::var(std::ref(meaning)),"meaning");
chai.add_global(chaiscript::var(&meaning),"life");

Then, in ChaiScript. Both meaning and life represents the same variable in C++.

meaning = 43 //meaning == 43 in C++
life = 4.2 //meaning == 4.2 in C++

Reading Global Variables

Reading global varable back from ChaiScript to C++ is done in the same way we get functions from ChaiScript in out last tutorial.

float val = chai.eval<float>("meaningOfLife");

You should’n do this though. In most cases, it is easier to setup the variable in C++ then share it with ChaiScript instead of creating it in ChaiScript then read it back.

This concludes this short tutorial. We well be exploring OOP in ChaiScript in the next tutorial.

 

Advertisements

One thought on “ChaiScript Tutorial (5.5) – Global Variables in ChaiScript

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: