C
C#2mo ago
Cristi

Passing an array as a parameter to an event

hello, i have these arrays, which needs to get passed towards the clientside
string[] names = new string[] { "Darius Campbell", "Andreea Gonzalez", "Michael Black" };
int[] money = new int[] { 100, 500, 200 };
player.TriggerEvent("SERVER:CLIENT:showCharInterface");
player.TriggerEvent("SERVER:CLIENT:displayAccountInfo", username, names, money);
string[] names = new string[] { "Darius Campbell", "Andreea Gonzalez", "Michael Black" };
int[] money = new int[] { 100, 500, 200 };
player.TriggerEvent("SERVER:CLIENT:showCharInterface");
player.TriggerEvent("SERVER:CLIENT:displayAccountInfo", username, names, money);
problem is that when they get passed, they are received as object[] args the username remains a string type, but the arrays become newtonsoft.json.linq.jarray, and i can't find a version of this plugin which is compatible with .net core 3.1 If you can help me either find a version for the newtonsoft which works, or help me get my arrays into my clientside handler, thank you!
19 Replies
Buddy
Buddy2mo ago
Use System.Text.Json Stop using Newtonsoft Newtonsoft is purely for legacy reasons
Cristi
CristiOP2mo ago
yeah but my framework uses it idk my workaround was to put all names into one string and then split it at the compiler but idk if its the best solution
Omnissiah
Omnissiah2mo ago
you should still be able to introduce system text json and leave newtonsoft in its place how are you deserializing this
felsokning
felsokning2mo ago
I'm 99.999% sure MSFT hired James Newton-King (the guy who wrote Newtonsoft.Json), so - as @Buddy said, you should move to using System.Text.Json. So, server-side would be JsonSerializer.Serialize(obj) and client-side would be JsonSerializer.Deserialize<ObjType>(objString).
FusedQyou
FusedQyou2mo ago
FYI Newtonsoft is the correct way in Unity. Considering this appears to be game code, it could very well be Unity. STJ is not supported unless you step into hoops to get it working. Nothing a beginner should do FYI, Newtonsoft correctly deserialized your array, but since it is unaware of the type is used JArray which is an abstract of their library so that you can still work with arrays There's also JObject and JToken and the idea is that these are used in the event there is no known or suitable type for your object Now, idk what your code is. Maybe you can share that. But if you have this then there is a method in JArray that allows you to map it into an object using reflection, assuming it is the correct object. You can probably omit this altogether if you properly deserialize it to begin with, assuming you have control over that
felsokning
felsokning2mo ago
That probably means/infers the client side doesn't have the full object definition, then, yeah? So, like object <ThisObject> has property <myProperty> server-side but client-side, that doesn't exist?
FusedQyou
FusedQyou2mo ago
Lots of guesswork if client code is not shared. If a library handles this, not much can be done. If they made it themselves, they should fix their code.
felsokning
felsokning2mo ago
Leading off with Unity-->Newtonsoft.Json would've helped, too. 😅
FusedQyou
FusedQyou2mo ago
It's not even about not having the full object definition BTW, because then it would still deserialize into the object type rather than JArray. This is intentional deserialization into JArray due to not specifying a target type. To be specific, I believe it actually deserializes into JObject
Cristi
CristiOP2mo ago
question, i read online that system.text.json is integrated in .net core 3.1, but when i try to use it in my client, i get this
Compilation Error: Extended static script safety checks failed!

CS0234: The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
-> cs_packages/ClientSide/Character/CharHandler.cs:4

CS0103: The name 'JsonSerializer' does not exist in the current context
-> cs_packages/ClientSide/Character/CharHandler.cs:40

CS0103: The name 'JsonSerializer' does not exist in the current context
-> cs_packages/ClientSide/Character/CharHandler.cs:41
Compilation Error: Extended static script safety checks failed!

CS0234: The type or namespace name 'Json' does not exist in the namespace 'System.Text' (are you missing an assembly reference?)
-> cs_packages/ClientSide/Character/CharHandler.cs:4

CS0103: The name 'JsonSerializer' does not exist in the current context
-> cs_packages/ClientSide/Character/CharHandler.cs:40

CS0103: The name 'JsonSerializer' does not exist in the current context
-> cs_packages/ClientSide/Character/CharHandler.cs:41
FusedQyou
FusedQyou2mo ago
.NET Core 3.0 So yes, it should exist
Cristi
CristiOP2mo ago
well it doesnt weird
FusedQyou
FusedQyou2mo ago
I suggest you just use Newtonsoft instead of refactoring everything now. One thing at a time
Cristi
CristiOP2mo ago
i ve found my api has some methods to handle json like this RAGE.Util.Json.Deserialize<>() and to use this i had to reference a newtonsoft.json dll but i had those in my runtime folder which copes with my api
FusedQyou
FusedQyou2mo ago
Nice, looks like that one deserializes to a target type
Cristi
CristiOP2mo ago
so i'm good now thank you yes it's cool
FusedQyou
FusedQyou2mo ago
Great 👍
Cristi
CristiOP2mo ago
another question like i have this project on .net core 3.1 and i would like to use .net 8 what should i do when switching frameworks should i just use updated references for .net 8 and i should be good? because this server of mine, it's meant to be used in .net core 3.1 but i see a lot of people that put it on .net 8 but when i tried to i had really weird errors i mean with mysql.data, and mysqlconnection, neither worked even though they were versions compatible with .net 8
FusedQyou
FusedQyou2mo ago
.NET 8 is the latest LTS Definitely try to update because your version is very old .NET barely has breaking changes so it should not be too hard Just remember to also update all packages to the latest, or to a version that supports it Third party libraries might not be as easy though, but it depends on the library
Want results from more Discord servers?
Add your server