Create a loading screen when sending server request

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
bp7
Newbie
Posts: 1
Joined: Wed Sep 25, 2019 8:29 am
Contact:

Create a loading screen when sending server request

#1 Post by bp7 » Wed Sep 25, 2019 8:47 am

Hi All

I am new on RenPy, I am testing some possibility on the engine.

I am trying to do below but no help in coding, and seeking for advice:

1. I am using a input to ask for data
2. Popup a loading screen after enter
3. Use urllib2 to send request to server and wait for response
4. Off the loading screen
5. Output the result in dialog

My screen coding as below:

Code: Select all

screen overlay_loading():
    tag loading
    zorder 80
    modal True

    vbox:
        xalign 0.5
        yalign 0.5

        text "{color=ffffff}Loading...{/color}"
When i am working on 2, if the screen is opened, step 3 will not run and hold

So I have tried use renpy.invoke_in_thread in step 3 to call a function, and close the screen (step 4) within it
But after that, I cannot go back to main thread to do step 5

Below is part of code for step 3 without calling any loading screen

Code: Select all

label inputsearch:
    $ searchdata = renpy.input("Input your search content!")
    $ searchdata = searchdata.strip()
    jump check_inputsearch

label check_inputsearch:
    if not searchdata:
        "Search content cannot empty!"
        jump inputsearch
    else:
        jump senddata
        
label senddata:
    $ myresult = _("")

    init python:
        import urllib, urllib2, socket, json
        
        def function_loading(isopen):
            if isopen:
                renpy.call_screen("overlay_loading")
            else:
                renpy.hide_screen("loading") 

        def function_searchdata(inputdata):
            try:
                print(inputdata)

                data = urllib.urlencode({ "r" : inputdata }).encode("utf-8")

                req = urllib2.Request("http://apicall/")
                req.add_header('Content-Type','application/x-www-form-urlencoded; charset=utf-8')
                
                responsedata = urllib2.urlopen(req,data,30)
                result = json.loads(responsedata.read())

                global myresult 
                myresult = result['d']
                #function_loading(False)
                print("off")
                print(myresult)
            except urllib2.URLError as e:
                print type(e)    #not catch
            except socket.timeout as e:
                print type(e)    #catched
                #raise MyException("There was an error: %r" % e)


    python:

        #renpy.call_screen("overlay_loading")
        
        function_searchdata(searchdata)
        #renpy.invoke_in_thread(function_searchdata, searchdata)
        #function_loading(True)

    e "Return result: [myresult]"

    # This ends the game.

    return
I am using RenPy 7.3.2.320

Thanks for reading.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1433
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: xavimat
Location: Italy
Contact:

Re: Create a loading screen when sending server request

#2 Post by xavimat » Thu Sep 26, 2019 1:25 pm

I haven't checked all your code, but you probably need renpy.show_screen() instead of renpy.call_screen().

Also, don't put "init python" inside a label. Renpy will execute all "init whatever" at init time, so it's wasting indentation and your code is more confusing.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Post Reply

Who is online

Users browsing this forum: Google [Bot], tapanojum