Compound magically turning into a Promise

92 views
Skip to first unread message

Geoff Kuenning

unread,
Apr 12, 2021, 3:14:24 PM4/12/21
to Glowscript Users
I'm having big troubles with a program that I need to release to students tomorrow.  I've cut it down to a small sample.  There's a simple function that creates a compound and returns it.  Printing in the function demonstrates that the compound is what I want.  But after I call it, it turns into a Promise, which is undocumented.

Here's a minimal example:

GlowScript 3.1 VPython

scene.width = 640
scene.height = 480

def make_alien(starting_position, starting_vel = vec(0, 0, 0)):
    print("in make_alien")
    alien_body = sphere(size = 1.0*vec(1, 1, 1), pos = vec(0, 0, 0), color = color.green)
    alien_eye1 = sphere(size = 0.3*vec(1, 1, 1), pos = .42*vec(.7, .5, .2), color = color.white)
    alien_objects = [alien_body, alien_eye1]

    print("making compound, pos =", starting_position)

    print("alien objects are", alien_objects)
    com_alien = compound(alien_objects)
    print("setting position")
    com_alien.pos = starting_position
    print("setting velocity")
    com_alien.vel = starting_vel
    print("compound type is", type(com_alien))
    print("compound contents are", com_alien)
    print("returning com_alien")
    return com_alien

print("making alien1")
alien1 = make_alien(starting_position = vec(6, 0, -6), starting_vel = vec(0, 0, -1))
print("alien1 is a", type(alien1))
print("alien1 is", alien1)
print("made alien1", dir(alien1))
print("pos is", alien1.pos)
print("promise offers", dir(Promise))

RATE = 30
scene.autoscale = False
scene.forward = vec(0, -3, -2)

while True:
    rate(RATE)


And here's some sample output:
making alien1
in make_alien
making compound, pos = < 6, 0, -6 >
alien objects are [[object Object], [object Object]]
setting position
setting velocity
compound type is <class 'dict'>
alien1 is a <class 'promise'>
alien1 is [object Promise]
made alien1 []
pos is undefined
promise offers []

Note that "returning com_alien" doesn't appear!  If I comment out the "compound contents are" line, then "returning com_alien" appears but nothing else changes.



Bruce Sherwood

unread,
Apr 12, 2021, 4:25:23 PM4/12/21
to Glowscript Users
Thanks for the clear report. I expect to be able to fix this rather quickly, but here is a workaround:

Use "GlowScript 3.0 VPython" instead of "GlowScript 3.1 VPython".

Comment out these two print statements:

    print("compound contents are", com_alien)
    print("alien1 is", alien1)

Bruce

Bruce Sherwood

unread,
Apr 12, 2021, 11:28:14 PM4/12/21
to Glowscript Users
I've fixed the problem in version 3.1. You should, though, comment out those two print statements. Please let me know whether your full program works properly. Thanks.,

Bruce

Geoff Kuenning

unread,
Apr 13, 2021, 12:30:30 AM4/13/21
to glowscri...@googlegroups.com
Wow, that was fast. It works now (sans prints; they were just
there to try to debug the problem anyway). Thanks!
> --
>
> ---
> You received this message because you are subscribed to a topic
> in the Google Groups "Glowscript Users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/glowscript-users/6vz1rOSJByk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email
> to glowscript-use...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/glowscript-users/46d3436c-888d-4753-b451-ece6b5e7f83bn%40googlegroups.com.

--
Geoff Kuenning ge...@cs.hmc.edu
http://www.cs.hmc.edu/~geoff/

Keep trying, and keep the best.

Bruce Sherwood

unread,
Apr 13, 2021, 11:22:40 AM4/13/21
to Glowscript Users
Scome context: If you click "Share or export this program" and inspect the JavaScript code that is produced using the RapydScript-NG Python-to-JavaScript transpiler, you'll see "async" prepended to your functions and "await" prepended to calls of those functions. This is necessary so that your functions can contain waiting options such as scene.pause(). RapydScript itself doesn't insert the needed async and await elements, that's done by GlowScript after RapydScript's work, by detecting your functions and function calls in the JavaScript code. It has taken nearly two years of work to get this (nearly) right, and occasionally a user reports some very special case that eluded detection, as was your case.

Bruce

Reply all
Reply to author
Forward
0 new messages