Index out of range URScript functions

I came across this issue over the past few days.

Posting it here more as a FYI to other people who may come across the issue.

If you have mulitple subprograms that use the same urscript containing defined functions, it can cause the urp compiler to generate the runtime code oddly and throw some strange errors including ‘Index out of range’ and ‘Variable not defined’ during runtime. It can occur randomly even after it has worked in previous compilations.

Even if you try to guard the script with a if conditional it will still compile in and cause the issue.

Solution is to define the functions in the main program, and suppress the calls to the script in the subprograms.
If you wish to call and test the subprograms separately you can then un-suppress them.

Hope this helps someone. Cheers.

Hi, @jcorbett. I think I’m facing the same issue these days (consistently “Index out of range”, though). However, I don’t quite understand your explanation of the conditions that cause the issue and neither what you mean by “suppressing” and “unsuppressing” subprograms. (Also, do “sub-programs” refer to “functions defined within the main-function”?)

It is a lot to ask, but I would really appreciate if you could elaborate or illustrate the case with dummy-code, maybe?

Kind Regards.

If you’re getting it consistently, you may just have an actual instance of accessing an element of an array that doesn’t exist. If you have an array of integers called myArray and it was 5 items large (0, 1, 2, 3, 4) but you try to access myArray[5] you’ll get this error. What the original post is outlining is that subprograms brought in using the Subprog node in Polyscope can cause issues if those programs have the same URScript functions/variables as other subprograms/main program. You run into instances of double defining them, and having weird scoping issues. He’s saying the solution is to define all necessary variables/functions only ONCE in the main program, and “suppress” or “comment out” or just straight up delete, the function/variable definitions from the subprograms. That way you would retain only 1 definition and the compiler doesn’t do weird things.

Thank you @eric.feldmann for providing additional insight. We only ever send raw URScript-programs from a remote PC over network to the primary interface. Re-reading the initial comment through “PolyScope-goggles” makes it so much easier to understand, indeed: the redefinition-issue becomes apparent and sub-program obviously refers to that type of node.

As for the problem I was facing (as I finally managed to pin-point):
URScript programs need to be enclosed inside a root/main “def SomeProgramName():” function. From what I encountered here it appears as if it is impossible to “return” from the root-function. According to URScript-documentation “return” needs to be followed by a return-value or by the keyword “None”. I don’t know what the controller/primary-interface uses the main-function’s return value for, but both “return None” and “return 0” cause “Index out of range” errors upon execution. (“return” on its own doesn’t even run. as expected.)
Thus, instead of an early return (as would be valid within “child-functions”):

def SomeProgramName():
    # set_up
    ...
    
    if (not new_additional_safety_check()):
        tear_down()
        return None    # "Index out of range" on teaching-pendant at runtime.
    end

    # long main part
    ...

    #tear_down
    tear_down()
end

one needs enclose within an if-condition that spans over the majority of the program:

def SomeProgramName():
    # set_up
    ...
    
    if (new_additional_safety_check()):
        # long main part
        ...
    end

    #tear_down
    tear_down()
end

I too hope that this addition of mine saves someone else a few headaches in the future. :wink: