question

joehot200 avatar image
joehot200 asked

Can I create a custom client and server in a single project?

Hi,

When I created my game solely with Unity Networking (without PlayFab), I was able to create a world which was able to be both a client and a server. If I ran the world with the -nographics flag, I would detect that when the world ran and, instead of spawning a player, it would become an authoritative server. However, if I ran the world normally, it would connect to the server as an ordinary player.

Is it possible for me to do the same with a PlayFab custom server?

My game (which currently uses Photon, because I thought that could have custom servers, only now I've found out I'll have to convert back to Unity networking... grrrrrrrr) is already coded in a manner in which this is possible, all I would need to do is set an "is server" boolean to true in my code.

I've also never set up a PlayFab custom game server before. I presume I would have to make separate code run if it's a custom server, or if it's just a regular client, for either connecting to the server or registering the server with PlayFab.

I'm completely and utterly lost, and any help would be really appreciated. I've tried sifting through the example custom server code, but it isn't clear to me how I can use that code to achieve the result I am looking for.

Thank you,

Joe

Custom Game Servers
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

Simply put, no, you don't want to do that. The issue is that if you were to ship to end-users an executable that is capable of calling the Server API endpoints, that would mean you're sending them an exe that also contains your title's Secret Key - which a hacker could then use to do anything at all to your game (delete users, change your configuration, set all your game catalog items to zero cost, etc.). The end user should never have access to your Secret Key.

6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

joehot200 avatar image joehot200 commented ·

Thank you for the answer. However, I'm afraid that won't quite fly for me. Considering that my world relies a lot on physics, if I changed one tree on the client, I would have to add the same tree on the server. I can't genuinely believe developers would be expected to maintain two versions of their game and keep updating them in parallel, simply to have a custom server.

I know for me personally, such a strange decision would slow my game development to a crawl.

If I can't have the same EXECUTABLE be a custom server, surely I can still have the same project as a custom server? Would the solution not simply be to export the project twice: One with the secret key, to upload to the server, and one without the secret key, to send to players.

However, I'm not sure how to do that, and I'm not sure how I can make my existing game work as a PlayFab server. Creating a separate project is effectively out of the question, and so I need to do what is necessary with one project.

0 Likes 0 ·
brendan avatar image brendan joehot200 commented ·

To be clear, what I'm specifically saying is that you should not ship a game that contains the logic to do both. What you can do is to separate the parts that require Server API calls and the Secret Key for your game, as long as your diligent about having consistent use of #if statements, in order to partition that into a build that you'll only ever use for the hosted game servers. So yes, you can certainly use the same project - have a look at the way we do this in the server sample's shared settings file: https://github.com/PlayFab/PlayFabGameServer/blob/2ee3b6e0ee796ff50ee5f3df7eaa9c5270c4c24a/PlayFabGameServerClientExample/Assets/PlayFabSDK/Shared/Models/PlayFabSharedSettings.cs.

In fact, the server sample is designed specifically for what you're describing - having one project that can be used to build both the server and the client.

0 Likes 0 ·
joehot200 avatar image joehot200 brendan commented ·

I'm not really sure how that settings file helps me, if I'm honest. This is really driving me up the wall, and you can see why, because I'm trying to incorporate a custom server into my already created game. What do you mean by the sample being designed for what I'm describing? The sample clearly seems to have two projects; one for the client, and one for the server. I simply need something like two scripts, one that connects to a server, and one that opens a server, and this example is just straining my brain trying to work out what code I actually care about. My game currently makes no server API calls whatsoever anyway. Is there anything out there that actually shows me how to just create and connect to a server? I mean, I could do it with Unity Networking; why can't I do it with PlayFab?

0 Likes 0 ·
Show more comments
joehot200 avatar image joehot200 commented ·

@Brendan What I mean is that, I presume that the server and client both connect to each other via Unity's networking API, and PlayFab's matchmaking is what directs each player to a server.

My problem is that integrated into the example are tons of things related to a chat server, and I'm becoming very confused about what code I actually need to copy to establish a connection with Unity's Networking API. I have a lot of experience with Unity's networking, so as long as I can establish a connection, then I'm good, however I can't work out how to do so.

I've been programming for 7 years and *should* be an expert now, so it's irritating me to the extreme that I can't work out how to do this. If I could just establish a connection and a NetworkManager I would be fine.

0 Likes 0 ·
brendan avatar image brendan joehot200 commented ·

Yes, you can use any networking library that can make the UDP (left this off previously) or TCP connection, so yes, you can use Unity's networking code to connect. The only thing you need to do is use the specific info the client gets back fro the Matchmake call (IP and Port) to connect - nothing fancy.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.