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

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

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

#1 Post by zmook » Thu Jun 16, 2022 9:55 am

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'}
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
plastiekk
Regular
Posts: 40
Joined: Wed Sep 29, 2021 4:08 am
Discord: plastiekk#3072
Contact:

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

#2 Post by plastiekk » Thu Jun 16, 2022 10:27 am

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. ^.^
Why on earth did I put the bread in the fridge?

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

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

#3 Post by Ocelot » 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?
< < insert Rick Cook quote here > >

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

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

#4 Post by zmook » 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).
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

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

#5 Post by Alex » Thu Jun 16, 2022 11:16 am

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.

Post Reply

Who is online

Users browsing this forum: Google [Bot]