C
C#2mo ago
Based

Godot UDP Client not functioning properly

I am making a Godot project where I have made a basic fighting game which will take inputs from a raspberry pi pico controller i have made which is a UDP server which sends data packets to the UDP client on Godot based on button presses for it then to be interpreted into actions in the game. one of my objects is stuck as null and i cant seem to fix it along with a whole other issue with the packet detection running every frame
53 Replies
Based
BasedOP2mo ago
the code right now is:

using Godot;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.Text;

public partial class ClientUDP : Node
{
private UdpClient client;
private IPEndPoint remoteEndPoint;

public override void _Ready()
{

UdpClient client = new UdpClient();
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.135"), 1738);

// Send data to the server
byte[] data = Encoding.UTF8.GetBytes("UDP client connected");
client.Send(data, data.Length, remoteEndPoint);


client.Close();
}

public override void _Process(double delta)
{
// Called every frame. Delta is time since the last frame.

// Recieve code here
IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 0);
UdpClient client = new UdpClient();
byte[] receivedData = client.Receive(ref receiveEndPoint);
string response = Encoding.UTF8.GetString(receivedData);

GD.Print("THIRD PART WORKS"); //
// Listen for packets
if (response == "RK")
{
GD.Print("RK WORKS!!!!");
}
else if (response == "LK")
{
GD.Print("LK WORKS!!!!");
}
else if (response == "RP")
{
GD.Print("RP WORKS!!!!");
}
client.Close();
}
}

using Godot;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.Text;

public partial class ClientUDP : Node
{
private UdpClient client;
private IPEndPoint remoteEndPoint;

public override void _Ready()
{

UdpClient client = new UdpClient();
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.135"), 1738);

// Send data to the server
byte[] data = Encoding.UTF8.GetBytes("UDP client connected");
client.Send(data, data.Length, remoteEndPoint);


client.Close();
}

public override void _Process(double delta)
{
// Called every frame. Delta is time since the last frame.

// Recieve code here
IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 0);
UdpClient client = new UdpClient();
byte[] receivedData = client.Receive(ref receiveEndPoint);
string response = Encoding.UTF8.GetString(receivedData);

GD.Print("THIRD PART WORKS"); //
// Listen for packets
if (response == "RK")
{
GD.Print("RK WORKS!!!!");
}
else if (response == "LK")
{
GD.Print("LK WORKS!!!!");
}
else if (response == "RP")
{
GD.Print("RP WORKS!!!!");
}
client.Close();
}
}
Based
BasedOP2mo ago
image version:
No description
Mkp
Mkp2mo ago
private UdpClient client;
...
public override void _Ready()
{

UdpClient client = new UdpClient();
private UdpClient client;
...
public override void _Ready()
{

UdpClient client = new UdpClient();
You never initialize client Instead you create a new client every _Process and _Ready call You should only need to instantiate one
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Based
BasedOP2mo ago
is client not initialised on the first line of _Ready?
Mkp
Mkp2mo ago
Nope you create one and assign it to a local variable, meaning that variable only exists in _Ready
Based
BasedOP2mo ago
or am i missing something? oh happy halloween
Mkp
Mkp2mo ago
...
client = new UdpClient();
...
...
client = new UdpClient();
...
will assign the class member client to the value you want (UdpClient)
Based
BasedOP2mo ago
oh i already tried this the error still occurs, let me try again real quick ah wait no its a different error now i forgot E 0:00:01:0304 void System.Net.Sockets.Socket.ValidateReceiveFromEndpointAndState(System.Net.EndPoint, string): System.InvalidOperationException: You must call the Bind method before performing this operation. <C++ Error> System.InvalidOperationException <C++ Source> :0 @ void System.Net.Sockets.Socket.ValidateReceiveFromEndpointAndState(System.Net.EndPoint, string) <Stack Trace> :0 @ void System.Net.Sockets.Socket.ValidateReceiveFromEndpointAndState(System.Net.EndPoint, string) :0 @ int System.Net.Sockets.Socket.ReceiveFrom(System.Byte[], int, int, System.Net.Sockets.SocketFlags, System.Net.EndPoint&) :0 @ System.Byte[] System.Net.Sockets.UdpClient.Receive(System.Net.IPEndPoint&) ClientUDP.cs:33 @ void ClientUDP._Process(double) Node.cs:2131 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&) ClientUDP_ScriptMethods.generated.cs:48 @ bool ClientUDP.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&) CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error, Godot.NativeInterop.godot_variant*)
Mkp
Mkp2mo ago
using Godot;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.Text;

public partial class ClientUDP : Node
{
private UdpClient client = new UdpClient();
private IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.135"), 1738);
private IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 0);

public override void _Ready()
{
// Send data to the server
byte[] data = Encoding.UTF8.GetBytes("UDP client connected");
client.Send(data, data.Length, remoteEndPoint);

client.Flush();
}

public override void _Process(double delta)
{
byte[] receivedData = client.Receive(ref receiveEndPoint);
string response = Encoding.UTF8.GetString(receivedData);

GD.Print("THIRD PART WORKS"); //
// Listen for packets
if (response == "RK")
{
GD.Print("RK WORKS!!!!");
}
else if (response == "LK")
{
GD.Print("LK WORKS!!!!");
}
else if (response == "RP")
{
GD.Print("RP WORKS!!!!");
}
}
}
using Godot;
using Microsoft.Win32;
using System.Net;
using System.Net.Sockets;
using System.Text;

public partial class ClientUDP : Node
{
private UdpClient client = new UdpClient();
private IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.135"), 1738);
private IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 0);

public override void _Ready()
{
// Send data to the server
byte[] data = Encoding.UTF8.GetBytes("UDP client connected");
client.Send(data, data.Length, remoteEndPoint);

client.Flush();
}

public override void _Process(double delta)
{
byte[] receivedData = client.Receive(ref receiveEndPoint);
string response = Encoding.UTF8.GetString(receivedData);

GD.Print("THIRD PART WORKS"); //
// Listen for packets
if (response == "RK")
{
GD.Print("RK WORKS!!!!");
}
else if (response == "LK")
{
GD.Print("LK WORKS!!!!");
}
else if (response == "RP")
{
GD.Print("RP WORKS!!!!");
}
}
}
Based
BasedOP2mo ago
nothing on visual studio though
Mkp
Mkp2mo ago
This is more akin to what you want
Based
BasedOP2mo ago
let me try it
Mkp
Mkp2mo ago
im not sure if flush exists on UdpClient, its convention though so, might be FlushAsync ¯\_(ツ)_/¯
Based
BasedOP2mo ago
no definition for flush apparently
Based
BasedOP2mo ago
No description
Based
BasedOP2mo ago
let me try async
Mkp
Mkp2mo ago
Is there anything in intellisense?
Based
BasedOP2mo ago
still nothing for FlushAsync
Mkp
Mkp2mo ago
Okay then just omit it
Based
BasedOP2mo ago
got it
Based
BasedOP2mo ago
still have 2 warnings on the bottom
No description
Based
BasedOP2mo ago
i think its just a visual studio bug at this point i commented them out at one point and they still gave me the warnings
Mkp
Mkp2mo ago
commenting is definitely not the solution
Based
BasedOP2mo ago
yeah i understand im just saying that the warnings are still given to me despite having removed the code entirely at one point
Mkp
Mkp2mo ago
(did you save??)
Based
BasedOP2mo ago
yeah its just being weird
Mkp
Mkp2mo ago
No description
Mkp
Mkp2mo ago
Did you?
Based
BasedOP2mo ago
yeah since the screenshot
Based
BasedOP2mo ago
No description
Based
BasedOP2mo ago
so it doesnt seem to be giving me any errors in godot but
Mkp
Mkp2mo ago
Those warnings seem old
Based
BasedOP2mo ago
it also doesnt seem to be loading into the game itself yeah they are i think promising results i think i need a client.Close(); at some point the game wont load without it
Mkp
Mkp2mo ago
That doesnt make sense
Based
BasedOP2mo ago
its like stuck on the script so it cant load the game ill throw it in and see if it works
Mkp
Mkp2mo ago
It shouldn't hang on _Ready() You can remove the code from _Process() for now
Based
BasedOP2mo ago
comment it out or just straight up remove it?
Mkp
Mkp2mo ago
comment
Based
BasedOP2mo ago
yeah the client.close seems not great
No description
Based
BasedOP2mo ago
alright done
Mkp
Mkp2mo ago
Yeah it seems like _Process() is at fault
Based
BasedOP2mo ago
removed the clients too yeah i think its this one specifc line byte[] receivedData = client.Receive(ref receiveEndPoint); this one i believe
Mkp
Mkp2mo ago
If it blocks until it gets data, then yeah the game will get hung up
Based
BasedOP2mo ago
yeah i should uh
Mkp
Mkp2mo ago
Which is expected cuz you not using Async
Based
BasedOP2mo ago
i should probably start up the server huh i forgot about that doesnt seem to have helped
Mkp
Mkp2mo ago
Are you sure the PI is 192.168.0.135 and its being hosted on that ip and port? If you hosting on localhost its not gonna work
Based
BasedOP2mo ago
yeah its on that ip and port let me rerun some code to confirm
Based
BasedOP2mo ago
No description
Based
BasedOP2mo ago
No description
Mkp
Mkp2mo ago
idk wtf I am looking at
Based
BasedOP2mo ago
oh yeah its python not c# i shoulda clarified basically the port is 1738 and the ip is 192.168.0.135 of the server i cannot for the life of me figure it out
Want results from more Discord servers?
Add your server