Using databases games
You will need five Text objects, two Input Field objects, and two Button objects. Doing this requires editing properties in the Rect Transform component, located in the Inspector window while an object is selected. Changing the values is as simple as selecting the designated field and changing the value within it.
Alternatively, you can use the Scene window to click and drag objects where you want them. They have also been assigned names to help you know which object is which. If you wish to match the names, then renaming the object is done by right clicking the object and choosing Rename. Additionally, what type of object each item is will be noted in this table. Now all the objects need some proper placeholder text. This can be adjusted by selecting an object in the Hierarchy window, then navigating to the Text component in the Inspector window.
For NameResults and ScoreResults , those can be left with empty text for now. This brings up all child objects found under the parent. Next, find the text objects being used to label the input fields, and change their text to reflect which field is being edited. Finally, the text for the buttons should be changed to better indicate what the buttons do. Starting with the SendScoreButton object, click the arrow next to it to show the child Text object.
While not required, if you wish to change the alignment and size of any other text, all you need to do is find the Text component in each object and adjust the size, alignment, and other properties to your liking. You will need just a single C scripts, called HighScoreControl.
This script will be called HighScoreControl. Once you have created your script, open it by double clicking the script in the Assets window. The server script will be executed and send data back into Unity to later display in the UI created earlier. Posting a new score follows a similar pattern, but requires you use the POST method to send data to addscore.
In real-world applications, you would likely need to do more than just hash your values to secure them. But, for the purpose of this example, this process will be kept simple.
Once the values are sent through the HTTP request, addscore will handle the process of adding the score to the database. You may recognize the secretyKey value from the addscore.
Make sure that this value matches the version seen in addscore exactly. Additionally, the two URL strings will be used to point Unity to where the server scripts are so data can be sent and retrieved from the database using those PHP scripts. Finally, four Text objects are set up in code. These will be used to change the text seen in the UI. Later on, Unity will be told which objects in the editor are to be changed using these variables.
Next come two methods, one for getting scores, the other for sending. These will be the methods tied to your buttons in the UI. Both methods start coroutines that will soon be defined, while quickly resetting some text to their defaults. Speaking of coroutines, now would be a good time to go ahead and create the first one. The GetScores coroutine, as the name implies, will be responsible for communicating with the MySQL database in order to retrieve the top five scores along with player names currently stored in the database.
As mentioned before, an HTTP request will be sent to communicate with the display. This PHP script will organize the data before Unity wetakes that data and separates the returned information to neatly fit it into the texts of nameResultText and scoreResultText. Those curious underscores found in the echo statements at the end of display will be used to more easily separate this data. Next comes PostScores , which will handle the process of putting new data into the database.
It takes two parameters which correspond to the inputs in the UI input fields. The PHP script handles the rest from there, comparing the hash it received with the one it creates using similar data before inserting the data.
The HighScoreControl script is now complete. Be sure to save your work, then return to the Unity editor for a few final tasks. To do this, navigate to the Button component in the Inspector window and find the OnClick event list. However there are many downsides too - it is hard to patch individual parts of the data when they're stored in a single database file, it is not ideal for many types of complex data that games need and which you said you'd store outside - but will have references to and from things inside , it is not very flexible when you need to change the schema, it is not necessarily backwards compatible after you change the schema, etc.
For these reasons, most game developers will just use their own format. Professional developers who are performance conscious sometimes go one step further and save the in-memory data structure directly to disk so that it can be loaded in with a minimum of processing. A database isn't fast, using a database is a whole lot slower than traditional memory access. The reason is simple, a database is dynamic, there is a bunch of overhead attached to it, the queries need to be parsed, hashes need to be computed and other stuff.
There are is only one advantage from using a database and that's persistence. You can run one or multiple applications with one database over a long period of time without having to fear data lass. But game clients have absolutely no need for that. Now what would you think would a database do if you asked it to get every object less than px away? It would do exactly the same! Just with a bunch of overhead which would make this one extremely simple operation cost about times the processor time depending on how many entities you have.
Databases ain't no magic. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Learn more. Should I use an SQL database to store data in a desktop game? Asked 9 years, 3 months ago. Active 4 years, 6 months ago. Viewed 18k times. Improve this question. Q: What have I failed to ask that you think we need to consider with regards to game development? These are very engaging and very data-driven. Millions of people refresh their screen every time a player scores.
End users want to see where they stand versus their friends and versus the entire player universe. Ultimately, this will generate more data and require more bandwidth than other games. Thanks for visiting DZone today,. Edit Profile.
Sign Out View Profile. Over 2 million developers have joined DZone. Data-driven insights drive user experience, which drives revenue. In this interview, learn how important databases are in game development. Like 2.
Join the DZone community and get the full member experience. Join For Free. Q: What are the keys to developing successful games? Q: What are the most significant changes to game development?
0コメント