- Home /
UNET Discrete Server/Client. ClientRPC? Command?
Hi all, I'm having a lot of trouble with UNET, trying to understand how to implement a discrete client/server setup. With the old system I could create my client, and my server, connect, and use RPCs to do everything I wanted. Now... I have no idea, and it's kinda frustrating because I want to do some really basic things.
I've read through the documentation, watched a few hours worth of tutorials, and downloaded sample projects, as well as trawled through these questions to see if anyone has my problem, and no luck so far.
Specifically; From the documentation, server works fine public void SetupServer() { NetworkServer.Listen(4445); isAtStartup = false; }
Client connects void ConnectToServer() { myClient = new NetworkClient(); myClient.RegisterHandler(MsgType.Connect, OnConnected); myClient.Connect("127.0.0.1", 4445); }
How do I execute the equivalent of an RPC? Say, from the client, I want to send a simple string to the server? I'll then want to send that string to clients.
I've read about [Command] and [ClientRPC] and [ClientCallback], but literally every example I've found uses a game that can act as host, client, or both. In the project I'm working on there is no need to run the game as a host, because it's not that type of multiplayer.
Consider it asynchronous. A client will play, and at some point some updates will be sent to the server (a score, for example). And all that server will do is store that information, and at some point propagate it out to clients.
Can anyone give direction on the best way to achieve this kind of system? Can I use [Command] [ClientRPC]? How do I implement them when they aren't sharing the same application codebase and thus don't have the same methods/functions? (do they need to? RPCs had to be on both client and server, but you could leave the one which didn't need to execute anything blank - is it like this?)
Any help appreciated Thanks
Answer by hailtonothing · Jan 10, 2016 at 05:50 AM
Hi guys, it took some time but I solved the problem and basically the answer I've come up with is to use messages. I made a more detailed post about the solution here if anyone cares about the detail: http://hourlyfragments.tumblr.com/post/135813394725/message-received
Answer by Fattie · Dec 01, 2015 at 04:50 AM
"and thus don't have the same methods/functions"
it's totally meaningless otherwise -- of course, you have to have matching functions!!!!
you may as well ask "how can two totally unrelated networks network?" !
note that you can, if you want, arrange to simply pass a string, which "means" a command in your system. you can then (if you want) ignore these "word commands" which you don't know about yet, flag a version error, or whatever.
In the spirit of your very general questions about network topology for games,
http://answers.unity3d.com/answers/346977/view.html
and the long answer here http://answers.unity3d.com/questions/326626/games-with-multiple-maps.html
It's been a while since I've done raw network program$$anonymous$$g, but "matching functions" is not something I've seen outside Unity's RPCs in version 4x, so I don't feel like it's that obvious.
The links are too old to be useful for the question I'm asking, I'm afraid. I had this working previously, so I didn't have a problem with how it used to function, it's the new stuff I can't figure out.
Okay here's a more specific question, because I'm not sure the above really answers what I'm asking.
On the client, if I have: [Command] void CmdSendText(string bleh) { }
then call CmdSendText("some nonsense");
and then on the server I would have [Command] void CmdSendText(string bleh) { //do something with this text, log it or whatever }
Is this how the command system works?
Thanks
Edit** I also don't know why the code formatting isn't working in my posts, sorry about that.
Answer by jeango · Dec 15, 2015 at 09:09 AM
Hey there, ClientRPC, Command and ClientCallback are all part of the High Level API. And when things are High Level, it also means it doesn't necessarily cover your specific needs.
If there's something you want to achieve that can't be achieved with the High Level API, I suggest you take a look at the Low Level API (aka Transport Level API)
http://docs.unity3d.com/Manual/UNetUsingTransport.html
I think this is perhaps where you would want to look for answers.
Edit: Just stumbled across this example of a Master Server implementation (from the looks of it, that's exactly what you're trying to do http://forum.unity3d.com/threads/master-server-sample-project.331979/ )
this is a great answer. but it's worth noting that you can do "absolutely anything" with the high level system. (after all, using the correct version of the RPC call you can send any type of "signal" or information to any other connected object, so, you can "do anything", Turing would approve.) if one is beginning with networking and has not yet figured out network topology, there's perhaps not much point going to Unity's offered lower-level API. by all means, in due course of time one should master the low-level API but the OP should perhaps first master the high level API. After all it's simple.
A critical tip is that you use the "one to one" version of the call, in the real world. http://answers.unity3d.com/answers/326658/view.html
I'm not sure what you mean by "the correct version of the RPC call".
your link is referring to the deprecated RPC API. I'm not familiar with it, but from all I have read about it, it has nothing to do with the UNET API.
as explained at great length repetitively on those linked discussions, you only (of course) use ordinary 1->1 topology, not the weird "everyone->everyone" topology which is offered as a sort of (impressive, but totally pointless) novelty by Unity.
yes, this discussion here got extended here more broadly than your original OP. Sounds like you've got it all figured out, so, enjoy!!! bye
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Networking between projects: how to send ViewIDs? 1 Answer
Network how to send data/activate function 1 Answer
ClientRpc not called on Client 1 Answer