musademir
musademir
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Alright, thank you so much for taking the time to help
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
I will implement that too 👍
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Yes, it looks like a better solution 👍
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Alright thank you so much. It was an informative talk for me. Let me check them all and try my best 🙂
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
What do you mean adding referans to MethodInfo?
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Thank you, I also think that but since it is server-client message using reflection in every tiny message in a mobile game will probably cause performance issue. I will try some caching for that 👍
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
I will try the first one
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
I actually am loading them automatically from assembly then register them to the dic
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Yes it depends on the message, But overall I know all of them
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
public class FindMatchMessageListener : IDarkRiftMessageListener<FindMatchMessage>
{
public void OnMessageReceived(FindMatchMessage message, IClient client)
{
//TODO: Do Something
}
}
public class FindMatchMessageListener : IDarkRiftMessageListener<FindMatchMessage>
{
public void OnMessageReceived(FindMatchMessage message, IClient client)
{
//TODO: Do Something
}
}
and here is the example
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
It is a generic listener class that listens given message from given client
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Here is my real code
public interface IDarkRiftMessageListener
{

}

public interface IDarkRiftMessageListener<T> : IDarkRiftMessageListener
where T : IDarkRiftMessage
{
void OnMessageReceived(T message, IClient client);
}
public interface IDarkRiftMessageListener
{

}

public interface IDarkRiftMessageListener<T> : IDarkRiftMessageListener
where T : IDarkRiftMessage
{
void OnMessageReceived(T message, IClient client);
}
private Dictionary<ushort, IDarkRiftMessageListener> _messageListeners = new();
private IDarkRiftMessageListener GetListener(ushort tag)
{
return _messageListeners.ContainsKey(tag)
? _messageListeners[tag]
: null;
}

private void HandleMessage(ushort tag, Message message, IClient darkRiftClient)
{
var listener = GetListener(tag);
if (listener == null)
{
return;
}

using DarkRiftReader reader = message.GetReader();

var messageType = _messageTypes[listener];
var darkRiftMessage = (IDarkRiftMessage)Activator.CreateInstance(messageType,true);

reader.ReadSerializableInto(ref darkRiftMessage);

listener.OnMessageReceived(darkRiftMessage, darkRiftClient);//I have to call that
}
private Dictionary<ushort, IDarkRiftMessageListener> _messageListeners = new();
private IDarkRiftMessageListener GetListener(ushort tag)
{
return _messageListeners.ContainsKey(tag)
? _messageListeners[tag]
: null;
}

private void HandleMessage(ushort tag, Message message, IClient darkRiftClient)
{
var listener = GetListener(tag);
if (listener == null)
{
return;
}

using DarkRiftReader reader = message.GetReader();

var messageType = _messageTypes[listener];
var darkRiftMessage = (IDarkRiftMessage)Activator.CreateInstance(messageType,true);

reader.ReadSerializableInto(ref darkRiftMessage);

listener.OnMessageReceived(darkRiftMessage, darkRiftClient);//I have to call that
}
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
I tried that, but things gets messy when I try to call functions
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Or do you have any suggestion for me to implement that kind of listener thing in a better way?
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
So what do you think should I do since I am not able to use in keyword I will not be able to get data in parameter. When I use out I am only available to use it as output.
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Alright, thank you sir. I would love to check that deeply, I did not know that deeply.
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
As mentioned in the documentation It does work If I use out keyword
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Yeah I guess the problem is about unboxing it right?
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
I mean, MyData can be convertable to IMyDataInterface without any error
67 replies
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Sorry i mean I was listening 🙂 (lack of my english)
67 replies