How do you use is_var_undef with vars instead of globals?

Hey there,

I’m new to ChaiScript, using 5.8.6 because I’m on VS2013.

I’m trying to use my_var.is_var_undef, like I would with a global, but I can’t sort out the logic.

Here’s my script that I’m running from C++ twice in a row, from the same chaiscript::ChainScript instance because I’d like to persist the state, and I’m not sure how to begin another ‘session’ without creating a new ChaiScript instance.

As aside, create_simple_modal and create_highlighted_text both return custom types of mine, which does work.

puts("---- BEGIN ----");

puts("before var");
var modal;
if (is_var_undef(modal)) {
    puts("before create_simple_modal");
    modal = create_simple_modal("Test modal from script");
}

puts("before is var undef my text");
var my_text;
if (is_var_undef(my_text)) {
    puts("before my_text =");
    my_text = create_highlighted_text_with_static_content("static content here", 12, -1); 
    puts("before modal.addChild");
    modal.addChild(my_text);
}

The first time this script is ran, everything works as I’d expect, a modal and a text gets created, and the following is printed:

CHAI: ---- BEGIN ----
CHAI: before var
CHAI: before create_simple_modal
CHAI: before is var undef my text
CHAI: before my_text =
CHAI: before modal.addChild

but the second time, it sees var modal; and throws a reasonable error about modal already being defined:

CHAI: ---- BEGIN ----
CHAI: before var
CHAI ERROR: Error: "Variable redefined 'modal'" 

I’ve tried doing if (is_var_undef("modal")) which always evaluates to true, which isn’t what I want, and I tried just removing the var modal; line, but then modal isn’t recognized in the script. Switching to globals however, global modal; global my_text; works and I’ll use that if nothing else but I’d love to know if I’m doing this correctly or not.

The end result I’m looking for is basically to only create the modal and text the first time this script is ran, and for nothing to happen the second (or 3rd, or 4th etc) time you run it. I’d prefer not to use a global but it might be needed in this specific usecase.

So my question is, how can I use is_var_undef with a var variable so that I don’t redefine it, like I would a global? Should I just not try to rerun the same script over again? Should I be using globals instead?

Thanks!

Try putting your whole script inside a local block/scope, by putting { and } around it. That way the variable modal actually will be local to each script evaluation (in that case, there’s really no point in checking if it’s undefined, because it always will be).

Otherwise, what you’ve done is just the same as a script that did this:

var modal;
// some other stuff
var modal; // Error: "Variable redefined 'modal'"

versus this, which would be fine:

{
    var modal;
    // some other stuff
}

{
    var modal;
   // and more...
}

Declaring the same variable twice at the same scope is illegal in ChaiScript, as in C++ (although you may override a variable name from an “outer” scope with a local variable).

I hope that helps…

Thanks that makes sense, that var modal; is redefined a second time. So it definitely seems like I’m approaching this from the wrong angle then, but that answers my question thank you.

I’ll probably eval a file that defines modal then make a second re-runnable script that assumes modal exists and create a bunch of texts that way, because if I scope modal within the braces I wouldn’t be able to access it from another script.

Thank you for the answer, I’ve got a lot to learn.