Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

serialization

This topic is 4769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone help me debug some C# serialization issues? Here is the source code: game source My goal is to save the game state out to an XML file and be able to read it back in.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by intrest86
Ummm... could you tell us what the actual problems are, and post the problem code?


I'm not sure what the problem is. I guess that is my point...I don't understand serialization well enough even to troubleshoot it.

The code is posted at the link I provided and is best understood in the context of the running game.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tom Knowlton
I'm not sure what the problem is.


Does it crash? Fail to compile? If it runs, what are the symptoms that it's not working properly? Are you not sure how to code a specific task (e.g. it's psuedocoded out)?

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Tom Knowlton
I'm not sure what the problem is.


Does it crash? Fail to compile? If it runs, what are the symptoms that it's not working properly? Are you not sure how to code a specific task (e.g. it's psuedocoded out)?


The problem is this: I had someone helping me with this game for a few months. He was a lot better in C# than I am. He had lots of good ideas that we BEGAN to implement -- with him helping me each step of the way.

Then he got really busy with work and had no more free time to help me. So now I am left with a game that is designed to use Serialization to save the game state to the hard disk as well as use it for communication over the internet (for online play).

The game is capable of generating an XML file, but the file does not appear to be complete - which seems to indicate that some part of the Serialization process is failing. My guess is it is the array that contains the game piece objects or moves is the failure point, but I don't know how to fix it.

I am having a very difficult time understanding how the XML file is actually supposed to look when it is properly written to disk.

GAMESTORE.CS


using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Robotz
{
[XmlRoot("GamePlayer"), Serializable]
public class GameMove
{
private int _fromX;
private int _fromY;
private int _toX;
private int _toY;
private int _prevMovementPoints;

public GameMove()
{
}


[XmlAttribute("PrevMovePoints")]
public int PrevMovePoints
{
get{return _prevMovementPoints;}
}

[XmlAttribute("FromX")]
public int FromX
{
get{return _fromX;}
}

[XmlAttribute("FromY")]
public int FromY
{
get{return _fromY;}
}

[XmlAttribute("ToX")]
public int ToX
{
get{return _toX;}
}

[XmlAttribute("ToY")]
public int ToY
{
get{return _toY;}
}

public GameMove(int fromX,int fromY,int toX,int toY, int prevMovementPoints)
{
_fromX = fromX;
_fromY = fromY;
_toX = toX;
_toY = toY;
_prevMovementPoints = prevMovementPoints;
}

public void PlayMove(GameWorld gw)
{
gw.MovePiece(_fromX,_fromY,_toX,_toY);
}

}


[XmlRoot("GameTurn"), Serializable]
public class GameTurn
{
private ArrayList _moves;

[XmlElement("move")]
public Robotz.GameMove[] GameMove
{
get
{
Robotz.GameMove[] GameMove = new Robotz.GameMove[_moves.Count];
_moves.CopyTo(GameMove);
return GameMove;
}
set
{
if(value == null) return;
Robotz.GameMove[] movea = (Robotz.GameMove[])value;
_moves.Clear();
foreach(Robotz.GameMove gm in movea)
_moves.Add(gm);
}
}



public ArrayList Moves
{
get{return _moves;}
}

public GameTurn()
{
_moves = new ArrayList();
}

public void AddMove(GameMove gm)
{
_moves.Add(gm);
}

public int DeleteLastMove() //Returns previous movement points for last move made
{
int prevMovementPoint = 0;
if (_moves.Count > 0)
{
prevMovementPoint = ((GameMove)_moves[_moves.Count - 1]).PrevMovePoints;
_moves.RemoveAt(_moves.Count - 1);
}
return prevMovementPoint;
}

public bool CanUndo
{
get {return _moves.Count > 0;}
}


public void PlayTurn(GameWorld gw)
{
foreach (GameMove gm in _moves)
{
gm.PlayMove(gw);
}
}
}

[XmlRoot("GameStore"), Serializable]
public class GameStore
{
private GamePlayer _playerOne;
private GamePlayer _playerTwo;
private ArrayList _gameTurns;

public GameStore()
{
}

[XmlElement("turn")]
public Robotz.GameTurn[] GameTurn
{
get
{
Robotz.GameTurn[] GameTurn = new Robotz.GameTurn[_gameTurns.Count];
_gameTurns.CopyTo(GameTurn);
return GameTurn;
}
set
{
if(value == null) return;
Robotz.GameTurn[] turnsa = (Robotz.GameTurn[])value;
_gameTurns.Clear();
foreach(Robotz.GameTurn ta in turnsa)
_gameTurns.Add(ta);
}
}


public ArrayList Turns
{
get{return _gameTurns;}
}

[XmlAttribute("GameTurnCount")]
public int TurnCount
{
get{return _gameTurns.Count;}
}

[XmlAttribute("GameTurnPlayerOne")]
public GamePlayer GetPlayerOne
{
get{return _playerOne;}
}

[XmlAttribute("GameTurnPlayerTwo")]
public GamePlayer GetPlayerTwo
{
get{return _playerTwo;}
}

public GamePlayer GetCurrentPlayer
{
get
{
if((_gameTurns.Count % 2) == 0)
{
return GetPlayerOne;
}
else
{
return GetPlayerTwo;
}
}
}

public GameStore(GamePlayer playerOne, GamePlayer playerTwo)
{
_playerOne = playerOne;
_playerTwo = playerTwo;
_gameTurns = new ArrayList();
}

public void PlayAllTurns(GameWorld gw)
{
gw.InitGamePieceArray();
gw.PointsLeft = 5; //starting points
foreach (GameTurn gt in _gameTurns)
{
gt.PlayTurn(gw);
gw.PointsLeft = 10; //Next turn points available
}
}

public void AddTurn(GameTurn gt)
{
_gameTurns.Add(gt);
}

public bool CanReplay
{
get{return (_gameTurns.Count > 0);}
}
}
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!