Websocket
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.
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.
Websocket
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
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
- PyTom
- Ren'Py Creator
- Posts: 16096
- 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
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(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
Re: Websocket
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.
Re: Websocket
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
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
- PyTom
- Ren'Py Creator
- Posts: 16096
- 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
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(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
Re: Websocket
I have now changed the screen to use a function, which is working well. This is the code that was causing we problems
and the label called....
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
this way works just fine
Code: Select all
screen runtimers:
timer 5 repeat True action Call("readMessage")
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)
- PyTom
- Ren'Py Creator
- Posts: 16096
- 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
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.
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(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
Re: Websocket
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.
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.
Who is online
Users browsing this forum: No registered users