From 66e46f6c9b2a25955c6706bcdb5bbe8645f7c1d1 Mon Sep 17 00:00:00 2001 From: ChrisLR Date: Sun, 27 Nov 2022 18:30:44 -0500 Subject: [PATCH] More docs adjustment --- .../base_systems/godotwebsocket/README.md | 95 ++++++++++--------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/evennia/contrib/base_systems/godotwebsocket/README.md b/evennia/contrib/base_systems/godotwebsocket/README.md index 6917f445fc..92d7b9c089 100644 --- a/evennia/contrib/base_systems/godotwebsocket/README.md +++ b/evennia/contrib/base_systems/godotwebsocket/README.md @@ -1,20 +1,14 @@ # Godot Websocket -_Contrib by ChrisLR 2022_ - - -# Overview +Contribution by ChrisLR, 2022 This contrib allows you to connect a Godot Client directly to your mud, - and display regular text with color in Godot's RichTextLabel using BBCode. - You can use Godot to provide advanced functionality with proper Evennia support. -# How to make a basic install +## Installation -### Evennia side You need to add the following settings in your settings.py and restart evennia. ```python @@ -24,69 +18,80 @@ GODOT_CLIENT_WEBSOCKET_CLIENT_INTERFACE = "127.0.0.1" ``` This will make evennia listen on the port 4008 for Godot. +You can change the port and interface as you want. -### Godot side +## Usage -This section assumes you have knowledge for using Godot and -will not go into details. +The tl;dr of it is to connect using a Godot Websocket using the port defined above. +It will let you transfer data from Evennia to Godot, allowing you +to get styled text in a RichTextLabel with bbcode enabled or to handle +the extra data given from Evennia as needed. + + +This section assumes you have basic knowledge on how to use Godot. +You can read the following url for more details on Godot Websockets +and to implement a minimal client. -You can follow the minimal client tutorial at https://docs.godotengine.org/en/stable/tutorials/networking/websocket.html -Then you need to change the websocket_url for your mud, like +Then at the top of the file you must change the url to point at your mud. ``` +extends Node + # The URL we will connect to export var websocket_url = "ws://localhost:4008" -``` - -Also remove the protocol from -``` -var err = _client.connect_to_url(websocket_url, ["lws-mirror-protocol"]) ``` -to + +You must also remove the protocol from the `connect_to_url` call made +within the `_ready` function. ``` -var err = _client.connect_to_url(websocket_url) +func _ready(): + # ... + # Change the following line from this + var err = _client.connect_to_url(websocket_url, ["lws-mirror-protocol"]) + # To this + var err = _client.connect_to_url(websocket_url) + # ... ``` This will allow you to connect to your mud. - After that you need to properly handle the data sent by evennia. - To do this, you should replace your `_on_data` method. - You will need to parse the JSON received to properly act on the data. - Here is an example ``` -var data = _client.get_peer(1).get_packet().get_string_from_utf8() -var json_data = JSON.parse(data).result -if json_data[0] == 'text': - for msg in json_data[1]: - label.append_bbcode(msg) +func _on_data(): + # The following two lines will get us the data from Evennia. + var data = _client.get_peer(1).get_packet().get_string_from_utf8() + var json_data = JSON.parse(data).result + # The json_data is an array + + # The first element informs us this is simple text + # so we add it to the RichTextlabel + if json_data[0] == 'text': + for msg in json_data[1]: + label.append_bbcode(msg) + + # Always useful to print the data and see what we got. + print(data) ``` The first element is the type, it will be `text` if it is a message - -It can be anything you would provide to the Evennia `msg` function, more on that later. - +It can be anything you would provide to the Evennia `msg` function. The second element will be the data related to the type of message, in this case it is a list of text to display. - Since it is parsed BBCode, we can add that directly to a RichTextLabel by calling its append_bbcode method. -# Advanced usage - If you want anything better than fancy text in Godot, you will have - to leverage Evennia's OOB to send extra data. You can [read more on OOB here](https://www.evennia.com/docs/latest/OOB.html#oob). -Example +In this example, we send coordinates whenever we message our character. Evennia ```python @@ -97,10 +102,11 @@ Godot ```gdscript func _on_data(): ... - if json_data[0] == 'text': for msg in json_data[1]: label.append_bbcode(msg) + + # Notice the first element is the name of the kwarg we used from evennia. elif json_data[0] == 'coordinates': var coords_data = json_data[2] player.set_pos(coords_data) @@ -109,16 +115,11 @@ func _on_data(): ``` A good idea would be to set up Godot Signals you can trigger based on the data - you receive, so you can manage the code better. -# Known Issues +## Known Issues -Sending SaverDicts and similar objects straight from Evennia .DB will cause issues, +- Sending SaverDicts and similar objects straight from Evennia .DB will cause issues, + cast them to dict() or list() before doing so. -cast them to dict() or list() before doing so. - - -Godot 3.x Richtext does not support background colors, it will be supported - -in Godot 4.x but the Websockets implementation changes in that version. \ No newline at end of file +- Background colors are only supported by Godot 4. \ No newline at end of file