Websocket

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
w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Websocket

#1 Post by w022a » Mon May 21, 2018 4:29 pm

Hi there,

I'm pretty new to Ren'Py but absolutely loving the flexibility of this engine, how rapidly one can develop a game and being able to extend with pure Python.

I've been experimenting with Websockets but haven't been able to find any reference implementation.

My requirement is for the game to be able to effectively register for callbacks to a web service. I am flexible the message protocol. Use case is that the game connects to server, establishes a session and then another person can send a message which is displayed as dialogue, or changes a variable. Slightly analogous to chat client/server

I've been able to get Python websocket-client working in ren'py for a short-lived request/response scenario, but as a long-lived connection, it doesn't seen to like threading.

Grateful for any advice on how to implement this.

Thanks

User avatar
PyTom
Ren'Py Creator
Posts: 15168
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Websocket

#2 Post by PyTom » Mon May 21, 2018 6:22 pm

I wouldn't use websockets. Or at least, I'd never had any luck with them. Consider polling using normal web requests.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Websocket

#3 Post by w022a » Tue May 22, 2018 2:58 am

Noted thanks Tom. With that advice, I'm considering either polling via HTTP request via a timer on a screen invoking a call, or using a simple websocket client outside of renpy which writes updates to file and have ren'py read the file, again polling via a timer on a screen invoking a call. Both of these approaches look to work well with ren'py.

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Websocket

#4 Post by w022a » Wed May 23, 2018 1:29 am

I have discovered an issue with my polling implementation

Using a timer action on a screen to call the function that does an http call or file read causes any text or input boxes to be cleared. I think what I need here is a call_in_new_context but that doesn't look like an option in screen language

Any advice on how to run polling code to a timer (which would set a variable) without inteeing with current display elements?

Thanks

User avatar
PyTom
Ren'Py Creator
Posts: 15168
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Websocket

#5 Post by PyTom » Wed May 23, 2018 11:23 am

It shouldn't do that. What does your script look like?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Websocket

#6 Post by w022a » Wed May 23, 2018 4:16 pm

I have now changed the screen to use a function, which is working well. This is the code that was causing we problems

Code: Select all

screen runtimers:

    timer 5 repeat True action Call("readMessage")
and the label called....

Code: Select all

label readMessage:
    python:
        with open("C:/room/game/messages.txt", "r") as msgFile:
            msgRead = str(msgFile.read())
            #assume it's just a one liner for test
            msgFile.close()
    return



In line with the 5s call time, text or text input gets cleared

But, i have now changed the screen statement to use a function i've defined in the init block and this preserves the screen

Code: Select all

screen runtimers:

    timer 5 repeat True action Function(readMsgFile)
this way works just fine

User avatar
PyTom
Ren'Py Creator
Posts: 15168
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Websocket

#7 Post by PyTom » Wed May 23, 2018 5:14 pm

Yes, Call ends the current statement. So you don't want to do that.

Note that reading a file on disk is slow, and the way you're doing it here, it'll block the main thread, which can lead to framedrops.

I'd suggest perhaps using https://www.renpy.org/doc/html/other.ht ... _in_thread in the timer function to invoke a background thread. Really, Ren'Py isn't meant for this sort of thing, so there isn't a great way to do it.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

w022a
Newbie
Posts: 13
Joined: Mon May 21, 2018 4:15 pm
Contact:

Re: Websocket

#8 Post by w022a » Fri May 25, 2018 7:04 am

thanks - invoke_in_thread is working well. It actually is also working well for a basic Websocket implementation, using example chat server implementation from here
https://github.com/dpallot/simple-websocket-server

I'm currently using file-based polling so that the Main thread and the new thread can pass data to each other and not monitoring state of the new thread. I will take a look at using queues.. Although appreciate i'm veering a little way off what Ren'py is designed for - it's really cool POC to be able to interact with the game from a web site.

Post Reply

Who is online

Users browsing this forum: No registered users