So Many Stars

While many game services and platforms have built-in ratings system, there may come a time when you, like me currently, need a way to allow a user to rate your game using a standard 5 star system. I took a bit of time today to put together a scene to handle this.

First I created filled and outline star graphics:

 

Next I created a scene and laid out a couple of buttons and the 5 star outlines:

The stars are Image UI components. The OK button is not interactable until a rating is selected.

Next I added some event triggers to the stars:

The only unusual thing about this is I added a parameter to the standard event handler:

public void StarEnter(GameObject obj)

public void StarExit(GameObject obj)

public void StarClick(GameObject obj)

Since I’m using the same 3 events for all 5 stars I needed a way to tell which star is being handled. I didn’t want to have to go through some convoluted chunk of code to figure out which star the mouse was over. This would have involved doing a raycast from the mouse location and seeing it the ray hit something, then figuring out which star it hit. Once I set up the empty events in my script the Unity UI automatically recognized the need for a GameObject parameter.

Here’s the entire Rating class:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;

 

public class Rating : MonoBehaviour {

 

    public Image Star1;
    public Image Star2;
    public Image Star3;
    public Image Star4;
    public Image Star5;

 

    public Sprite Star;
    public Sprite StarOutline;

 

    private int _ratingSelected = 0;

 

    public Button OKButton;

 

    void Start()
    {
        //check for existing rating and disable if exists
        if (PlayerPrefs.HasKey("Rating"))
        {
            StarEnter(GameObject.Find(PlayerPrefs.GetInt("Rating").ToString() + "Star"));

 

            _ratingSelected = PlayerPrefs.GetInt("Rating");
        }
    }

 

    public void StarEnter(GameObject obj)
    {
        if (_ratingSelected == 0)
        {
            switch (obj.name)
            {
                case "1Star":
                {
                    Star1.sprite = Star;

 

                    break;
                }
                case "2Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;

 

                    break;
                }
                case "3Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;

 

                    break;
                }
                case "4Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;
                    Star4.sprite = Star;

 

                    break;
                }
                case "5Star":
                {
                    Star1.sprite = Star;
                    Star2.sprite = Star;
                    Star3.sprite = Star;
                    Star4.sprite = Star;
                    Star5.sprite = Star;

 

                    break;
                }
            }
        }

 

    }

 

    public void StarExit(GameObject obj)
    {
        if (_ratingSelected == 0)
        {
            switch (obj.name)
            {
                case "1Star":
                {
                    Star1.sprite = StarOutline;

 

                    break;
                }
                case "2Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;

 

                    break;
                }
                case "3Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;

 

                    break;
                }
                case "4Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;
                    Star4.sprite = StarOutline;

 

                    break;
                }
                case "5Star":
                {
                    Star1.sprite = StarOutline;
                    Star2.sprite = StarOutline;
                    Star3.sprite = StarOutline;
                    Star4.sprite = StarOutline;
                    Star5.sprite = StarOutline;

 

                    break;
                }
            }
        }
    }

 


    public void StarClick(GameObject obj)
    {
        _ratingSelected = Convert.ToInt32(obj.name.Substring(0, 1));
        OKButton.interactable = true;
    }

 

    public void OKButton_Click()
    {
        //save rating
        PlayerPrefs.SetInt("Rating", _ratingSelected);
        Application.LoadLevel("Menu");
    }

 

    public void CancelButton_Click()
    {
        Application.LoadLevel("Menu");
    }
}
}

Since we don’t want the player to be able to change a rating once it’s made, the first thing we do is check to see if the player has already rated the game. I’m storing this in the PlayerPrefs for convenience sake. Obviously it should be in a place a little more secure. If the player has already rated the game, we call the StarEnter handler, passing the control for that number of stars, which sets the related number of stars. We then set the _ratingSelected member with the value from the PlayerPrefs key. Since the event handlers don’t do anything if the _ratingSelected member has a value other than 0, this effectively disables them.

The StarExit handler resets the images to the star outline. We could have simply not passed a parameter to it and just reset all of the stars, but I like the consistency of having a parameter for all 3 handlers.

If the player clicks a star, the _ratingSelected member is set and the OK button enabled, allowing the player to save the rating.

Pretty straightforward. If you use this as is, you’ll have to have a scene named “Menu” and Image controls with the names I used or you’ll get an error.

Feel free to comment – is this useful? Would you have done something different? Am I wasting my time here? :)

Rolling the Dice With Unity

No, I’m not talking about taking a chance using Unity to do game development. That decision was made once MS stopped caring about XNA :( (obligatory links here and here on it’s impending demise). I’m talking about having rolling dice in your game. In this case it’s just one die and it’s 2D not 3D. Still, it might prove useful to some people so here we go.

So I’m working on a 2D board game that uses a die to determine the number of squares a player moves. I wanted to show the player something so they’d know the game is rolling a die rather than just generating a random number and showing it. Turns out it’s fairly easy to do using Unity.

First off, I needed some pictures of the 6 sides of a die:

     

I didn’t want to take up any space on the board and also wanted to let the player know in a rather obvious way that it was time to roll the die, so I decided to slide in a panel with the image of the die on it and allow him to tap or click it to start the rolling process. Here’s what I ended up with:

Notice the panel is far off to the left. It’ll slide in from and out to the left. This is done with a simple animation:

At the end of the animation the x position of the panel is set to 0 which, since it’s anchored in the center of the Canvas moves it to the middle of the screen. I do the exact opposite to slide the panel back off the screen in the animation to close the panel, setting the x position to –1000 at the end of the animation. If you’ve never used animations before, there are a ton of resources to help you learn them. As always, your best first step is the Unity site. Here’s a link to the manual section for animations. Here’s a nice tutorial that covers slide UI controls in and out as I’m doing, as well as a ton of other concepts.

Once I had the animations set up, all that was needed was a small amount of code to do the die rolling:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

 

public class DieManager : MonoBehaviour {

 

    public Sprite[] Dice;

 

    private bool _isRolling;

 

    private float _totalTime;
    private float _intervalTime;

 

    private int _curDie;

 

    Image _die;

 

    private bool _dieRolled;

 


	// Use this for initialization
	void Start ()
    {
        Init();
	}

 

    private void Init()
    {
        _totalTime = 0.0f;
        _intervalTime = 0.0f;
        _curDie = 0;
        _dieRolled = false;
        _die = GameObject.Find("DieImage").GetComponent<Image>();
        _die.sprite = Dice[_curDie];
    }

 

	void Update () 
    {
        if (_isRolling)
        {
            _intervalTime += Time.deltaTime;
            _totalTime += Time.deltaTime;

 

            if (_intervalTime >= 0.1f)
            {
                //change die
                _curDie = Globals.Rnd.Next(0, 6);

 

                //set image to selected die
                _die.sprite = Dice[_curDie];

 

                _intervalTime -= 0.1f;
            }

 

            if (_totalTime >= 2.00f)
            { 
                _isRolling = false;
                _dieRolled = true;
            }
        }

 

	}

 

    public void DieImage_Click()
    {
        if(!_dieRolled)
            _isRolling = true;
    }
}

The Dice member holds the 6 images of the die. It’s public as I set them in the Unity UI:

The script is attached to the Canvas object. The die images are then dragged into place from the folder in the game Assets where I’d placed them.

The _isRolling member is used to determine whether or not the time is tracked between frames in order to know when it’s time to change the die image (every 1/10 of a second) or when the die rolling is completed (after 2 seconds).

The _totalTime and _intervalTime members are added to each frame to know when it’s time to change the die image and when the die rolling is completed.

The _curDie member holds the value of the most recently generated number between 0 and 5 (remember kids, arrays are numbered starting at 0, see the screenshot above!) and is the index into the Dice array for displaying the image.

_die is the reference to the Image control in the Panel.

_dieRolled is used to determine if the animation is done for this turn of the game so the player can’t just keep clicking the image to roll again.

The DieImage_Click event is used in an Event Trigger on the DieImage object:

So how does the panel slide in and out since it doesn’t happen automatically? Two simple events for the buttons in the scene:

    public void PanelTestButton_Click()
    {
        GameObject panel = GameObject.Find("DiePanel");
        Animator animator = panel.GetComponent<Animator>();
        animator.Play("DiePanelOpen");
    }

 

    public void PanelOKButton_Click()
    {
        GameObject panel = GameObject.Find("DiePanel");
        Animator animator = panel.GetComponent<Animator>();
        animator.Play("DiePanelClose");
    }

 

I’ve left a bug in the logic as an exercise for the reader. You can only click the die once. It doesn’t reset when you close the panel. How would you solve this? I’ll post a comment later. :)

The Beginning of the End

So yesterday Microsoft announced it’s turning off XBLIG. It’s been a great 9 year run, but for those of us that have been keeping track, we’ve been expecting it for a while. Andy posted his thoughts at the end of the blog post, and people like Indie Gamer Chick have commented on it already, so I thought I should add my $.02 on the topic, seeing as how I was one of the guys there at the beginning.

After the announcement at GDC and seeing some of the videos MS released that supposedly showed what XNA was capable of, those of us who wanted to do game development for a living but not belong to a AAA studio got very excited. While what we ended up with in XNA Game Studio allowed us to create almost any game we wanted (except for the inevitable dev that wanted to make an MMO as their first game!) it wasn’t exactly AAA quality. A lot of the devs weren’t exactly AAA developers either and we ended up with a lot of junk released onto XBLIG that gave it a very negative reputation in the industry. There were a lot of great games there but very few gamers wanted to sift through the junk.

Microsoft announcing the first Dream Build Play was another first in the industry. Anyone had the chance to submit a game and win cash and a chance to get their game on XBLA, which meant major exposure. I think the several competitions were a relative success and made many developers’ dream of moving to full-time game development become reality.

While sales data that devs released were all over the place, many games brought in a ton of money for indie standards. Considering costs for indie games aren’t quite up to the 7 figure range that AAA titles hit, many devs seemed to be making out pretty well. I’m not quite sure why, but this didn’t seem to be enough for Microsoft. Support for XBLIG tapered off, both in updates to XNA GS and responses to issues that came up in the community. The XNA team dwindled to nothing and problems with payments and the release pipeline stacked up. For years we couldn’t get a solid answer to what was to become of the XBLIG community.  The writing seemed to be on the wall – XBLIG was dead. Many devs moved on to other technologies and platforms.

Personally, XNA has led to me becoming a twice-published author and tech book editor, Microsoft MVP, and conference speaker. I couldn’t have anticipated what has happened in the past 9 years. While I didn’t become one of the big XNA success stories, I’ve definitely benefited greatly from the existence of XNA and the XBLIG community. I’ve met a lot of great people, got to visit the Microsoft campus several times, and helped out the community in some small way I think.

Although XNA is dead, the community will move on and hopefully continue to create great games. I’ll be there, doing what I can and try to get my games out there for people to enjoy. Who knows, I may eventually become the next great indie game dev. :)

Creating a Space Shooter Weapon System

So a question came up the other day from Dave Voyles about how to design a weapon system for a space shooter. He needed to figure out how to track what weapons a player had collected and how to switch between them. I got to thinking about it and decided to see what I could come up with. Here’s the basic requirements that I figured would be good to have:

  • A ship could start out with no weapons or 1 or more weapons, with or without ammo
  • A weapon can have infinite ammo or require ammo pickups
  • The player shouldn’t be able to switch to a weapon that hasn’t been equipped on the ship or if it doesn’t have ammo
  • An ammo type could be shared between weapons (think universal energy cells for energy powered weapons)
  • Ammo pickups could have different amounts of ammo for an ammo type
  • Switching between weapons could wrap around both forward or backward. For example, using an Xbox controller, the left and right bumpers could be used to switch to the previous or next weapon – if the first weapon is selected and the left bumper is pressed, the last weapon that’s equipped would be selected.
  • An icon on the game’s HUD would show the currently selected weapon and the ammo available for that weapon would be displayed.

Starting from an empty Unity project, we’ll only need one scene and some GUI elements in that scene. I’m using buttons for all of the player input in this sample, but you would want to handle whatever method of input you’re targeting for your game, touch included. Here’s what my example looks like:

The 5 images on the right are used to display the currently selected weapon, which is the blank image between the Next and Previous buttons. Notice that the button to fire the weapon is initially set to disabled. Whether or not to enable this will be handled by the script we’ll attach to the scene. The small images above the currently selected weapon image will show the weapons that have been equipped on the ship. The buttons to pickup weapons and ammo would be replaced in a real game with sprites or some other element that the player’s ship would interact with.

The next thing we’ll need is a script to control everything and a script to define weapons and ammo. We’ll look at the latter first:

using UnityEngine.UI;

public enum AmmoType
{
    Regular,
    Shotgun,
    Sniper,
    Energy
}

public enum WeaponType
{
    Vulcan,
    SoloGun,
    Sniper,
    ShotGun,
    Seeker
}

public class Weapon
{
    public WeaponType Type;
    public Image HUDIcon;
    public AmmoType Ammo;
    public int AmmoCount;
}

 

For our basic example this is all we’ll need. More than likely your weapon class would have more members and functionality and you might even have an ammo class.

Notice that we only have 4 ammo types. The SoloGun and the Seeker I decided were energy weapons and will share whatever energy ammo is available.

The script to control the interaction with the weapons is only a bit more complex in my opinion:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
using UnityEngine.EventSystems;

public class WeaponInventory : MonoBehaviour
{
    private int[] weaponAmmoRate = new int[] { 10, 1, 1, 1, 5 };

    Dictionary<WeaponType, Weapon> weapons;
    WeaponType curWeaponIndex = WeaponType.Vulcan;

    public Image CurWeaponIcon;

    public Image VulcanImage;
    public Image SoloGunImage;
    public Image SniperImage;
    public Image ShotgunImage;
    public Image SeekerImage;


    public Image MiniVulcanIcon;
    public Image MiniSoloGunIcon;
    public Image MiniSniperIcon;
    public Image MiniShotgunIcon;
    public Image MiniSeekerIcon;


    public Button FireButton;

    public Text AmmoDisplay;

    public bool WrapWeapons = true;

    private Dictionary<AmmoType, int> Ammo; //-1 for infinite


	// Use this for initialization
	void Start () {

        weapons = new Dictionary<WeaponType, Weapon>();

        weapons.Add(WeaponType.Vulcan, new Weapon() { Type = WeaponType.Vulcan, HUDIcon = VulcanImage, Ammo= AmmoType.Regular });

        MiniVulcanIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);

        Ammo = new Dictionary<AmmoType, int>();

        Ammo.Add(AmmoType.Regular, 100);
        Ammo.Add(AmmoType.Energy, 0);
        Ammo.Add(AmmoType.Shotgun, 0);
        Ammo.Add(AmmoType.Sniper, 0);

        FireButton.interactable = true;

        GetCurWeaponImage();
	}

    public void NextWeapon()
    {
        if (!WrapWeapons && (int)curWeaponIndex == Enum.GetValues(typeof(WeaponType)).GetUpperBound(0))
            return;
        else
        {
            WeaponType originalIndex = curWeaponIndex;

            curWeaponIndex++;
            while (!weapons.ContainsKey(curWeaponIndex))
            {
                if ((int)curWeaponIndex > Enum.GetValues(typeof(WeaponType)).GetUpperBound(0))
                    curWeaponIndex = 0;
                else
                { 
                    curWeaponIndex++;
                    if (curWeaponIndex == originalIndex)
                        break;
                }
            }

            GetCurWeaponImage();
            CheckWeapon();
        }
    }

    public void PreviousWeapon()
    {
        if (!WrapWeapons && (int)curWeaponIndex == 0)
            return;
        else
        {
            WeaponType originalIndex = curWeaponIndex;

            curWeaponIndex--;
            while (!weapons.ContainsKey(curWeaponIndex))
            {
                if ((int)curWeaponIndex < 0)
                    curWeaponIndex = (WeaponType)Enum.GetValues(typeof(WeaponType)).GetUpperBound(0);
                else
                {
                    curWeaponIndex--;
                    if (curWeaponIndex == originalIndex)
                        break;
                }
            }

            GetCurWeaponImage();
            CheckWeapon();
        }
    }

    private void CheckWeapon()
    {
        bool enable = Ammo[weapons[curWeaponIndex].Ammo] > 0;

        FireButton.interactable = enable;
    }

    private void GetCurWeaponImage()
    {
        CurWeaponIcon.sprite = weapons[curWeaponIndex].HUDIcon.sprite;

        UpdateAmmoDisplay();
    }

    public void PickupVulcanWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Vulcan))
        { 
            weapons.Add(WeaponType.Vulcan, new Weapon() { Type = WeaponType.Vulcan, HUDIcon = VulcanImage, Ammo = AmmoType.Regular });
            MiniVulcanIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSoloGunWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.SoloGun))
        {
            weapons.Add(WeaponType.SoloGun, new Weapon() { Type = WeaponType.SoloGun, HUDIcon = SoloGunImage, Ammo = AmmoType.Energy });
            MiniSoloGunIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSniperWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Sniper))
        { 
            weapons.Add(WeaponType.Sniper, new Weapon() { Type = WeaponType.Sniper, HUDIcon = SniperImage, Ammo = AmmoType.Sniper });
            MiniSniperIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupShotgunWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.ShotGun))
        { 
            weapons.Add(WeaponType.ShotGun, new Weapon() { Type = WeaponType.ShotGun, HUDIcon = ShotgunImage, Ammo = AmmoType.Shotgun });
            MiniShotgunIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }

    public void PickupSeekerWeaponButtonClick()
    {
        if (!weapons.ContainsKey(WeaponType.Seeker))
        { 
            weapons.Add(WeaponType.Seeker, new Weapon() { Type = WeaponType.Seeker, HUDIcon = SeekerImage, Ammo = AmmoType.Energy });
            MiniSeekerIcon.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
        }
    }


    public void FireWeaponButtonClick()
    {
        Ammo[weapons[curWeaponIndex].Ammo] -= weaponAmmoRate[Convert.ToInt32(weapons[curWeaponIndex].Ammo)];

        if (Ammo[weapons[curWeaponIndex].Ammo] == 0)
            FireButton.interactable = false;

        UpdateAmmoDisplay();
    }

    private void UpdateAmmoDisplay()
    {
        AmmoDisplay.text = Ammo[weapons[curWeaponIndex].Ammo].ToString();
    }

    public void PickupRegularAmmoButtonClick()
    {
        Ammo[AmmoType.Regular] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupEnergyAmmoButtonClick()
    {
        Ammo[AmmoType.Energy] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupShotgunAmmoButtonClick()
    {
        Ammo[AmmoType.Shotgun] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

    public void PickupSniperAmmoButtonClick()
    {
        Ammo[AmmoType.Sniper] += Convert.ToInt32(EventSystem.current.currentSelectedGameObject.tag);
        CheckWeapon();
        UpdateAmmoDisplay();
    }

}

I had originally planned on using an enum for weaponAmmoRate, but the duplicate values made that impractical. Obviously the values need to match the order of the WeaponType enum.

The weapons variable holds all the possible weapons a ship can carry, even if they’re not equipped. If you add new weapons to the WeaponType enum, there will automatically be room for them in the possible weapons the ship can equip, no change necessary.

The curWeaponIndex is straightforward, it points to the item in the weapons array that’s currently selected.

CurWeaponIcon is the image in the scene that shows the player the weapon that’s currently selected. We need the member here to allow us to change it when the player uses an input device to switch between weapons. We could grab the Image every time the weapon is switched, but that’s cumbersome, more code than we need to use and wastes time. You’ll see this a lot in Unity, having a member in a script that is hooked to a UI object. This is one of the features of Unity that I really like. The Unity IDE recognizes the public members of a script and allows you to set them by dragging something from the Hierarchy onto it in the Inspector.

The next two chunks of Images are used to set the CurWeaponIcon and display which weapons the ship has equipped respectively.

We have a reference to the button that fires the selected weapon as we need to enable or disable it when the selected weapon is changed or if the selected weapon runs out of ammo.

As the player fires the selected weapon, we use the AmmoDisplay member to update the amount of ammo that’s available for the weapon.

Some games allow the selection of the weapon to wrap from last to first or vice versa when navigating through the equipped weapons. The WrapWeapons member allows us to decide whether or not we enable this feature. The code for handling the navigation between weapons uses this member to correctly set the selected weapon as we’ll see shortly.

The Ammo member holds the amount of ammo for every type of weapon. In the case of the some weapons you might want this is be infinite. This is usually the case with the default or first weapon that’s equipped in the ship. Setting the value to –1 for a weapon allows for infinite ammo for a weapon.

At the start of the scene we create our weapons array, add a Vulcan to it, set all the UI elements to indicate we have a weapon set, and add some ammo for it.

In the NextWeapon and PreviousWeapon methods we first check to see if the current weapon is the last or first respectively and if weapon wrap is not turned on we just return as there’s nothing to do. We then go through the weapons list checking to see if a weapon has been equipped on the ship to select. If we go through the entire list and don’t find one we just re-select the original weapon. We set the select weapon icon and check to see if the weapon has ammo, disabling the fire button if not. We also update the display of the available ammo for the selected weapon.

For each of the method for picking up a weapon we first see if the weapon is already equipped. If not, we create an instance in the weapons list and set the icon to indicate that weapon is available.

When the fire button is pressed we subtract from the ammo for that weapon based on the weapon rate for the weapon and disable the weapon is the ammo has run out. There’s a potential bug in this code. See if you can find it. 😉

When ammo is picked up, we grab the Tag property of the GameObject to see how much ammo it contains and add that to the proper element in the Ammo array. One thing that you could change for a real game is add a property to the ammo GameObject to indicate what type of ammo it is and have one method for handling all ammo pickups. Try replacing the buttons with a GameObject that does this.

That’s it. Pretty simple I think, but it should be enough to at least start you out with a space shooter type game. As also, feedback and questions are welcome. :)

More GameSparks and Unity Goodness

So if you’ve read the previous post and have a GameSparks project set up with leaderboards and achievements and a Unity project that handles adding achievements and scores you’re all set to complete the basics by adding functionality to display the high scores and the achievements a player has earned.

Add a new scene and set it up like the following:

leaderboards

All that’s needed is a few lines of code to display the scores in the leaderboard. Open the Leaderboards class and add the following:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;

public class Leaderboards : MonoBehaviour {

	// Use this for initialization
	void Start () {

        LeaderboardDataResponse response = new LeaderboardDataRequest().SetLeaderboardShortCode("HighScoreLeaderboard").SetEntryCount(10).Send();

        if(!response.HasErrors)
        {
            foreach (var entry in response.Data)
            {
                Text player = GameObject.Find("Player" + entry.Rank.ToString()).GetComponent<Text>();
                player.text = entry.UserName;
                Text score = GameObject.Find("Score" + entry.Rank.ToString()).GetComponent<Text>();
                score.text = entry.GetNumberValue("Score").ToString();
            }
        }
        else
        {
            Text player = GameObject.Find("Player1").GetComponent<Text>();
            player.text = "No Scores";
        }
	}

    public void OKButtonClick()
    {
        Application.LoadLevel("MainMenuScene");
    }
}

If you don’t have any scores other than the one from the previous post you should add some players through the GameSparks Test Harness and log some scores. To do so use the RegistrationRequest and AuthenticationRequest calls:

new player2

 

authentication

then add scores using the PostHighScoreEarned call under LogEvent:

posthighscore

Make sure you make all three calls in this order or your scores won’t be posted correctly.

On to achievements. I created a couple of additional achievements from the one created in the previous post:

achievements

Of course, we’ll need a scene to display them in:

achievementsscene

The graphic for each achievement is a UI Image object. We set the tag for the graphic to the achievement’s shortcode so we can find it when the scene loads.

Create a script and attach it to the scene’s Canvas as usual. Add the following to it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;


public class Achievements : MonoBehaviour {

	// Use this for initialization
	void Start ()
    {
        new ListAchievementsRequest().Send((response) =>
        {
            if (!response.HasErrors)
            {
                foreach (ListAchievementsResponse._Achievement achievement in response.Achievements)
                {
                    //if achievement hasn't been earned grey out icon
                    if (achievement.Earned.HasValue && !achievement.Earned.Value)
                    {
                        Image graphic = GameObject.FindGameObjectWithTag(achievement.ShortCode).GetComponent<Image>();
                        graphic.color = new Color(1f, 1f, 1f, .1f);
                    }
                }
            }
        });
	}

    public void OKButtonClick()
    {
        Application.LoadLevel("MainMenuScene");
    }

}

The ListAchievementsRequest call gets all the achievements set up in the system with a member for each achievement that tells if the player has earned it. If he hasn’t we change the alpha component of the color for the achievement’s graphic to indicate that the player hasn’t earned it.

That’s all it takes to get basic achievements and leaderboards working in your game. The project for this post is located here.

There is a lot more functionality in GameSparks that I’d like to cover. I’m thinking I might dig into some of the social functionality like friends and messages next. If you’ve looked into GameSparks and there’s something that you’d like to see covered let me know.

Integrating GameSparks into a Unity game

A big selling point of video games is having things like leaderboards and awards/achievements. Unfortunately, implementing these things is a lot of work. Tools like Azure Mobile Services and 3rd party offerings make this a little easier, but it’s still a lot of work. I originally started looking at Azure Mobile Services, via bitrave, but recently stumbled across something that looked like it might be a bit easier, being built specifically for what I was looking to implement – GameSparks. I’m going to go over using the SDK in a couple of posts, each one being relatively short and sweet.

Obviously, the first thing that needs to be done is to download and install the SDK. Rather than rewrite something, just follow the instructions here and come back when you’re done. Go ahead, I’ll wait. :)

OK, so you should be all set up and able to authenticate against the service. The first thing we’ll need is some way to log in to the game. We have to have an ID for the player in order to track things like leaderboards and achievements. We’ll need a scene to allow the user to enter the data for a user ID and password:

login screen

Create a new scene and place some UI controls similarly to the layout in the picture. Notice that I’ve put a GameObject in the scene for the GameSparks functionality as the instructions above stated. Once you have the scene create a Login class and add the following to it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;

public class Login : MonoBehaviour {

    public InputField LoginIDText;
    public InputField EmailText;
    public InputField PasswordText;
    public Text LoginErrorText;

    public void CreateAccountButtonClick()
    {
        //ensure all items were entered
        if (LoginIDText.text.Length > 0 && EmailText.text.Length > 0 && PasswordText.text.Length > 0)
        {
            RegistrationResponse response = new RegistrationRequest().SetUserName(LoginIDText.text).SetDisplayName(LoginIDText.text).SetPassword(PasswordText.text).Send();

            if (!response.HasErrors)
            {
                Global.UserID = response.UserId;
                Application.LoadLevel("MainMenuScene");
            }
            else
                LoginErrorText.text = "All information must be entered";
        }
    }

    public void OKButtonClick()
    {
        //verify credentials
        if (LoginIDText.text.Length > 0 && PasswordText.text.Length > 0)
        {
            AuthenticationResponse response = new AuthenticationRequest().SetUserName(LoginIDText.text).SetPassword(PasswordText.text).Send();
            if (!response.HasErrors)
            {
                Global.UserID = response.UserId;
                Application.LoadLevel("MainMenuScene");
            }
            else
                LoginErrorText.text = "Incorrect credentials";
        }
    }

    public void ExitButtonClick()
    {
        Application.Quit();
    }
}

Note the references to the GameSparks namespaces added at the top. We’ll need these to send requests to the service. We have the members that we’ll link to our controls so we can retrieve the text in them and 3 methods for handling clicking on each button.

If the player is creating an account, we send the info to the GameSparks service using the RegistrationRequest object and assuming we get a valid response set a global (yes, I use globals. Don’t hate! :D) with the ID so we can use it later, then load the menu scene. If the player already has an account we use the AuthenticationRequest object and, if the credentials are correct, load the menu scene. Fairly straightforward stuff. Once you have the code entered, save and go back to Unity. Drag the class from the Project tab onto the Canvas in the Hierarchy and hook up all the events and drag the input fields onto the Login class members in the Inspector. If you’re not sure how to do this, head over to the Unity Tutorials site and learn the basics of working in the Unity IDE. If you’re really stuck let me know and I’ll try to walk you through the process. :) The interesting stuff happens next.

Before we can use the Achievements and Leaderboards scenes we need to set up some achievements and leaderboards in GameSpark. In the GameSparks Configurator, click on Achievements, then the Add New “+” graphic:

gamesparksachievements

Add some info for the achievement:

achievementinfo

Click the Save button and you’re done with that part. You can add as many other achievements as you’d like of course.

Next we’ll need an event that’ll be called when an achievement is earned. Click on the Events item in the Configurator and create a new Event:

achievementevent

The ShortCode is the same as the Name here since the text area isn’t wide enough to show it. :( We’ll send the Player’s ID and the shortcode for the achievement to a chunk of Cloud Code that’ll save the achievement. After you save the event, go ahead and click on the Cloud Code item. Under the Bindings | Events item, click on the PostPlayerAchievementEarned item and add the following code:

achievmentcloudcode

Save that and that’s it for the GameSparks side of things.

How do we tell GameSparks the player has earned an achievement? Good question, let’s implement that now. Create a new scene that we’ll use to fake earning an achievement (and adding to a leaderboard soon):

testscene

Add a script file call GameSparksTest and add the following to it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GameSparks.Api;
using GameSparks.Api.Messages;
using GameSparks.Api.Requests;
using GameSparks.Api.Responses;
using GameSparks.Core;



public class GameSparksTest : MonoBehaviour {
    public Text ResponseText;
    public void TestAchievementButtonClick()
    {
        LogEventRequest request = new LogEventRequest(); ;
        request.SetEventKey("PostPlayerAchievementEarned");
        request.SetEventAttribute("PlayerID", Global.UserID);
        request.SetEventAttribute("AchievementID", "PlayedGame");

        ResponseText.text = "";

        request.Send((response) =>
        {
            if (!response.HasErrors)
                ResponseText.text = "Achievement Added";
            else
                ResponseText.text = response.Errors.JSON;
        });

        if (ResponseText.text == "")
            ResponseText.text = "Response not received";

    }

}

Fairly straightforward. Create the LogRequest object, set the name of the event to be called, set the parameters, and send it off.

Leaderboards work in a similar fashion. Head back to the GameSparks Configurator and set up an event:

leaderboardevent

Then set up a leaderboard:

leaderboard

That’s it for the GameSparks side of things. Go back to the GameSparksTest class in your Unity project and add a handler for the TestLeaderboardButton (making sure to hook it up to the button):

public void TestLeaderboardsButtonClick()
{
    LogEventRequest request = new LogEventRequest();
    request.SetEventKey("PostHighScoreEarned");
    request.SetEventAttribute("Score", 10);

    request.Send((response) =>
    {
        if (!response.HasErrors)
            ResponseText.text = "High Score Posted";
        else
            ResponseText.text = response.Errors.JSON;
    });

    if (ResponseText.text == "")
        ResponseText.text = "Response not received";
}

It doesn’t get much simpler than that I think. You can use the Test Harness section of the GameSparks site to pull the data for the achievements and leaderboards for the player to verify that the data was saved. Using the Test Harness is pretty straightforward. You’ll have to make an authentication call, then use the List AchievementRequest under the Player section and the ListLeaderboardsRequest under the Leaderboards section, filling in the necessary information in the JSON.

I’ve put the Unity project for this post here. You’ll have to fill in your own GameSparks credentials of course. Feel free to ask any questions about what we’ve gone over.

Next post I’ll add functionality to pull and display the achievements for a player and display the leaderboard information. Take a stab at figuring it out beforehand if you’d like.

World Screen Screenshot

Well, it’s getting there:

WorldScreen

New Dungeon Editor Screenshot

Some progress:

  • Added 2nd layer for regular and secret doors
  • Triggers (X in screenshot) and traps (square in screenshot) now rendering
  • Load and save working, using sharpSerializer since normalize XmlSerializer doesn’t like arrays, among other things.

 

DungeonEditorScreenshot2

Dungeon Editor, 1st Iteration

Behold my awesome graphical skills! :) It’s got a long way to go, but it’ll do for now:

 

DungeonEditor

Thoughts on Stat Types and Skills

So I’m thinking of going with just a few types of stats for the dungeon crawler I’m prototyping:

Strength – how much a character can carry and modifies the damage done with melee weapons
Dexterity – helps determine if a character hits during combat and how well he performs skills like lockpicking
Agility – how well a character moves and dodges during combat and performs skills like climbing
Constitution – helps determine a character ability to soak up damage, whether in combat or due to things like poison. Fairly standard.
Intelligence – overall stat for character’s mental abilities, includes will power in resisting magical spells like illusions, ability to learn spells, etc. I can’t think of a better term for this.

I don’t want to overdo this, but I want some flexibility and “realism” (quoted because I mean realistic in the game world, not necessarily in this one :D). Some RPG systems lump agility and dexterity together into one stat, which I think doesn’t fit. A character could be a great lockpicker but horrible at dodging blows in combat or a great swordsman but doesn’t climb very well, so one stat for both is kind of silly.

For skills, the usual dungeon crawler types of actions will need to be done by a character, besides combat. Lockpicking, the ability to find things that don’t want to be easily found (traps, secret door, etc.), climbing (maybe, for getting out of pits if I implement them), sneaking up on other entities. I’m not sure how deep I want to go here. Implementing skills means dealing with allowing a character to become better at them, and I’ve never really liked many skill progression systems. “Realistically”, a character would get better at things the more they do them, as in games like Dungeon Siege. That’s a good bit of work to handle. Less realistic is allowing a character to “train” at skills by spending skill points. Easier to implement, but just feels wrong.