0

I am saving game data in my XNA C# Windows game and I've come across a problem (due to my lack of knowledge).

I've created a struct outside my public class Game1 as follows

public struct SaveGameData
    {
        public string PlayerName;
        public int Score;
    }

Then inside the main method (public class Game1)

SaveGameData saveGameData = new SaveGameData()
        {
            PlayerName = "Jimmy",
            Score = 100,
        };

I can't access PlayerName elsewhere in my code so I thought I could set PlayerName = to another variable such as "string name". But I get the following error message

a field initializer cannot reference the nonstatic field, method or property

Is there a way of changing these values dynamically? Or am I going about it the wrong way?

EDIT

Okay sorry for the lack of information provided.

I was declaring two variables

public String name;
public int score; //small s

Then I was trying to set PlayerName = name and Score = score but I was getting the above error.

By taking Wimmel's advice, I changed the fields to static and that is gotten rid of the error.

However, in my Update method, when I update the score value...the value of Score is not updating also. In my .txt file that I am outputting to, the Score value is the initial value of "score"

UserBruiser
  • 155
  • 1
  • 2
  • 17
  • 2
    Is that *really* the code? Because it should work .. "the *nonstatic* field" part of the error message seems out of place. –  Nov 25 '12 at 19:59
  • Agree with @pst this works just fine when I try it in VS 2010. – Andy Nov 25 '12 at 20:06
  • It all works fine if I set PlayerName = "jimmy" or any other text but if i set it to a String variable, it gives me the error. – UserBruiser Nov 25 '12 at 20:09
  • Give an example of how you set that string value. – neeKo Nov 25 '12 at 20:10
  • Using the Score as an example, I was setting 'public int score;' I then changed the above(original post) to Score = score, I have taken Wimmel's advice and changed this to 'public static int score;' This has gotten rid of the error but it doesn't seem to update properly, but I suppose that's a different problem – UserBruiser Nov 25 '12 at 20:19

1 Answers1

4

You are trying to access SaveGameData which is just the type. You must change saveGameData or make the fields static.

wimh
  • 15,072
  • 6
  • 47
  • 98
  • @pst, The code is missng, but you will get that errormessage when you try to access `SaveGameData.PlayerName`. – wimh Nov 25 '12 at 20:01
  • Naturally; I'm just trying to flush out this answer - but that useful information as an example. –  Nov 25 '12 at 20:04
  • @Wimmel no? By changing the fields to static, it has gotten rid of the error. Naturally, I'm thinking that, that was the problem? – UserBruiser Nov 25 '12 at 20:23
  • @NoGimmicks lol, ok. but it would have been more clear if you added an example of the code which caused the error. – wimh Nov 25 '12 at 20:26
  • @Wimmel I've added more information but I think you've answered my question Wimmel, thank you! – UserBruiser Nov 25 '12 at 20:33
  • 1
    @NoGimmicks: you didn't fix the problem, you merely papered over it. Show your full code and we can come up with an actual fix. – siride Nov 25 '12 at 20:44
  • @siride haha sorry. basically I'm using this http://stackoverflow.com/questions/3723287/what-is-a-good-example-of-saving-game-data-in-xna-4-0 I've edited to fit my code, I'm not sharing my entire game1.cs as I've a lot there (bad OO practice I know) – UserBruiser Nov 25 '12 at 20:54
  • 2
    @NoGimmicks: you only need to show the line that's failing, not your whole code. – siride Nov 25 '12 at 20:55
  • @siride ahhhhhhhhhh. "Show your full code" I'm getting mixed signals haha It's OK. I'm not getting an error anymore, Wimmel fixed it. The only other issue is that my output file is not being updated but don't worry, I'll sort it out. Thank you anyway, for wanting to help. – UserBruiser Nov 25 '12 at 21:04
  • @NoGimmicks: no, Wimmel didn't fix it, and that might be why you're having other problems. This can be fixed really easily if you want, and I suggest you just post the line of code that gives you the error. – siride Nov 25 '12 at 21:06
  • @siride but I did? Before when I was getting an error, it was as described above. The lines that gave me an error was PlayerName = name; and Score = score; – UserBruiser Nov 25 '12 at 23:39
  • 2
    @NoGimmicks: ahh, I see what's going on, although it's still hard to piece together based on the paucity of code you provided. Yes, making the fields `name` and `score` static is an acceptable solution, since they, as instance members, cannot be accessed in your static `Main` method. I thought you were setting your structure fields to be static. I apologize for wasting your time. – siride Nov 26 '12 at 01:11