0

I'm making an XNA 4 game, and I want to create checkpoints for certain areas in my stage. I figured I could create a "ghost" object of the player object that would be created whenever the player reaches a checkpoint. And when you want to reload that checkpoint, you'll start to where that ghost player is. As for now, I thought this would be an easy way to achieve this (although I think XML may be a better solution, but I've no idea on how to use that, yet). But the player has too many variables (health, stamina, ammo, bleeding timer, silver keys, golden keys, coins, infection, position, speed, angle, states [involve dying, infected, bleeding, burning, dodging, etc], I just thought that statemets like "continuep1 = p1;" (both are instances of the same object) would re-assign all the variables to the values that the other class contains, but when I tried to re-load "p1 = continuep1" it wouldn't work. It doesn't seem to do anything.

So I'm wondering, do I have to re-assign ALL the variables one by one? Should I start using XML? Or is there a way to assign all the variables without having to do it one by one? (I'm not asking for code, unless the last question is possible)

Roman
  • 28
  • 3
  • `continuep1 = p1;` is reference assigning, it becomes the same object, so that's why it won't work. Other than copying all the fields by hand, you could use either reflection or serialization to clone the object. A nice short example on using reflection for a shallow copy can be found at http://stackoverflow.com/a/8181736/1020861 – neeKo Dec 23 '13 at 10:13

1 Answers1

1

In my opinion I would say yes, start using xml. Don't worry about using xml, it's pretty simple once you get your head around it. A great way to learn would be to see your characters information displayed in xml format.

I think the most ideal option would be to serialize your game. So this involves storing the state of your character, the position it is at, the direction it is facing(which texture is currently loaded) and the stats(health, staming, bleeding etc).

With this you could reload your game from the last save when the player dies or when the game is next played. This would solve both cases.

This tutorial on making a top-down RPG game in Xna 4 is very good in my opinion and goes into good detail. Many of the techniques in this tutorial apply to more than just this genre and style of game.

http://xnagpa.net/xna4rpg.php

Part 11A(Game Editor) is the first part of a tutorial guiding the reader in making an editor for their game. With this Winforms project items, classes, quests etc can be added to the game and are stored as Xml files. At the bottom of page 6, and pages 7 and 8 outlines methods to serialize and deserialize your game.

In Part 11C(Game Editor), particularly at the bottom of page 9 and page 10. A save game method is introduced which serializes the game. You can also look at the new game and open game methods too.

These methods work with DataManager classes which store the items, classes etc in Dictionary's. This may be how you want to model your game information, if not the code in these guides may have to be altered to work with your solution.

I thought it best to provide my source for learning on this subject, rather than regurgitating it. I would recommend reading around this, not just the pages I've pointed to. Furthermore, in a later tutorial the serialization goes a step further and the files are stored in .xnb, which is a much more secure way to store information.

I hope this has helped and that you appreciate the guide in the right direction as opposed to just being given the answer. Besides, this isn't a task that would be solved in one method anyway.

Let me know how you get on and I'd be happy to help more.

tjheslin1
  • 1,378
  • 6
  • 19
  • 36
  • ah, I'll check that one out, I used a .txt file to make my maps, I think all you described would be even better. I'm guessing this would involve re-arranging the code all over again? If that's so, I guess I'll jut finish this game as it is right now, and start anew with this new method. But thanks! I'm sure I can find a lot of useful information there! :D – Roman Dec 23 '13 at 20:57
  • Glad I could help. It's a really good tutorial. I think you could quickly read through it and take parts out of it. I followed it completely as though I had done graphics/game programming before, I had never done it with XNA. Also, on of the projects in this guide is a generic rpg engine class, useful for the next game made! – tjheslin1 Dec 23 '13 at 22:56