C
C#7d 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
BasedOP7d 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
BasedOP7d ago
image version:
No description
Mkp
Mkp7d 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 User7d ago
Message Not Public
Sign In & Join Server To View
Based
BasedOP7d ago
is client not initialised on the first line of _Ready?
Mkp
Mkp7d ago
Nope you create one and assign it to a local variable, meaning that variable only exists in _Ready
Based
BasedOP7d ago
or am i missing something? oh happy halloween
Mkp
Mkp7d ago
...
client = new UdpClient();
...
...
client = new UdpClient();
...
will assign the class member client to the value you want (UdpClient)
Based
BasedOP7d 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
Mkp7d 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
BasedOP7d ago
nothing on visual studio though
Mkp
Mkp7d ago
This is more akin to what you want
Based
BasedOP7d ago
let me try it
Mkp
Mkp7d ago
im not sure if flush exists on UdpClient, its convention though so, might be FlushAsync ¯\_(ツ)_/¯
Based
BasedOP7d ago
no definition for flush apparently
Based
BasedOP7d ago
No description
Based
BasedOP7d ago
let me try async
Mkp
Mkp7d ago
Is there anything in intellisense?
Based
BasedOP7d ago
still nothing for FlushAsync
Mkp
Mkp7d ago
Okay then just omit it
Based
BasedOP7d ago
got it
Based
BasedOP7d ago
still have 2 warnings on the bottom
No description
Based
BasedOP7d 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
Mkp7d ago
commenting is definitely not the solution
Based
BasedOP7d 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
Mkp7d ago
(did you save??)
Based
BasedOP7d ago
yeah its just being weird
Mkp
Mkp7d ago
No description
Mkp
Mkp7d ago
Did you?
Based
BasedOP7d ago
yeah since the screenshot
Based
BasedOP7d ago
No description
Based
BasedOP7d ago
so it doesnt seem to be giving me any errors in godot but
Mkp
Mkp7d ago
Those warnings seem old
Based
BasedOP7d 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
Mkp7d ago
That doesnt make sense
Based
BasedOP7d ago
its like stuck on the script so it cant load the game ill throw it in and see if it works
Mkp
Mkp7d ago
It shouldn't hang on _Ready() You can remove the code from _Process() for now
Based
BasedOP7d ago
comment it out or just straight up remove it?
Mkp
Mkp7d ago
comment
Based
BasedOP7d ago
yeah the client.close seems not great
No description
Based
BasedOP7d ago
alright done
Mkp
Mkp7d ago
Yeah it seems like _Process() is at fault
Based
BasedOP7d ago
removed the clients too yeah i think its this one specifc line byte[] receivedData = client.Receive(ref receiveEndPoint); this one i believe
Mkp
Mkp7d ago
If it blocks until it gets data, then yeah the game will get hung up
Based
BasedOP7d ago
yeah i should uh
Mkp
Mkp7d ago
Which is expected cuz you not using Async
Based
BasedOP7d ago
i should probably start up the server huh i forgot about that doesnt seem to have helped
Mkp
Mkp7d 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
BasedOP7d ago
yeah its on that ip and port let me rerun some code to confirm
Based
BasedOP7d ago
No description
Based
BasedOP7d ago
No description
Mkp
Mkp7d ago
idk wtf I am looking at
Based
BasedOP6d 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