XNA Creators Club Online
Page 1 of 1 (14 items)
Sort Posts: Previous Next

classes problem...again

Last post 12/10/2008 12:17 AM by Gilgamesch. 13 replies.
  • 12/7/2008 4:49 PM

    classes problem...again

    hey guys,

    me again having problems with classes xD.

    so i have this over here:



    clNPC[ NPC = new clNPC[10];
    clNPC[ MapNPC = new clNPC[5];


    for (byte i = 1; i < 6; ++i)


    {
       MapNPC[i] = NPC[the map number and so on. mapnpc[i]]
    }


    for (byte i = 1; i < 6; ++i)
    {
        Random RandomCoord = new Random();
        MapNPC[i].X = Convert.ToInt16(RandomCoord.Next(0, 25));
        MapNPC[i].Y = Convert.ToInt16(RandomCoord.Next(0, 25));
    }

    now the problem here is:


    all MapNPC have the same x and y...o.O

    how come?


    thanks!

  • 12/7/2008 5:09 PM In reply to

    Re: classes problem...again

    Gilgamesch:
    all MapNPC have the same x and y...o.O

    how come?

    Whenever you do new Random() it will pick a seed value based on the current time. Your loop will run fast enough that it will probably get the same seed value each time it goes through the loop, so it will give the same two numbers. All you do to is new a Random variable once and keep using Next() on that.
  • 12/7/2008 5:20 PM In reply to

    Re: classes problem...again

    hmm doesnt work..

    i put the new Random before the whole loop
  • 12/7/2008 5:32 PM In reply to

    Re: classes problem...again

    clNPC[ NPC = new clNPC[10];
    clNPC[ MapNPC = new clNPC[5];

    for (byte i = 0; i < MapNPC.Length; i++)
    {
        MapNPC[i] = NPC[the map number and so on. mapnpc[i]]
    }

    Random RandomCoord = new Random(Environment.TickCount);

    for (byte i = 0; i < MapNPC.Length; i++)
    {
         MapNPC[i].X = Convert.ToInt16(RandomCoord.Next(0, 25));
        MapNPC[i].Y = Convert.ToInt16(RandomCoord.Next(0, 25));
    }
    Mercury Particle Engine - add visual effects to your windows and Xbox360 games with ease!
    http://mpe.codeplex.com/
  • 12/7/2008 6:29 PM In reply to

    Re: classes problem...again

    hmm yeah i tried it like this, somehwow for every mapnpc the x is the same and the y is the same so:


    mapnpc[1].x = 3;
    mapnpc[1].y = 5;


    mapnpc[2].x = 3;
    mapnpc[2].y = 5;


    edit:


    i tried this here:


    MapNPC[1].X = 2;
    MapNPC[1].Y = 2;

    MapNPC[2].X = 3;
    MapNPC[2].Y = 3;

    MapNPC[3].X = 4;
     MapNPC[3].Y = 4;

    MapNPC[4].X = 5;
    MapNPC[4].Y = 5;

    MapNPC[5].X = 6;
    MapNPC[5].Y = 6;

    x1 = MapNPC[1].X;
    y1 = MapNPC[1].Y;

    x2 = MapNPC[2].X;
    y2 = MapNPC[2].Y;

    x3 = MapNPC[3].X;
    y3 = MapNPC[3].Y;

    x4 = MapNPC[4].X;
    y4 = MapNPC[4].Y;

    x5 = MapNPC[5].X;
    y5 = MapNPC[5].Y;



    now i put a break-point at:  y5 = MapNPC[5].Y, and now see what the x1,y1 and so on are, it tells me that all equal 6...how come? o.O
  • 12/7/2008 9:35 PM In reply to

    Re: classes problem...again

    It's been a couple of weeks since I wrote a line of C# now and you'd be surprised how quickly you forget specifics like this. I do wish to remember that you can't modify members on struct instances through an indexer into a list or array like that. Instead you need to write your modifier code like this.

    Random rnd = new Random(whateverSeedVariable); 
     
    for(int i = 0; i < MapNPC.Count; ++i) 
        //copy list instance to temporary variable 
        Vector2 temp = MapNPC[i]; 
     
        //modify the members 
        temp.X = rnd.Next(0, 25); 
        temp.Y = rnd.Next(0, 25); 
     
        //copy the modyfied variable back into the list 
        MapNPC[i] = temp; 
     

  • 12/7/2008 9:53 PM In reply to

    Re: classes problem...again

    oke thanks for the reply but the thing is, both mapnpc[i].x and .y are integers for themselfes :O...so they arent declared as integers at all, and somehow all the different variables inside mapnpc[i] will change for all the i's as soon as i change it for one o.O
  • 12/7/2008 10:37 PM In reply to

    Re: classes problem...again

    Presumably then every element of MapNPC refers to the same object. Are you aware that classes are reference types and what that means? If variables are a reference type then more than one of them can refer to the same instance of an object, so changing the data for an object using one variable, you will see it changed for all the other variables that refer to that object.
  • 12/7/2008 10:54 PM In reply to

    Re: classes problem...again

    ok i know what you mean but look at this, tahts my code:

    public class clNPC
        {
           public string name;
           public int x,y;
         
           public clNPC()
           {
              name = "";
              x = 0;
              y = 0;

           }


    at an other codefile i have:



    clNPC[ NPC = new clNPC[10];
    clNPC[ MapNPC = new clNPC[5];




    and then:





    for (byte i = 0; i < 11; ++i)
         NPC[i] = new clNPC();

    for (byte i = 1; i < 6; ++i)
            MapNPC[i] = new clNPC();



    NPC[1].Name = "Test";
    NPC[1].X = 10;
    NPC[1].Y = 11;



    for (byte i = 1; i < 6; ++i)
    {
       MapNPC[i] = NPC[the map number and so on. mapnpc[i]..so basicly i saved it for testing purpose as 1]
    }


    for (byte i = 1; i < 6; ++i)
    {
        Random RandomCoord = new Random();
        MapNPC[i].X = Convert.ToInt16(RandomCoord.Next(0, 25));
        MapNPC[i].Y = Convert.ToInt16(RandomCoord.Next(0, 25));
    }




    now, every for all MapNPC[i] the .x and .y is changed into the last .x and .y.


    so if MapNPC[1].x = 5;

    and later i have MapNPC[5].x = 3;

    the MapNPC[1].x will be 3 too...


    thanks!


  • 12/7/2008 11:01 PM In reply to

    Re: classes problem...again


    for (byte i = 1; i < 6; ++i)
            MapNPC[i] = new clNPC();
    How will that even run without throwing an exception? Array indexing starts at 0, so you're missing one already by starting your for loop at 1, and if you go until i == 5 (i < 6 constraint) you should get an index out of bounds exception because your array only has 5 slots in it (indices 0, 1, 2, 3, and 4).

    for (byte i = 1; i < 6; ++i)
    {
        Random RandomCoord = new Random();
        MapNPC[i].X = Convert.ToInt16(RandomCoord.Next(0, 25));
        MapNPC[i].Y = Convert.ToInt16(RandomCoord.Next(0, 25));
    }
    As mentioned above this will not work. Every Random object you allocate will start with the same seed (because the seed is based on time and you're in a tight loop) so they will always generate the same sequence of numbers. You need to write that loop like this:

    // instantiate the Random object outside of the loop 
    Random RandomCoord = new Random(); 
     
    // use 0 and 5 here; not 1 and 6. arrays start indexing at 0. 
    for (byte i = 0; i < 5; ++i) 
        MapNPC[i].X = Convert.ToInt16(RandomCoord.Next(0, 25)); 
        MapNPC[i].Y = Convert.ToInt16(RandomCoord.Next(0, 25)); 

  • 12/7/2008 11:24 PM In reply to

    Re: classes problem...again

    Gilgamesch:

    for (byte i = 1; i < 6; ++i)
    {
       MapNPC[i] = NPC[the map number and so on. mapnpc[i]..so basicly i saved it for testing purpose as 1]
    }

    I'm not sure what you mean here (it would be better to copy and paste the exact code you're running), but I guess that this is where you are assigning each element of MapNPC to refer to the same object from NPC. Using = won't copy the actual object, it will just make the variables refer to the same object.

    If you do MapNPC[1] = NPC[1] and then MapNPC[5] = NPC[1], changing MapNPC[1].x will change the same data as using MapNPC[5].x would.
  • 12/8/2008 2:20 PM In reply to

    Re: classes problem...again

    Opprobrious:
    Gilgamesch:

    for (byte i = 1; i < 6; ++i)
    {
       MapNPC[i] = NPC[the map number and so on. mapnpc[i]..so basicly i saved it for testing purpose as 1]
    }

    I'm not sure what you mean here (it would be better to copy and paste the exact code you're running), but I guess that this is where you are assigning each element of MapNPC to refer to the same object from NPC. Using = won't copy the actual object, it will just make the variables refer to the same object.

    If you do MapNPC[1] = NPC[1] and then MapNPC[5] = NPC[1], changing MapNPC[1].x will change the same data as using MapNPC[5].x would.


    okey, that seems to be the exact problem i am facing,

    so how can it be done? when i want for all i's the MapNPC[i] take over all the data from NPC[1] for e.g, like this? :


    for (byte i = 1; i < 6; ++i)
    {
       MapNPC[i].Name = NPC[1].Name
       MapNPC[i].x = NPC[1].x
       MapNPC[i].y = NPC[1].y
    }


    oir would it still just refer and not copy the data?


    thanks very much for your help guys!
  • 12/9/2008 7:42 AM In reply to

    Re: classes problem...again

    The answer is... it depends. Study the difference between Value Types and Reference Types to figure this one out. Your examples there (Name, number, number) are value types and will copy. But if they were objects it would copy a reference instead. Primitives, enums, and structs are all value types which will copy by value. Did you know you can mouse over a variable to figure out if its an object or a value? The type will appear in the left side of the yellow box that appears.
  • 12/10/2008 12:17 AM In reply to

    Re: classes problem...again

    thanks for the help, yeah i found out about the yellow box like 5 hours ago xD

    and i fixed the problem ^^
Page 1 of 1 (14 items) Previous Next