First we must derive our new class from the Simkin skScriptedExecutable. This object exposes the methods required to communicate with the scripting system. The constructor takes the name of the script that will communicate with the C++ object. We must override the three standard methods in order to support that allow the script to get and set variables, and call methods in our object.
The implementation of the object must filter for the names of the variables and methods the object exports to the script, but still call the Simkin base class methods for others. An implementation for our ScriptedObject is:
Here you'll notice that we have implemented the private function "foo", which takes an int as a parameter. The C++ object detects that the script wants to call this function within "method". You could easily make foo return a value by inserting the return value of foo into the Simkin returnValue parameter.
Now we need to instantiate on object of our class, giving it the name of our script, which I have made "test.s". We also set up the simkin interpreter here in the main program:
Now comes the fun part - making a script to make our C++ program do things without having to recompile! Here I have made use of the foo function, as well as getting and setting the m_param member variable in the C++ object.
What the script is doing should be pretty self-explanitory, as I've put comments in. If you can get this up and running, you've got almost all you need. To compile the test1 project you will need to setup your directories like mine (Simkin and xerces as sibling directories of the test1 directory), or update the include and library directories to point to your location.
Please download the full sources and executable by clicking here.