function calls in init exit scope?

10 views
Skip to first unread message

Peter Ebus

unread,
Apr 29, 2025, 5:38:15 AMApr 29
to Glowscript Users
Hi everybody
I've been using vpython for a while to create visualisations for my electromagnetism class. Everything works fine when using vpython, but when I try to run code in Glowscript things stop working. I've been able to narrow it down to the following: It seems that calling functions during initialisation only runs the statement following the function call, then exits the method (see code example below). This seems to happen regardless of scope- I've tried making the function a member of the class, or even the init method itself, with the same result. As I'm no expert on Javascript I don't have a clue why this code works fine in vpython but does not compile to Javascript (or not as expected). Anyone know the reason for this behaviour, and possibly a solution? 
Thanks,
Peter

def red():
    return vec(1,0,0)

class Body:
    def __init__(self, a_pos, a_color):
        #self.my_color=red() # works as expected in vpython, but will only run next statement, then exit __init__() in Web VPython 3.2
        self.my_color = a_color
        self.my_pos = a_pos
        #self.my_color= red() # will not cause error, but will not update self.my_color

    def draw(self):
        arrow(pos=self.my_pos, axis=vec(-1, 0, 0), color=self.my_color)

my_body = Body(a_pos = vec(0, 0, 0), a_color=vec(0,1,0))
my_body.draw()

Max DeMarr

unread,
Apr 29, 2025, 8:32:21 AMApr 29
to Glowscript Users
This is very funny behavior. When probing around at the timing of the current color value inside and outside of the class initialization, I found that it was starting the draw event with old values written to the color. Both of your commented-out lines are perfectly okay, but the draw method is being queued with the old value of the color. It seems that simply adding "sleep(0.01)" after you create the my_body instance of Body ensures that the values are properly updated. I am also not sure as to why this is needed.

Best,
- Maximillian

Peter Ebus

unread,
Apr 29, 2025, 9:19:44 AMApr 29
to Glowscript Users
Hi Maximilian,
This does indeed confirm other behaviour I noticed earlier. Looks like instantiation starts a separate thread. sleep(0) after creating the instance does the trick. Ugly, but at least I have a workaround. Thanks for the update! 
Peter

Harlan Gilbert

unread,
Apr 29, 2025, 10:45:50 AMApr 29
to glowscri...@googlegroups.com
This is part of a larger problem, as there are other behaviors where synchronization of functions is problematic.  (Rate statements in functions rather than the main program.)

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/glowscript-users/1d91f3fa-56fa-469d-bada-94fcb47d8626n%40googlegroups.com.


--
Harlan Gilbert, Ph.D.
High School Math, Physics, Computer Science, and Philosophy Teacher
Collegium Chair
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977
Reply all
Reply to author
Forward
0 new messages