donderdag 7 april 2016

Interacting with games through a website

For a small art project I was asked to create a website that is able to interact with a game. The assignment was to develop a web form that users can fill to submit a message to the game. The information that has been sent to the game becomes part of the game.

The game, is a Tamagotchi, fed by people sending food through a website. You also can  add a message when sending the food and the Tamagotchi will react to it.

I’ve created a simple PHP based service that adds “food” to a queue in a database and another service to request the content of this queue. This service returns a Json object on request that is de-serialized in Unity by using LitJson. I was not able to use the build in Json serializer of Unity, it didn’t accept an array of objects for some reason.


GetFood.cs


using UnityEngine;
using System.Collections;
using LitJson;

public class LucyGetsFood : MonoBehaviour {
    private string _URL = "url to a website/service.php";

 public void GetFood()
 {
  StartCoroutine(GetFood());
 }

 IEnumerator GetFood() {
  WWW www = new WWW(_URL);
  yield return www;
        string jsonString = System.Text.ASCIIEncoding.ASCII.GetString(www.bytes).TrimEnd('\0');
        JsonData inputList = JsonMapper.ToObject(jsonString);

        if(inputList["inputList"].Count > 0)
        {
            print(inputList["inputList"][0]["name"].ToString());
            for(int i = 0; inputList["inputList"].Count > i; i++)
            {
                string name = inputList["inputList"][i]["name"].ToString();
                string color = inputList["inputList"][i]["color"].ToString();
                string message = inputList["inputList"][i]["message"].ToString();

                SpwanFood(name, color, message);
            }
        }
 }
}

I have worked with Json before so I thought that is would be done in no time. But it turned out to be different. I did not expect the difficulties I encountered with the build in Unity Json serializer. It was interesting to work with Json for Unity and I think it is valuable knowledge for the future. I would like to learn more about web-database based user information for things like users save games and high scores.
This project was a good experience to be better prepared to properly perform other tasks related to games interacting with web databases.

Geen opmerkingen :

Een reactie posten