More docs adjustment
This commit is contained in:
parent
5b45e40144
commit
50f6182770
1 changed files with 48 additions and 47 deletions
|
|
@ -1,20 +1,14 @@
|
||||||
# Godot Websocket
|
# Godot Websocket
|
||||||
|
|
||||||
_Contrib by ChrisLR 2022_
|
Contribution by ChrisLR, 2022
|
||||||
|
|
||||||
|
|
||||||
# Overview
|
|
||||||
|
|
||||||
This contrib allows you to connect a Godot Client directly to your mud,
|
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.
|
and display regular text with color in Godot's RichTextLabel using BBCode.
|
||||||
|
|
||||||
You can use Godot to provide advanced functionality with proper Evennia support.
|
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.
|
You need to add the following settings in your settings.py and restart evennia.
|
||||||
|
|
||||||
```python
|
```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.
|
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
|
The tl;dr of it is to connect using a Godot Websocket using the port defined above.
|
||||||
will not go into details.
|
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
|
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
|
# The URL we will connect to
|
||||||
export var websocket_url = "ws://localhost:4008"
|
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.
|
This will allow you to connect to your mud.
|
||||||
|
|
||||||
After that you need to properly handle the data sent by evennia.
|
After that you need to properly handle the data sent by evennia.
|
||||||
|
|
||||||
To do this, you should replace your `_on_data` method.
|
To do this, you should replace your `_on_data` method.
|
||||||
|
|
||||||
You will need to parse the JSON received to properly act on the data.
|
You will need to parse the JSON received to properly act on the data.
|
||||||
|
|
||||||
Here is an example
|
Here is an example
|
||||||
```
|
```
|
||||||
var data = _client.get_peer(1).get_packet().get_string_from_utf8()
|
func _on_data():
|
||||||
var json_data = JSON.parse(data).result
|
# The following two lines will get us the data from Evennia.
|
||||||
if json_data[0] == 'text':
|
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]:
|
for msg in json_data[1]:
|
||||||
label.append_bbcode(msg)
|
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
|
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.
|
||||||
It can be anything you would provide to the Evennia `msg` function, more on that later.
|
|
||||||
|
|
||||||
The second element will be the data related to the type of message, in this case it is a list of text to display.
|
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.
|
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
|
If you want anything better than fancy text in Godot, you will have
|
||||||
|
|
||||||
to leverage Evennia's OOB to send extra data.
|
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).
|
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
|
Evennia
|
||||||
```python
|
```python
|
||||||
|
|
@ -97,10 +102,11 @@ Godot
|
||||||
```gdscript
|
```gdscript
|
||||||
func _on_data():
|
func _on_data():
|
||||||
...
|
...
|
||||||
|
|
||||||
if json_data[0] == 'text':
|
if json_data[0] == 'text':
|
||||||
for msg in json_data[1]:
|
for msg in json_data[1]:
|
||||||
label.append_bbcode(msg)
|
label.append_bbcode(msg)
|
||||||
|
|
||||||
|
# Notice the first element is the name of the kwarg we used from evennia.
|
||||||
elif json_data[0] == 'coordinates':
|
elif json_data[0] == 'coordinates':
|
||||||
var coords_data = json_data[2]
|
var coords_data = json_data[2]
|
||||||
player.set_pos(coords_data)
|
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
|
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.
|
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.
|
- Background colors are only supported by Godot 4.
|
||||||
|
|
||||||
|
|
||||||
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.
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue