Game Development Reference
In-Depth Information
Objective Complete - Mini Debriefing
We learned how to use the
WWWForm
and
WWW
object to post and load the high score from
the server. We also used the MD5 encrypion to encrypt the key before posing the data to
protect it from unwanted users. Then, we used the
StartCoroutine()
funcion 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
Start()
funcion, we checked to make sure that we have the server
URL and encrypion key.
Next, in the
SendScore()
funcion, we irst created
WWWForm
. Then we used
AddField("action", "Posting");
, which will tell PHP that we want to send the score
by seing the
action
to
Posting
. (The
action
parameter and
Posting
value are set in
the PHP code, which you can see in the
HiScore.php
that I have atached with this code).
Then, we set the
hash
with the MD5 encrypion value of
hashKey
, set the
score
, and
name
to the
WWWForm
object. In the last line, we use
StartCoroutine(WaitingForRe
sponse(new WWW(PHPUrl, w_form), null))
funcion to wait for the response from
the server. The
StartCoroutine()
funcion basically takes the
IEnumerator
, which we
pass to the
WaitingForResponse(new WWW(PHPUrl, w_form), null)
funcion here.
This funcion basically creates the
WWW
object that sends our
WWWForm
object to the specific
PHPUrl
. It also takes the
Function
to callback when it is finished.
Then, we have the
WaitingForResponse()
funcion, which will wait for the response from
the server, and check if the sending request succeeds. Then we check if there is any callback
funcion to call. If there is a callback funcion, we will call it. Finally, we just clear all data by
using
www.Dispose()
.
Next, we created the
GetScore()
funcion, which is very similar to the
SendScore()
funcion except that we only send one parameter to PHP, which is
action
to tell PHP that
we want
GetScore
. Also, in the
StartCoroutine()
funcion, we put the callback funcion
in the
WaitingForResponse()
funcion, which is
LoadXMLData
. This will be called after
the loading is finished.
Then, we have the
LoadXMLData()
funcion, which will call the
XMLParser.
Parse()
funcion to parse the XML string data that returns from the server, then store
it in the
XMLParser
class. We also created
GetUserLength()
,
GetNameData()
,
GetScoreData()
, and
IsLoaded()
to get the user data from index and check if the data
has been loaded.
Then, we go back to the
HiScore
script to add the funcion that will save and load the user
data to the server database.
Finally, we applied the
ServerHiScore
script to the
HiScore
game object in
Hierarchy
view
to get the result we want.