How to test for the existence of a var added via add_global


Sorry for the noob question but my google fu has completely failed me here.
I need to update values added via add_global. These updates are coming from my C++ code. I’ve tried simply re-adding the value (failed). I could probably sort out how to update via a script but I cannot figure out how to test if the global already exists.


A couple of options:

You could look at this code:

For looking to see “does an object of a certain name accessible?” Technically that doesn’t tell you if it’s a global just if it’s visible.

There’s also this discussion on how to in general detect if objects of various types exist:

Not all of these functions are publicly accessible from C++, but they can be made accessible.


Thank you lefticus. I added a bit to my local copy of chaiscript. The main addition is in dispatchkit.hpp:

    void set_global(const Boxed_Value &obj, const std::string &name)

        chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);

        const auto itr = m_state.m_global_objects.find(name);
        if (itr != m_state.m_global_objects.end())
        } else {
            m_state.m_global_objects.insert(std::make_pair(name, obj));

Of course, now I am hanging out the window. Is it possible to get this small addition included in the project?

Thanks again for you help.


Certainly, fork ChaiScript on github, commit your changes, then initiate a pull request. That way we can keep everything organize.