Adise
Adise
CC#
Created by ROBERT311 on 4/6/2025 in #help
magic code???
Hello Could you provide more context? What exactly is failing, is it periodic (Works only on first build or works then does not then works then does not...)? What's the error u get?
16 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
!solved
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Thank yo so much everyone for helping me out!
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
As always, it turns out to be the stupidest crap
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Yep, definetly this. I've now made sure to initialize the parent class on unity's start method instead of directly and works as expected. I think the root of the problem is that the Udp class is a member of a client class that is a member of a monoBehaviour, and it trigered when compiled but it re-triggered on entering play, resulting in the old listener to ocupy the packetId spot on the dictionary
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
I still gota do a propper pass for correctly closing and disposing of everything aswell
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Yeah that's something I'll change later on, but as of now this is by design. I do think I may have find it tho. As @bighugemassive3 suggested I set a breakpoint in the constructor and I do think it's being called more than once, leaving an outdated reference in the dictionary (With it not accepting more listerners per packetId). I'm creating the instance directly in a class member: UdpConnection connection = new UdpConnection() This code is being run by unity and from when I can see in the debugger it looks like it creates the instance when it compiles, but then it does it again when entering playMode which I was not aware was a thing.
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
I'm invoking the listeners when an even happens:
public static void HandleTcpPacket(ServerPackets packetId, Packet packet)
{
#region HandleTcpPacket
NetworkManager.Singleton.FireOnPacket(packetId, packet);


bool foundReader = packetReaders.TryGetValue(packetId, out var reader);

if (!foundReader)
{
Debug.LogError($"Reader not found for packet {packetId}");
return;
}


IPayload payload = (IPayload)reader.DynamicInvoke(packet);

NetworkManager.Singleton.FireOnTcpMessage(packetId, payload);
NetworkManager.Singleton.FireOnMessage(packetId, payload);

if (packet.messageId == 0) return;
HandleAwaitedResponse(packet.messageId, payload);
#endregion
}
public static void HandleTcpPacket(ServerPackets packetId, Packet packet)
{
#region HandleTcpPacket
NetworkManager.Singleton.FireOnPacket(packetId, packet);


bool foundReader = packetReaders.TryGetValue(packetId, out var reader);

if (!foundReader)
{
Debug.LogError($"Reader not found for packet {packetId}");
return;
}


IPayload payload = (IPayload)reader.DynamicInvoke(packet);

NetworkManager.Singleton.FireOnTcpMessage(packetId, payload);
NetworkManager.Singleton.FireOnMessage(packetId, payload);

if (packet.messageId == 0) return;
HandleAwaitedResponse(packet.messageId, payload);
#endregion
}
But that is not the issue since the listeners do get correctly called
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
trying now
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Ah, I thought u meant literal test code, since u suggested I checked my test code
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Run the code -> Code don't work xdd
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
the Udpconnection class does inherit from IDisposable, but I did check and the instance is not disposed if that's what you are refering to
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
I'm not even testing yet, I was just writing it, I don't like TDD
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
I've been battling this all day And I can see, through debugger that this when executing the listener does not have the updated members
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Nope, it does not make sense, that's why I'm here haha
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Yep
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
For the listner to exist in the same instance, so that even if values are changed after registering it, it will reside in the instance scope and so have access to them. In the example above, I'd expect foo to be changed when accesing it though the listener
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
You can see however, that in the RegisterListeners function I am not creating a new instance at any point, so I don't understand why this is happening. Am I just not using the Delegate.CreateDelegate(delegateType, instance, method); correctly? As u can see I am passing it the instance of the class, so when I call the created delegate it should exist in the same instance no?
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
No so, the TL,DR is: I have a class with a method that has the [MessageListener] attribute. I then Register (Store) the methods that have this attribute on a dictionary to be called when an event happens. When this even fires, the methods ARE called, but for some reason they "exist" in another class instance, it's like when I store the created delegate, even if I'm passing the instance it creates a new one. Example flow: - Instanciate class - Set class member foo = "bar" - Register listener - Set class member foo = "changed" - Event gets called - Listener gets triggered - In the listener, if I access the class member foo the value will be bar instead of changed.
55 replies
CC#
Created by Adise on 4/6/2025 in #help
✅ Class instance reference
Finally, when I call the stored listener:
private static void RouteMessage(ServerPackets packetId, IPayload payload)
{
#region RouteMessage
Debug.Log($"Routing message {packetId}");
lock (_messageListenerslock)
{

bool isListenerFound = messageListeners.TryGetValue(packetId, out var listener);
Debug.Log($"Listener found: {isListenerFound}");
if (!isListenerFound) return;

listener.DynamicInvoke(payload);
}
#endregion
}
private static void RouteMessage(ServerPackets packetId, IPayload payload)
{
#region RouteMessage
Debug.Log($"Routing message {packetId}");
lock (_messageListenerslock)
{

bool isListenerFound = messageListeners.TryGetValue(packetId, out var listener);
Debug.Log($"Listener found: {isListenerFound}");
if (!isListenerFound) return;

listener.DynamicInvoke(payload);
}
#endregion
}
The listener gets called and the correct payload is passed and everything, but it seems like it can't access the instance members (in this case the UdpConnection class instance members). If Instead of registering the handler in the constructor I call it later, the class members from that moment will be accessible when the listener is called. Is there a way to fix this so that the instance is stored as a reference and not this "snapshot" behavior here?
55 replies