Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using this API to send a message from the client to the host game. I know the host is getting the messages bc when I print them to the log it will increase or decrease in number depending on how many I send, but it wont give me the data inside. Just gives me the generic name of the list? containing the data.

Here is where MPMessage is defined: Stardew-Valley-Mods/MPMessage.cs at master · Platonymous/Stardew-Valley-Mods · GitHub[^]



//receive Messages
List<MPMessage> clientMessage = PyNet.getNewMessages("MyMod.MyAddress").ToList();

foreach (object message in clientMessage)
    this.Monitor.Log(message.ToString());


What I have tried:

Also tried using "address" instead of "message". It wouldn't even accept it.
Posted
Updated 3-Sep-18 9:21am
Comments
George Swan 4-Sep-18 2:18am    
Use 'var' instead of object in your foreach statement then pass the property of the message class that contains the message text to the Log method

1 solution

The MPMessage class you show does not explicitly implement a ToString overload, which means it "devolves" to the first class in the inheritance hierarchy that does: object
And the default object implementation of ToString returns the name of the class, not any useful information about an instance's content. If you want to log a single string with the content you will have to manually assemble it from the MPMessage properties yourself.
 
Share this answer
 
Comments
F-ES Sitecore 4-Sep-18 4:25am    
Or implement a custom ToString function. I think custom ToString is ok if this is just for logging\debugging, but if it's for anything meaningful or functionally specific then yes you should definitely be constructing the string from the object properties.
OriginalGriff 4-Sep-18 4:35am    
That's what I normally do, but from the question I think the class is part of an existing API so he probably can't modify it - and you can't add an override with an extension method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900