Hello,
I have seen this question many times in old posts but never clearly answered (or answered for cpp). How do you pass data from a client to a server in RPC without touching the cpp part?
I have a bunch of data that I need to send from the client to the server to describe the user action. At first, I put my data into an Object. When the client is on the server, everything works fine but the client which is on remote got its data turned to null.
I realised that it was probably sending the reference instead of the value, so I tried to pass it as a structure. Same result.
I saw that the Actor type (instead of Object) could solve the issue, so I tried that as well with no success.
Some post mentioned to put the variables in the Object as replicated. Unfortunately for me, those variables are maps in maps (wrapped into structure as nested maps doesn’t seem possible into blueprint), so that is not really an option as map/set can’t seems to be replicated.
And using the save system, clearly seems like a workaround a want to avoid.
My next option is to pass my data as a JSON formatted string, which is what I would expect from the engine to do by default. A bit like Jackson would do between a Java backend and its frontend.
So my question is, is there a better way to do it? Like forcing a class/Object to be passed by value.
Regards,
Should be as simple as creating a custom event (run on server) adding the struct as input and calling the event.
Only time that would not work is when you are calling an event on an actor the client does not own. At which point the client calls the event on the characters authoritative proxy and then (now on server) call another event there.
If this is all directly character related… movement/action. Then you should be coding it to be deterministic. You cannot trust the client. The server (Authoritative Proxy) needs to resolve inputs itself.
Hello RevOverDrive,
Sorry for the late reply, I didn’t receive any notification.
I was expecting it to be that simple as well, so I investigated a bit more. Turns out the problem was not with the structure itself but with the fact that it was containing a map. If I put an array inside the structure everything goes well, but as soon as it is a map, the map is being cleared on the receiving.
UE seems to have some difficulties replicating maps. On version 5.0.3 at least, there is still a tooltip explaining that you can’t replicate a map/set property. So, my guess here is that RPC are somehow using the same mechanism as replication and thus creating this weird situation (may be that’s the basic way of working but in my mind, RPC are just remote API to help server/client to communicate).
On a side note, when you say “Then you should be coding it to be deterministic. You cannot trust the client. The server (Authoritative Proxy) needs to resolve inputs itself.”, you still do a pre check on the client right ? I totally agree that client cannot be trusted, and that server should validate the action, but the client should also filter and test the input to avoid bothering the server, right? Otherwise, it would be calling the server at each user input and that would be really unnecessary polluting (in my case any way, as I am doing a turn-based game).
Thank you for the help!
Yes, you should be validating inputs/actions… Def before sending an RPC.