[Solved] Some global variables problem

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
Jetar
Newbie
Posts: 8
Joined: Sat May 21, 2022 10:05 am
Contact:

[Solved] Some global variables problem

#1 Post by Jetar »

Hello! I've making a point&click minigame for my novel. I'm using slightly updated through inheritance SpriteManager. And I need to get counter on the screen, where I can count sprites left on screen.

I've got that label in game.rpy:

Code: Select all

label minigame:
    $ items = dict(
         item1 = ((995, 1206), (428, 475)),
         item2 = ((1110, 1147), (730, 837)),
         item3 = ((1446, 1614), (684, 820)),
         item4 = ((998, 1065), (566, 588)),
         item5 = ((1568, 1719), (327, 466)),
         item6 = ((921, 957), (463, 571)),)
    )
    $ image_name = "background"
    $ SM = SpriteManager_with_mask(event=events_func)
    $ point_and_click_init(items, image_name)
    scene minigame_background
    call screen SM_screen
And then in another file logic.rpy I've got that python funcs:

Code: Select all

    def point_and_click_init(items, image_name):
        sprites = []
        for item in items:
            idle_image = Image("props/{}_{}-idle.png".format(image_name, item))
            hover_image = Image("props/{}_{}-hover.png".format(image_name, item))
            t = Transform(child=idle_image)
            sprites.append(SM.create(t))
            sprites[-1].type = item
            sprites[-1].idle_image = idle_image
            sprites[-1].hover_image = hover_image
            sprites[-1].maskx = items.get(item)[0]
            sprites[-1].masky = items.get(item)[1]
        global sprites

Code: Select all

    def events_func(event, x, y, at):
        minigame_counter = len(sprites)
        def transform_hover(item):
            t = Transform(child=item.hover_image)
            item.set_child(t)
            SM.redraw(0)

        def pop_item(item):
            sprites.pop(sprites.index(item))
	    store.minigame_counter -= 1
            item.destroy()
            SM.redraw(0)

        if event.type == renpy.pygame_sdl2.MOUSEMOTION:
            for item in sprites:
                if item.maskx[0] < x < item.maskx[1] and item.masky[0] < y < item.masky[1]:
                    transform_hover(item)
                else:
                    t = Transform(child=item.idle_image)
                    item.set_child(t)
                    SM.redraw(0)
        if event.type == renpy.pygame_sdl2.MOUSEBUTTONUP:
            if event.button == 1:
                for item in sprites:
                    if item.maskx[0] < x < item.maskx[1] and item.masky[0] < y < item.masky[1]:
                        pop_item(item)
                    if len(sprites) == 0:
                        print('GOTIT')
                        renpy.jump("needed_label")
        global minigame_counter
I got counter in the game.rpy, but it doesent work:

Code: Select all

screen minigame_counter_screen:
    zorder 100
    hbox:
        spacing 10
        xalign 0.02
        yalign 0.02
        $ S = store.minigame_counter
        text "{color=#000}Items left{/color}"
        text "{color=#000}[S]{/color}"
It is not updated in real time, after pop_item func called, just show me 6 and then 1 at the end of the game. I just can't understand, what am I doing wrong. global sprites working well.
Last edited by Jetar on Sun May 22, 2022 9:57 am, edited 2 times in total.

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

Re: Some global variables problem

#2 Post by Alex »

Jetar wrote: Sat May 21, 2022 10:14 am ...It is not updated in real time, after pop_item func called, just show me 6 and then 1 at the end of the game. I just can't understand, what am I doing wrong. global sprites working well.
You need to restart interaction to see updated value onscreen. If you able to click through some dialog while your minigame is shown you'll se the changes in variable. Screen actions are use restart interaction internally, so using SetVariable() action will show the new value immediatelly.

Try

Code: Select all

        def pop_item(item):
            sprites.pop(sprites.index(item))
	    store.minigame_counter -= 1
            item.destroy()
            SM.redraw(0)
            renpy.restart_interaction()
https://www.renpy.org/doc/html/other.ht ... nteraction

User avatar
Jetar
Newbie
Posts: 8
Joined: Sat May 21, 2022 10:05 am
Contact:

Re: Some global variables problem

#3 Post by Jetar »

Alex wrote: Sat May 21, 2022 4:30 pmScreen actions are use restart interaction internally, so using SetVariable() action will show the new value
Yes, I'm using SetVariable() in Screens, but here I've changed variables in python func, not in Screen. It worked! renpy.restart_interaction() updates variable in the given Screen in real time by restarting that Screen interaction. Thank you!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]