Page 1 of 1

Can't update a global variable from a screen action?

Posted: Thu Jun 16, 2022 9:55 am
by zmook
Is this a bug or am I missing something?

I'm trying to periodically update a global variable. The best way I can find to run a timer seems to be to create a very minimal screen with a 'timer' statement in it (is there a better way? I don't know how much overhead there is in creating a screen vs figuring out how to directly start a timer thread or something.)

Anyway, the minimal screen should work, and up to a point it does. I can confirm my function gets called, and it *thinks* it updates the global variable, but the change is never seen outside the function. Is there a screen context gotcha I'm overlooking, or what?

Code: Select all

default g_idle_pose = {}

    def idler_set_pose():
        global g_idle_pose, g_pose_test
        g_idle_pose["eileen"] = "idle1"
        g_pose_test = "idle1"
        print ("test_set_pose called; g_idle_pose=%r" % g_idle_pose)

screen idler():
    timer 3.5 repeat True action Function(idler_set_pose)


label start:
	show screen idler
console output:

Code: Select all

test_set_pose called; g_idle_pose={'eileen':'idle1'}

> g_idle_pose
{ }

> g_pose_test
NameError: name 'g_pose_test' is not defined
If I call idler_set_pose directly from console, it works as expected:

Code: Select all

> idler_set_pose()
None

test_set_pose called; g_idle_pose={'eileen': 'idle1'}

> g_idle_pose
{'eileen': 'idle1'}

Re: Can't update a global variable from a screen action?

Posted: Thu Jun 16, 2022 10:27 am
by plastiekk
zmook wrote:
Thu Jun 16, 2022 9:55 am
II can confirm my function gets called, and it *thinks* it updates the global variable, but the change is never seen outside the function.
It seems your variable "g_pose_test" isn't defined and Renpy ignores the global statement so this var is just used in the function.
try to add:

Code: Select all

default g_pose_test = {}
I wondered about this myself as I used some global variables and later deleted them as they were not useful. I forgot to delete them out of the function either, but no error message was shown. ^.^

Re: Can't update a global variable from a screen action?

Posted: Thu Jun 16, 2022 10:38 am
by Ocelot
IIRC console will not show correct values, unless a new interaction is started. If you add a few say statements to the game and move to the next one after timer is triggered, does console shows correct values?

Re: Can't update a global variable from a screen action?

Posted: Thu Jun 16, 2022 10:53 am
by zmook
Ocelot wrote:
Thu Jun 16, 2022 10:38 am
IIRC console will not show correct values, unless a new interaction is started. If you add a few say statements to the game and move to the next one after timer is triggered, does console shows correct values?
Aha! Maybe that's it. Advancing to the next say statement causes the changed global variables to become visible (though calling renpy.restart_interaction() from console does not).

Re: Can't update a global variable from a screen action?

Posted: Thu Jun 16, 2022 11:16 am
by Alex
zmook wrote:
Thu Jun 16, 2022 10:53 am
Ocelot wrote:
Thu Jun 16, 2022 10:38 am
IIRC console will not show correct values, unless a new interaction is started. If you add a few say statements to the game and move to the next one after timer is triggered, does console shows correct values?
Aha! Maybe that's it. Advancing to the next say statement causes the changed global variables to become visible (though calling renpy.restart_interaction() from console does not).
Try to add renpy.restart_interaction() to your function.