Cabbage Logo
Back to Cabbage Site

Array Weirdness

I am using an opcode to return the array index of the input that contains a 1. I am using this with arrays of different sizes, so the first thing I do is get the ‘lenarray’ for the input.

My issue is that when I run this code within the actual program, it does not function properly and always returns -1 for lenarray, but as soon as I copy that same opcode into a stripped down program, it runs perfectly.

How can I troubleshoot this?

Ok, this lead me to wonder what the size of the array being passed, and even that came back with -1.

<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")

checkbox bounds(10, 10, 40, 40) channel("p1") radioGroup("pgrp")
checkbox bounds(10, 30, 40, 40) channel("p2") radioGroup("pgrp") 
checkbox bounds(10, 50, 40, 40) channel("p3") radioGroup("pgrp")
checkbox bounds(10, 70, 40, 40) channel("p4") radioGroup("pgrp")

keyboard bounds(8, 158, 381, 95)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1

instr 2
    SQrtObjSelect[] = fillarray("p1", "p2", "p3", "p4")
    kVal[] cabbageGetValue SQrtObjSelect
    if changed(kVal[0], kVal[1], kVal[2], kVal[3]) == 1 then
        printsk "len: %d\n", lenarray(kVal)
    endif
endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
i 2 0 -1
f0 z
</CsScore>
</CsoundSynthesizer>

I ran the above program in both 2.8 and 2.8.46, and both of them return -1 for the length of ‘kVal’.

You are using the i-rate version of lenarray, try lenarray:k() :+1:

Just when you thought you had that whole rate thing figured out…

Thanks, Rory.

I know. It’s crazy. But I think if you are calling opcodes in a functional way you should always, always, specify the rate. It will save you a lot of time.

I will say that I do like the functional syntax for Csound more than the original form… So, yeah, I should get in the habit of including the rate extension for the functional name.