- Home /
Server generated battle scene instead of client - Concept assistance please?
Hi Guys,
This is my first post, so i'm quite new and have spent the last few months playing around and getting to grips with all of unity's structure.
I'm looking a making a Rts game that doesn't require the client / user to be connected all the time which is fine as no client (p2p) networking or master server is required, all data is held within database and calculations are made dependent on information contained within database.
I understand how to go about creating a battle engine and scripting battles to take place with all units being npcs etc. My issue is created by simulating the battle engine or scene as neither player involved is required to be online at the point of simulation (due to traveling time which occurs in real time) and therefore would require the scene to be simulated preferably at my host server which can then be played back to the users at next log on, or streamed like a video as the simulation takes place.
Is this concept even possible, if so how would i go about it, the only logical solution would be to have a master server create a game room or scene in this case to begin at a set time and simulate the scene which is recorded then played back, with results from the simulation posting results back into the database.
Please could anyone point me in the right direction?
Regards Gilduck
Answer by hatshapedhat · Apr 20, 2013 at 02:02 PM
Sure it's possible. The question as to how is really broad and depends on lots of other things - what other kinds of interactions do you want to support, what kind of network code is required, that sort of thing.
Your server code doesn't have to be done in Unity directly - there are networking kits and tools (I use Photon, for example, pretty happy with that one, but there are others that may suit you better) and your server code is handling clients coming and going, processing messages from them, and managing the database. When some client connects and sends a message like "I want this legion to move to Corinth and attack that army", the simulation loop on the server would take care of that. The client might come back and send some other message to the server like "tell me what happened so far" and the server response might be "they are still walking" or it might be "the battle is over, you won" or it might be "no army was found at destination, your troops are idle". If it's the middle of the battle (assuming that also happens in real time) then the client might get back information pertaining to the playback you were mentioning. If you want to render that as a movie, that sounds to me like logic / work that would be done at a client. After all the server wouldn't want to spend a lot of time rendering some movie that no one might ever see - it should worry about the data and the simulation, not the presentation.
I don't know if that helps, but as I said the question was pretty broad.
I appreciate you response, i understand where you are co$$anonymous$$g from i've already created the game and it is already running but as a website, all the scripting is done using PHP to display the information and run sql queries against the database, there is some javascript inbuilt to perform some of the more complex calculations but the result is a couple of hundred webpages with next to no animated sprites etc.
That is Unity comes in, i've recreated the majority of the game in unity view several scenes (this again uses php to post and get data from the database) and all works fine the only part i'n struggling with is the battle engine / scene, yes i can get this to render on client side which is easy enough the problem is making sure that if two clients were rendering the same battle that the outcome would be the same as some of the npc's scripts have elements of chance using the random function to deter$$anonymous$$e there state (i.e. flee, dodge etc) and this function is also used to deter$$anonymous$$e the chance of strike and critical chance etc, therefore getting two rendered battles to have the exact same outcome is next to near impossible. This was therefore the reason for approaching to battle scene as run via the server or hosted end that can be streamed or watched at a later time by any client.
In regards to handling movement state's (server interaction code) this is already being handled by with unity requesting and sending messages through an PHP api i have setup, so as i can pass variables and request information from the the database.
Thank you for your response so far, i appreciate its difficult to know exactly what im trying to find out with the limited information provided.
Regards Gilduck
It almost sounds like the outcome of the battle is deter$$anonymous$$ed by client code which strikes me as a really bad idea, design wise. But if you just need to have consistent results, why not use the same random seed (store the seed on the server, maybe) and then all the random variations in a given encounter will play out the same, assu$$anonymous$$g they are evaluated in the same order, and the random number generator implementation is the same on all platforms.
Thanks for this, was not aware this could happen, I agree having the battle outcome based upon the client code is a bad idea, this is why i was trying to define the server to act out the battle since all units are NPC based and none player controlled, so no input is the required and all the data required for the battle to take place is present in the database prior to any battle taking place, therefore as long as i can ensure the battle taking place starts in the exact same way i.e. unit positioning, direction, speed, amount of units etc then every event or unit interaction should play out with the same outcome on each client based upon the order of random class calls.
Do either of you know if variations take place between different platforms i.e. different browsers in web-player or different operating systems for Client applications, this would be most helpful.
I may remove the random class out the scene / npc scripts and use the random function to submit the variables to the database prior to the battle taking place which would makes them static.
Think this would help.
Answer by lanwin · Apr 21, 2013 at 05:18 PM
The Random class provides a seed value. It's not documented at all, but technically, as long as the seed value is the same, all the calls to the Random.value will be the same.
Let's consider this script :
UnityEngine.Random.seed = 2;
float randomValue = Mathf.Floor(UnityEngine.Random.value * 10)
The first three calls must give you these results : 4, 0, 8 - that's the value that this code and this particular seed has given me.
So, if your two players have the same seed value, any Random result will be the same, thus assuring you they'll see the same thing, no matter how much calls to the Random class you do.
All you have to do is generate in PHP an int value, store it with your data, and transmit it to your players along the other data, and making it the seed value of the Random static class.
Hope this will help you ;)
(I didn't test with the Random.Range method though, but I don't see why it wouldn't act like the rest).
Thanks guys had thought of this or been made aware of its existence. i appreciate your input.
This would get around the strea$$anonymous$$g & playback aspect perfectly.
Is there a way of calculating the output of a rendered scene with out actually rendering the scene itself, this way i wont have wait for the simulated battle to take place before publishing a result to the server. i can imagine this could be quite a large task itself?
It can be done easily if your way of resolving the output is "scene-independant", as it doesn't rely on things like Transform (including position, rotation and scale), Physics (particularly collision detection or Raycast), etc.
If your logic only includes a few Random calls and some conditions, it can be done in PHP (as it seems it's what you mean by server).
If, ins$$anonymous$$d, it uses something like Transform or these kind of classes, you're gonna have to recreate a way to simulate these classes and the interactions between them, and it can quickly become a very big (and boring) task.
If you really want to have the output der$$anonymous$$ed by the server ins$$anonymous$$d of the client, as hatshapedhat wisely mentionned in his comment, you could have something like a "Unity Server", a version of your game you would have on your own computer. This specific version would have to take the data stored in your database, simulate the battle to calculate the output, log all the events and store all these things back into the database. You'd then serve this data to the real clients. For the players it'd seem like the server solved the outcome, and you wouldn't have to wait that someone actually play the battle to know the outcome (and in the same time preventing cheating, as only your own version would be allowed to simulate battles). As you must think, this isn't a very easy task either, but it has some advantages (you can use the code you've already done, you'll be able in the future to modify the way to calculate the income e.g. nerf some units' characteristics without having to update the client game, etc) and some inconvenients (mostly having an server instance of your game always available on your computer or on some dedicated server, and having a queue system to handle multiple battles).
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Renting a server for game hosting without a plugin 1 Answer
WebGL uNET Host Server on WAN 0 Answers
How to PollHostList already running as server? 0 Answers
Change collider size in Networking 0 Answers