Function inside another function not working

33 views
Skip to first unread message

JCH

unread,
May 19, 2018, 11:47:13 PM5/19/18
to Glowscript Users
I'm a beginner to python. In a glowscript program, I have a function (Initialize()) with a while loop in it and when a reset button is pressed, I want the while loop to run all over again (I put the Initialize() function inside the Reset() function). However, I get an error and I have no clue what it means. Does anybody possibly know how to fix it? Or something I did wrong?  When the Initialize() function inside the Reset() function (the highlighted one) is gone, it works fine. But when it is there, I get the error. Any suggestions? Is this even possible? I have no clue what's wrong. Thanks for any help.

Error(s):

Error: error streamlining source: : Function contains async calls but dopes not have _ parameter:
undefined at line 101 STACK: : Function contains async calls but does not have _
parameter: undefined at line 101 at _dolt

null

Relevant Code:

def Reset():
    global ball
    ball.pos.y = initialposy
    ball.vel.y = initialvely
    Initialize()

button(text="Reset", pos=scene.title_anchor, bind=Reset)

Initialize()

def Initialize():
    while True:
        rate(1/deltat)
        if running:
        #Update velocity (for Ek)
            ball.vel.y = ball.vel.y + g*deltat
            print("Velocity = ",ball.vel.y)
            Ek = 0.5*mass*ball.vel.y**2 #48.02
            print("Ek = ", Ek)
            Eg = Etotal - Ek
            ball.pos.y = Eg / (mass*g)
            if (ball.pos.y - ball.radius <= groundHeight):
                break

Bruce Sherwood

unread,
May 20, 2018, 10:35:24 AM5/20/18
to Glowscript Users
There are two somewhat subtle issues here.

1) In the documentation for widgets is this statement:

Important limitation of GlowScript VPython: The bound function cannot contain the statements rate, sleep, pause, waitfor, get_library, or read_local_file, statements that require pausing during execution. This limitation does not apply to VPython 7.

Your Reset function doesn't explicitly contain such statements but it calls the Initialize function which does. The result is a program that GlowScript can't handle. The fundamental issue is that JavaScript, to which your VPython program is compiled to be able to run in the browser, does not permit "synchronous" infinite loops, which lock up the browser. A JavaScript library called Streamline is invoked to rewrite your program to have an "asynchronous" structure. Essentially, contents of your loop are moved to a separate function that is called every t/deltat seconds.

2) It's not a good idea for Reset to call Initialize, which has already been called. Without seeing the entire program I can't be sure, but it's quite likely that if you simply delete the call to Initialize in the Reset function the program will run correctly, in that the next execution of the loop will use the modified valules of ball.pos.y and ball.vel.y.

Bruce
Reply all
Reply to author
Forward
0 new messages