How to execute this Python code using button 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
whitewolf0000
Newbie
Posts: 8
Joined: Fri Feb 22, 2019 1:33 pm
Contact:

How to execute this Python code using button action?

#1 Post by whitewolf0000 »

Hello there,
I should probably start this by saying that I have absolutely no knowledge of how Python or RenPy works, I've only started learning a few days ago so please bear with me!

I'm using the code below to implement a feature for the game to check for updates. Note only "check" for updates, it's simply opening a txt file online, grabbing its content, and comparing it with the config.version.

Code: Select all

init python:
    import webbrowser
    import requests
    import threading
    try:
        response = requests.get(
            'https://whatever.com/whatever.txt')
        data = response.text

        if float(data) > float(config.version):
            updateavailable = 1
        else:
            updateavailable = 0
    except Exception as e:
        updateavailable = 3
I very much stole this code (and modified it a bit, added the updateavailabale variable) from this great YouTube tutorial (https://www.youtube.com/watch?v=86-LK-6WUkY) so I'm quite clueless about how this is working, but it's working fantastic!

However, I would like to see how I can have this code executed only when a button (in the main menu) is pressed, and then have a screen to be shown saying that an update is available, or not, or if the checking has failed. As such:

Code: Select all

    
    button:
        image "gui/overlay/button_update_icon.png"
        pos (449, 548)
        action ?
        
Is this possible? I'm still working my way through RenPy and the documentation but couldn't find a working solution.
Thank you in advance.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How to execute this Python code using button action?

#2 Post by _ticlock_ »

Hi, whitewolf0000,

You can do something like this:

Wrapping your code in a function check_update for convenience:

Code: Select all

init python:
    def check_update():
        import webbrowser
        import requests
        import threading
        try:
            response = requests.get(
                'https://whatever.com/whatever.txt')
            data = response.text

            if float(data) > float(config.version):
                updateavailable = 1
            else:
                updateavailable = 0
        except Exception as e:
            updateavailable = 3
        return updateavailable
The function return the value of the variable updateavailable
Adding update_menu screen as you mentioned:

Code: Select all

screen update(updateavailable):

    tag menu
    use game_menu(_("Update"), scroll="viewport"):

        style_prefix "about"
        vbox:
            if updateavailable == 1:
                label _("New version available")
            elif updateavailable == 0:
                label _("This is the latest version")
            else:
                label _("Error occured")
Adding button to main menu:

Code: Select all

    button:
        image "gui/overlay/button_update_icon.png"
        pos (449, 548)
        action ShowMenu("update_menu", check_update())
It is a raw example but I think you get the point.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to execute this Python code using button action?

#3 Post by hell_oh_world »

I'm not sure whether it's safe to call the function like that. Screens refresh every time so it will call the function every time too. So the process might become intensive.
The ideal way would be is to really only check the update when a button is triggered, like what you said in your original post.
i would suggest showing the screen inside the function instead.
i also don't know why you're importing other libraries there as well, since you only used requests in your code. I might be wrong though.

Code: Select all

init python:
  def _check_update():
        import requests
        
        try:
            response = requests.get(
                'https://whatever.com/whatever.txt')
            data = response.text

            if float(data) > float(config.version):
                updateavailable = 1
            else:
                updateavailable = 0
        except Exception as e:
            updateavailable = 3
        return updateavailable

  def check_update():
    rv = _check_update()
    if rv == 1:
      renpy.notify("An update is available.") # you can replace renpy.notify with renpy.run(Show()) to show your custom screen, Show() is documented on renpy.

    elif rv == 0:
      renpy.notify("No update is available.")

    else:
      renpy.notify("Oops! Seems like something went wrong.")

screen your_screen():
  textbutton "Check for Updates":
    action Function(check_update)

whitewolf0000
Newbie
Posts: 8
Joined: Fri Feb 22, 2019 1:33 pm
Contact:

Re: How to execute this Python code using button action?

#4 Post by whitewolf0000 »

@_ticlock_: Thank you! I tried your code, but I think hell_oh_world is correct. Well, I don't know if this is related but using your code the game becomes somewhat laggy? Going from the splashscreen to the main menu takes much longer than before, and clicking on buttons has a delay.

@hell_oh_world: Thanks so much!!! It's working perfectly now! I think I know how the code is working... I think.
I have a button that shows the "update panel" screen, and a button on that screen to check for updates. And it even refreshes the results every time it's clicked, it's wonderful!!!

The imported libraries were just copied along with the code from the tutorial I mentioned, I didn't know they weren't needed. But you're correct, only requests is being used.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3791
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to execute this Python code using button action?

#5 Post by Imperf3kt »

Ren'Py includes a built in updater that can be used to check for updates (and not download them), why not use that?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to execute this Python code using button action?

#6 Post by Remix »

Note too that most of the larger game distribution hubs (play/steam/itch etc) have their own updaters built in and they will do a much better job.

If you are pursuing this your way, you should be thinking about running all this in a background thread anyway. You do not want to lock up your game while you query the server (which might not be quickly reachable) waiting for a response.
Frameworks & Scriptlets:

whitewolf0000
Newbie
Posts: 8
Joined: Fri Feb 22, 2019 1:33 pm
Contact:

Re: How to execute this Python code using button action?

#7 Post by whitewolf0000 »

Remix wrote: Sun Feb 28, 2021 8:05 am Note too that most of the larger game distribution hubs (play/steam/itch etc) have their own updaters built in and they will do a much better job.

If you are pursuing this your way, you should be thinking about running all this in a background thread anyway. You do not want to lock up your game while you query the server (which might not be quickly reachable) waiting for a response.
You're right about the distribution hubs. Although for now, I'm only considering Patreon or other similar platforms for distribution.

How would I go about running this in a background thread? I may or may not have just realized this issue until now!!
Imperf3kt wrote: Sat Feb 27, 2021 4:50 pm Ren'Py includes a built in updater that can be used to check for updates (and not download them), why not use that?
Right, I gave the updater a try, I couldn't really get it to work. I'm not sure if something in my testing process went wrong or what, but using just the 'updates.json' file of a newer version and using the 'update.Updater' command to check it wasn't working, so it seemed like this route was just simpler and more straightforward for me and what I'm trying to do.
That is unless the issue Remix mentioned is hard to fix!!


Overall, this is mostly a temporary little feature, where the user just sees that an update is available and goes to Patreon to download it.
I think in the future once I make the decision to add actual updating to the game, I will definitely purchase a dedicated server and go for the built-in updater.

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

Re: How to execute this Python code using button action?

#8 Post by zmook »

whitewolf0000 wrote: Sun Feb 28, 2021 11:00 am How would I go about running this in a background thread? I may or may not have just realized this issue until now!!
there is a renpy.invoke_in_thread() function, but very little in the way of easily findable demos or examples of use. It should work: pass a function that sets a flag in renpy.store, and test that flag on your menu screen. But it would certainly help if you've coded a threaded callback before.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

whitewolf0000
Newbie
Posts: 8
Joined: Fri Feb 22, 2019 1:33 pm
Contact:

Re: How to execute this Python code using button action?

#9 Post by whitewolf0000 »

zmook wrote: Sun Feb 28, 2021 1:07 pm
whitewolf0000 wrote: Sun Feb 28, 2021 11:00 am How would I go about running this in a background thread? I may or may not have just realized this issue until now!!
there is a renpy.invoke_in_thread() function, but very little in the way of easily findable demos or examples of use. It should work: pass a function that sets a flag in renpy.store, and test that flag on your menu screen. But it would certainly help if you've coded a threaded callback before.
Thank you! I'll be honest, I'm incredibly new to all this, not just Renpy and Python but coding in general and I'm learning as I go, would you mind being more specific?

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

Re: How to execute this Python code using button action?

#10 Post by zmook »

After some digging, I have found this demo from PyTom: https://patreon.renpy.org/news.html

You'll have download the example zip file.

This is nontrivial code to read for someone new to programming, but if you're up for it give it a shot. It's pretty close to what you want.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

whitewolf0000
Newbie
Posts: 8
Joined: Fri Feb 22, 2019 1:33 pm
Contact:

Re: How to execute this Python code using button action?

#11 Post by whitewolf0000 »

zmook wrote: Tue Mar 02, 2021 12:32 am After some digging, I have found this demo from PyTom: https://patreon.renpy.org/news.html

You'll have download the example zip file.

This is nontrivial code to read for someone new to programming, but if you're up for it give it a shot. It's pretty close to what you want.
Oh wow, this is actually a gold mine!! Thank you, I will definitely be studying it. Seems like exactly what I need.

lewishunter
Newbie
Posts: 13
Joined: Sun Aug 01, 2021 8:06 pm
Contact:

Re: How to execute this Python code using button action?

#12 Post by lewishunter »

About news.rpy it should be noted that any Python libraries that comes with it are now included inside Ren'Py so you can delete them (but make sure you delete the verify arguments and any code that ckecks for certifi in those libraries)

Post Reply

Who is online

Users browsing this forum: Google [Bot], mold.FF