Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
How to Save and Load a Custom Avatar into your Game
Date 12/10/2010    Tags XNA, XBLIG    (0)

The AvatarDescription.Description property holds an array of bytes that describe all the attributes associated with an Avatar. During the course of your game's development, you may find the desire to design your own Avatar and then put that Avatar in your game for all other gamers to see. For instance you may want a nicely dressed crazy looking middle-aged Avatar that would always serve as the host of your wacky trivia game...

Trivia or Die


This tutorial will show you how to collect the information that describes an Avatar, save that information, and then load it into an Avatar. This involves several steps to go through. I'm not aware of an easier way to do this but I am a bit new to the Avatar scene so if you are aware of something, chime in on the comments below and tell me how much time I wasted figuring this out myself and writing a tutorial how to do these unnecessary steps.


Design the Avatar

The easiest way to get the Avatar you want is to design the Avatar as your own by using your Xbox. If you're reading this now (and I suspect you are), you're obviously an Avatar aficionado and need no help here.


Get the current Description

Start your game using a profile that has your newly designed Avatar and load that Avatar (this isn't a basic Avatar tutorial so if you need help with that, try Google). Once you load that Avatar, it's easy to set a breakpoint on the AvatarDescription.Description object and see its value. This is a 1021 item array and unless you want to copy all those values yourself, you'll need a better way to save this description.

Calling the following method with your Avatar's AvatarDescription.Description property as an argument will loop through each byte of the array and create a string from it (one item per line).
public string GetAvatarDescriptionString(AvatarDescription description)
{
    string descriptionString = string.Empty;

    foreach (Byte thisByte in description.Description)
    {
        descriptionString += thisByte.ToString() + Environment.NewLine;
    }

    return descriptionString;
}
Set a breakpoint on either the return line here or somewhere afterwards in your code and run your game. When the game hits the breakpoint, hover over the string containing your description, click on the down arrow next to the magnifying glass that appears, and choose "Text Visualizer". Inside this window, right click the mouse, choose "Select All", right click again, and choose "Copy".

Paste this into a text file and save that file. For this example, we've saved this as AvatarDescription.txt.


Loading the description


Now that you have the bytes that make up the Avatar you want to use, you just need to load this information from the file you saved, copy it into a byte array, and apply it to an Avatar.

Add the text file you saved to your project. For purposes of this example, we will just add it to the root directory but feel free to move it elsewhere if you feel so inclined.

The following method will load the contents of the file you just created and return a byte array just like the one AvatarDescription.Description uses.

public byte[] LoadAvatarDescriptionString()
{
    byte[] description = new byte[1021];
    StreamReader fileStream;
    string thisLine = string.Empty;
    int count = 0;
            
    #if XBOX360
    fileStream = File.OpenText(StorageContainer.TitleLocation + "AvatarDescription.txt");
    #else
    fileStream = File.OpenText("AvatarDescription.txt");
    #endif

    while ((thisLine = fileStream.ReadLine()) != null)
    {
        description[count] = Convert.ToByte(thisLine);
        count++;
    }

    fileStream.Dispose();

    return description;
}
In order to apply this description to your Avatar, you just need to create a new instance of an AvatarDescription object and set a new instance of you AvatarRenderer object using the new AvatarDescription.

// AvatarDescription object
avatarDescription = new AvatarDescription(this.LoadAvatarDescriptionString()); 

// AvatarRenderer object
avatarRenderer = new AvatarRenderer(avatarDescription);   
Hope you enjoy your newly loaded Avatar! And if you're making an Avatar game, I hope it is something worthwhile too. I disapprove of you using this tutorial to aid in making a cash grab Avatar game.


This article has been view 1598 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)