Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Godot 4 version ? #1

Open
TheBricktop opened this issue Apr 4, 2023 · 11 comments
Open

Godot 4 version ? #1

TheBricktop opened this issue Apr 4, 2023 · 11 comments

Comments

@TheBricktop
Copy link

Is it possible to port this plugin to Godot 4 format?

@jferdelyi
Copy link
Member

jferdelyi commented May 12, 2023

Yes, it's planned when GDExtension is stable.

@MRIIOT
Copy link

MRIIOT commented Jun 30, 2023

I know there are reasons to use C extensions. C# side of things seems to be working.

using System.Text;
using Godot;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Packets;

public partial class MqttClient : Node
{
	[Export]
	private string _host = @"my.broker";
	
	[Export]
	private int _port = 1883;

	[Export] 
	private string[] _topics = new [] { @"t/#" };
	
	private IMqttClient _client;
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		GD.Print("hello from c#");
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		
	}

	private async void ConnectToggle()
	{
		var factory = new MqttFactory();
		_client = factory.CreateMqttClient();
		var options = new MqttClientOptionsBuilder()
			.WithTcpServer(_host, _port).Build();
		
		_client.ApplicationMessageReceivedAsync += ClientOnApplicationMessageReceivedAsync;
		
		GD.Print($"mqtt client connecting to = {_host}:{_port}...");
		var connect_result = await _client.ConnectAsync(options, CancellationToken.None);
		GD.Print($"mqtt client connection result = {connect_result.ResultCode}.");

		foreach (var topic in _topics)
		{
			GD.Print($"mqtt client subscribing to topic = {topic}...");
			var subscribe_result = await _client.SubscribeAsync(
				new MqttTopicFilter { Topic = topic }, 
				CancellationToken.None);
		}
		
		GD.Print("mqtt client ready");
	}

	private Task ClientOnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
	{
		GD.Print($"mqtt client msg on = {arg.ApplicationMessage.Topic}");
		GD.Print($"mqtt client msg body = {Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment)}");
		return Task.FromResult(0);
	}
}

@SamSmith17
Copy link

Hi there, super noob here. First of all, thanks for working on this project, it's way above my paygrade. Could someone please tell me if my assessment of the current situation is correct? I'm still very new to this so please forgive my ignorance.

  • This repo as of 7/12/2023 is written for Godot 3, and if you try to load it into Godot 4, it will not work out of the box because a some of the GDscript functions have changed between 3 and 4 (I'm getting errors for the MQTT object class and WebSocketClient, which seems to have been replaced with WebSocketPeer?)

  • An extension for Godot 4 exists called GDExtension that adds compatibility with C# scripts, and with that extension added to Godot 4, the C# script above works with Godot 4?

  • GDExtension is NOT currently merged into the latest stable release of Godot 4 (4.0.3 stable at time of writing) and until it is, this repo won't be out-of-the-box compatible with Godot 4.

-BUT if I can figure out how to add the GDExtension to Godot 4, then I can use it to add the C# script above, and it will work?

-AND if I can't figure out how to do that, then this functionality is planned when GDExtension is merged into a future stable release of Godot 4.x?

Do I have all that right? I love the idea of being able to use MQTT to link Godot with my Home Assistant environment, and be able to feed sensor data in and trigger switches from a custom Godot dashboard, but I don't currently have the base knowledge or skills to figure it out the MQTT link by myself and can't find a addon/script for MQTT support that is out-of-the-box compatible with Godot 4.

Thank you!!!

@MRIIOT
Copy link

MRIIOT commented Jul 12, 2023

My understanding is that GDExtensions are the glue into C++ libraries. You would use C++ for speed and compatibility across multiple build targets/platforms. Using C# on the other hand, limits the compatibility to desktop builds (?), but allows you to leverage C# and everything .NET in your projects. The code posted above would work out of the box by referencing the MQTTNet .NET libraries. I typically use JetBrains Rider for .NET development and like with any .NET IDE, you would reference MQTTNet library from Nuget.

@jferdelyi
Copy link
Member

Hello, Godot3 use system called GDNative for C++ plugins. In Godot4 the system was completely rebuild from scratch and called GDExtension. The problem is : GDExtension is not currently mature and the API is susceptible to change one day to another. So I decide to let the Godot4 version in standby for now.

About C#, there is no relations between GDNative/GDExtension and C#. Codes from Godot3 should works in Godot4.

@jferdelyi
Copy link
Member

But if it's really necessary, I can create a prototype using GDExtension. By the way, this plugin is part of a collection of plugins called GDWired (https://github.com/GDWired), I've created another plugin for MQTT using Paho called GDPaho (https://github.com/GDWired/GDPaho) also only for Godot3.

@SamSmith17
Copy link

That is all very helpful, thank you for your clear answers and all your hard work! The BLE support would be really fun to play with as well. I don't really NEED any of this functionality, and I probably wouldn't know how to use most of it anyway, I'm just excited to start playing with realworld data in Godot, and I've only ever used Godot 4. I'll be standing by for Godot 4 support whenever that happens, and following along as the GDWired project develops, thank you!

@SamSmith17
Copy link

SamSmith17 commented Jul 12, 2023

OK, so I decided to just download 3.5 (and 4.1, which apparently came out last week) so that I could see if that unbreaks some the 3.5 projects I've downloaded and had trouble with in 4.0.3.

I was able to load the sample scene with no errors, but I'm not sure what to do with it from there. If I just open the project and run the debug, I see the interface, but the buttons don't seem to do anything and there is no input fields to put in credentials. It seems like I have to put in my MQTT host address, port, username, password, and MQTT topics somewhere in this code, but I can't figure out where.

I see this bit in GDMosquito.gd

##
# About pw_callback
#var pfunc pw_callback(size: 4, rwflag: int) -> String:
#
#func tls_set(p_cafile: String, p_capath: String, p_certfile: String, p_keyfile: String, p_pw_callback: String) -> int:
#
# The string variable p_pw_callback must be the name of a function with this signature:
# 	- func pw_callback(size: int, rwflag: int) -> String:
# Where:
# 	- size is the size of the password buffer (the password should fit on it)
#	- rwflag is ????????
#	- return the paswword
##

And I'm guessing this has something to do with setting the password but I don't understand what to do with it, if anything. And now that I'm looking at it again, there is also a reference to a certfile, which makes me think this is maybe for setting up a new MQTT broker? Is that necessary if I'm already running a Mosquito Broker through my Home Assistant server on my local network?

Is it possible to subscribe a topic (like /homeassistant) and then receive/print MQTT updates from Mosquitto/Home Assistant within the game environment?

Hope these aren't dumb questions, I appreciate your help!

@MRIIOT
Copy link

MRIIOT commented Jul 13, 2023

which makes me think this is maybe for setting up a new MQTT broker?

Mosquitto version 2+, by default, binds to 127.0.0.1 on port tcp/1883 and allows anonymous connection (no user, no password). Certificate is only required if you configure Mosquitto for TLS (encrypted communication). If Mosquitto is running on a different computer than your client (eg. Godot) then you need to modify your Mosquitto configuration to bind to 0.0.0.0 (or a specific host IP address) and enable tcp listener (eg. port 1883).

https://mosquitto.org/man/mosquitto-conf-5.html

@MRIIOT
Copy link

MRIIOT commented Jul 13, 2023

but I'm not sure what to do with it from there

Looks like here is where the connection actually happens.

_mqtt_client.broker_connect(broker_address, broker_port, broker_keep_alive)

@jferdelyi
Copy link
Member

The discution is moved here #2 because no more related to Godot4

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants