Is there a way to redefine/reload a type in chaiscript?


Just wanted to throw in my 2c for my particular use case which I think is similar:

I am using chaiscript (which I absolutely love by the way) in a mobile game.

Scripting is used for defining levels in the game - so the script calls C++ functions to create landscape, sprites etc. for each level.

I currently have a ‘World’ script that is using “use” to include scripts for the levels



use("w" + to_string(worldNo) + level + ".chai");
where worldNo is the world number and level is the level name - so the scripts are something like


As it stands, I can’t have the same function name in the various level scripts, so I have to name the functions specific to the layer, e.g. L1LoadTerrain() rather than just loadTerrain().

When the player moves from level to level, the ‘use’ function loads the appropriate level script and all is well - but what would be great would be if it could first ‘UnUse’ the previous script.

That way the functions in each level script could be named identically, and the World script becomes very generic - simply calling the loadTerrain() method which will execute the function on the appropriate level.

Further to this, the ability to ‘UnUse’ a script would give the ability of downloading a new or modified script at runtime, which otherwise isn’t possible (unless there is another way?)

The only alternative I’ve managed to use at present is to re-instantiate my chaiscript objects in C++ so if anyone knows of an alternative method …


A few words on my perspective about this feature:

  1. It would be damn useful! Anything that helps modifying systems (in particular in games) without having to reset everything is useful and helps with creativity by tightening the feedback loop (tweak <=> check the result)
  2. It is not clear what exactly is asked. The thing that other languages seem to avoid but would be useful is to be able to change:
    a) functions
    b) data structures
    Without having to change the data currently loaded, OR by making it adapt if it’s a change of data structure.
    To me this looks like very hard to achieve for b) (a would work eaisly).
    The only way I can see to do this in a way that helps workflow is to provide a way to lead a script in the engine but clarifying to the engine that this is a new version of previous program, therefore, it have to “diff” it and apply the diff to the current program structure being loaded.

Is it what you are considering @lefticus? Or do you just consider allowing changing functions?


Inheritance and an overriden function would be another way to consider this…

class test {
  def test() {

class secondtest extends test {
  def test() {

Don’t think ChaiScript is there yet.