Godot Version
4.3 Stable
Question
I need to point out first that any answer will be welcome even if that just a suggestion. I need some of your advice and possibility to know how to make it more efficient and accurate as possible.
So i have a project with a python and a godot. It’s a youtube live chat fetching thing.
For the Python im using the pytchat · PyPI
The system that i use to send the data is save into an json file, contain an attachment from the pytchat. This is the example of the python script
import pytchat
chat = pytchat.create(video_id="uIx8l2xlYVY")
while chat.is_alive():
for c in chat.get().sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
... And More (Saved into Json File)...
Then when it done converted to json, i compare the file of the json and make it into the label for the script
var message_data: Dictionary = {}
var last_author: String = ""
var last_message: String = ""
var author: String = ""
var message: String = ""
func _process(delta: float) -> void:
if Is_Login:
message_data = load_json(data_file_path)
if message_data != {}:
author = message_data["author"]
message = message_data["message"]
if last_message != message or last_author != author and message != null:
_add_chat(author, message)
print("Sending Message into BubbleChat...")
last_message = message
last_author = author
This is working finely, but i think it has a flaw…
- By using _process it will fetch every time and it was a waste of resource (If the data is still the same)
- The dictionary data will need to open the json file first, and then compare it again and again and again (For the elapse of _process)
Of course i know i can use something like await
await get_tree().create_timer(0.2).timeout
or even a timer like this
func _ready() -> void:
timer.connect("timeout", _on_chat_fetch_timeout)
func _on_chat_fetch_timeout -> void:
if Is_Login:
message_data = load_json(data_file_path)
if message_data != {}:
author = message_data["author"]
message = message_data["message"]
if last_message != message or last_author != author and message != null:
_add_chat(author, message)
print("Sending Message into BubbleChat...")
last_message = message
last_author = author
But with that solution is stil not the best one yet because of the accuracy. (It has a chance to skip the message if the chat from the python is sended more than one before the timer / await is done)
So do you guys have any advice or suggestion? whether if is possible to make some signal from python into gdscript or something? or to make the value from python into gdscript without convert it into json first?