Detrumentum

Hey everybody,

I have recently been coding up a small 8-bit RPG, when (and of course, anyone who knows about coding RPGs would know this) all of a sudden, I realized that I had a need for a random number generator. Having learned a little bit in C#, I thought it would be similar in XNA, so I typed up the following:

Random Damage = new Random();

int RandomNumber = Damage.Next(3);

int PlayerDamage = Damage;

I thought this was pretty straight-forward, but I guess it isn't. I'm getting the following errors:

int RandomNumber = Damage.Next(3);

A field initializer cannot reference the nonstatic field, method, or property 'WindowGame1.AvatarBase.Damage'

int PlayerDamage = Damage;

A field initializer cannot reference the nonstatic field, method, or property 'WindowGame1.AvatarBase.Damage'

And lastly...

int PlayerDamage = Damage;

Cannot implicitly convert 'System.Random' to 'int'

I've searched up the forums and didn't find much similar (and hopefully this is the correct forum to place this). Any suggestions would be great.



Re: Game Technologies: General XNA (C#) Random Number Generation

George Clingerman

Well, you have a couple things going on here. First, you cannot obtain the random number outside of a method like it looks like you are doing.

So you need to initialize your RandomNumber and PlayerDamage objects inside of a method. That will take care of your "A field initializer cannot reference the nonstatic field, method or propert.." errrors.

So your code will need to look something like this.

void IniitializeDamage()
{
RandomNumber = Damage.Next(3);
...all your other code
}

The second thing is that you are trying to store the Random object you called "Damage" in your integer you created called "PlayerDamage". The compiler is not going to like that. I'm not sure exactly what you were trying to do with that, but you either need to make PlayerDamage a Random type or you need to get a Random number from "Damage" using the .Next method so that you are setting the same types.

Hope that helps. Also, you will want to start posting questions over at http://creators.xna.com because these forums will be locked down for questions soon.





Re: Game Technologies: General XNA (C#) Random Number Generation

Detrumentum

Fantastic! Thanks a bunch!

As for the link, I believe I'll be a frequent for the next little bit (for making a mistake such as the whole Damage / PlayerDamage thing hehehe)