Variable Not Defined Error Gen Codebox

Emmett Palaima's icon

Hi, I'm using gen codebox on a project for the first time, trying to figure out the syntax.

I'm getting an error saying a variable hasn't been declared that I have clearly declared just a few lines prior. What is causing this?

Error

Here is a screenshot of my error which states:

variable xdir is not defined

Here's the relevant code section:

if (reset == 1) {
    reset = 0;
    x = 3;
    y = 3;
    xdir = 1; //xdir clearly declared here 
    ydir = 0;
    len = 1;
    step = 0;
    change = 0;

    // Initialize the array with a spiral pattern
    for (i = 0; i < 64; i += 1) {
        poke(out_list, x + (y * 8), i + 1);
        x += xdir; //says xdir is not declared

xdir is very clearly defined as a variable just a few lines prior. Why is this happening?

Here is my complete codebox:

Data out_val(64);
Data out_list(64);

History reset(1);



amp = floor(in1 * 64);

if (reset == 1) {
    reset = 0;
    x = 3;
    y = 3;
    xdir = 1;
    ydir = 0;
    len = 1;
    step = 0;
    change = 0;

    // Initialize the array with a spiral pattern
    for (i = 0; i < 64; i += 1) {
        poke(out_list, x + (y * 8), i + 1);
        x += xdir;
        y += ydir;
        step += 1;
        if (step == len) {
            change += 1;
            if ((change % 2) == 0) {
                len += 1;
            }
            step = 0;
            if (xdir == 1) {
                xdir = 0;
                ydir = 1;
            } else if (ydir == 1) {
                xdir = -1;
                ydir = 0;
            } else if (xdir == -1) {
                xdir = 0;
                ydir = -1;
            } else if (ydir == -1) {
                xdir = 1;
                ydir = 0;
            }
        }
    }
}

for(i = 0; i < 64; i += 1){
	poke(out_val, 0, i);
}

for(i = 0; i < amp; i += 1){
	index = peek(out_list, i);
	poke(out_val, 1, index);
}


out1 = peek(out_val, 0);
out2 = peek(out_val, 1);
out3 = peek(out_val, 2);
out4 = peek(out_val, 3);
out5 = peek(out_val, 4);
out6 = peek(out_val, 5);
out7 = peek(out_val, 6);
out8 = peek(out_val, 7);
out9 = peek(out_val, 8);
out10 = peek(out_val, 9);
out11 = peek(out_val, 10);
out12 = peek(out_val, 11);
out13 = peek(out_val, 12);
out14 = peek(out_val, 13);
out15 = peek(out_val, 14);
out16 = peek(out_val, 15);
out17 = peek(out_val, 16);
out18 = peek(out_val, 17);
out19 = peek(out_val, 18);
out20 = peek(out_val, 19);
out21 = peek(out_val, 20);
out22 = peek(out_val, 21);
out23 = peek(out_val, 22);
out24 = peek(out_val, 23);
out25 = peek(out_val, 24);
out26 = peek(out_val, 25);
out27 = peek(out_val, 26);
out28 = peek(out_val, 27);
out29 = peek(out_val, 28);
out30 = peek(out_val, 29);
out31 = peek(out_val, 30);
out32 = peek(out_val, 31);
out33 = peek(out_val, 32);
out34 = peek(out_val, 33);
out35 = peek(out_val, 34);
out36 = peek(out_val, 35);
out37 = peek(out_val, 36);
out38 = peek(out_val, 37);
out39 = peek(out_val, 38);
out40 = peek(out_val, 39);
out41 = peek(out_val, 40);
out42 = peek(out_val, 41);
out43 = peek(out_val, 42);
out44 = peek(out_val, 43);
out45 = peek(out_val, 44);
out46 = peek(out_val, 45);
out47 = peek(out_val, 46);
out48 = peek(out_val, 47);
out49 = peek(out_val, 48);
out50 = peek(out_val, 49);
out51 = peek(out_val, 50);
out52 = peek(out_val, 51);
out53 = peek(out_val, 52);
out54 = peek(out_val, 53);
out55 = peek(out_val, 54);
out56 = peek(out_val, 55);
out57 = peek(out_val, 56);
out58 = peek(out_val, 57);
out59 = peek(out_val, 58);
out60 = peek(out_val, 59);
out61 = peek(out_val, 60);
out62 = peek(out_val, 61);
out63 = peek(out_val, 62);
out64 = peek(out_val, 63);
Magnus Åström's icon

Sorry for reviving an old thread, and the OP will have resolved this long ago. But having ran into the same error, I just wanted to throw this out there, for others!

In GenExpr, it appears that temporary variables are created when they're first assigned, and that this can't be inside a conditional statement block. It kind of makes sense, since the value could be undefined if the condition isn't met, and it kind does not make sense, as this perhaps isn't typically how one would expect things to work, coming from other programming languages.

So basically, in this case, the temporary variables x, y, xdir, ydir, etc would have to be assigned before the first if statement. Annoying as this may be (and afaik, undocumented) I this "feature" actually reminds us of the fact that variables don't persist in GenExpr: on the next run they will be undefined again. I wish the compiler error text was a bit more explanatory though!