Game Development Reference
In-Depth Information
Objective complete - mini debriefing
In this section, we learned how to use the
WWWForm
and
WWW
objects to post and load the
high score from/to the server. We also used the MD5 encryption to encrypt the key before
posting the data to protect it from unwanted users. Then, we used the
StartCoroutine()
function to wait for the response from the server.
First, we created the
ServerHiScore
script to send and receive the user data from the
server database. In the
Initialize()
function, we set the maximum number of users
that will be shown in the UI. We also set the PHP URL for communication with the server
database.
Next, we have the
WaitingForResponse()
function, which will wait for the response
from the server and check if the sending request succeeds. Then, we checked if there is any
callback function to call. If there is a callback function, we will call it after the communica-
tion has been finished. Finally, we just cleared all data using
www.Dispose()
.
Then, in the
SaveUserData()
function, we first created
WWWForm
. Then, we used
Ad-
dField("action", "PostScore");
, which will tell PHP that we want to send the
score by setting
action
to
PostScore
. The
action
parameter and
PostScore
value
are set in the PHP code, which you can see in
HiScore.php
by navigating to
Chapter8
|
PHP
in the
Project
view included with the package. Then, we added
hash
filed with the
MD5 encryption value of the combination of
user.name
,
user.score
, and
hashKey
. We also added the
score
and
name
fields and set their value to the
WWWForm
object. In the last line, we used the
StartCoroutine(WaitingForResponse(new
WWW(_phpUrl, _wwwForm), null));
function to wait for the response from the
server. The
StartCoroutine()
function basically takes
IEnumerator
, which is the
WaitingForResponse(new WWW(_phpUrl, _wwwForm), null))
function
here. This function basically creates the
WWW
object that sends the
WWWForm
object to the
specific
_phpUrl
variable. It also takes the function to callback when it is finished.
Note
For more information on
WWWForm
and
WWW
, visit the following links: