CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    May 2012
    Posts
    6

    Random array element gets value changed with variable

    Hi all,

    I'm making a roguelike RPG in VS 2008, XNA Studio 3.0.

    In the function that follows, I take a random element from an array (the master list of items, iM) and put it into a variable. The array is of type Item, an inherited class I created for all the different kinds of items. Each Item initially has a startingPos of (0,0), and they're supposed to be given a random startingPos before they're added to the mapItems.

    Code:
    public void CreateItems(ref List<Item> iM)
            {
                Random r = new Random();
               
    
                for (int it = 0; it < maxMapItems; it++)
                {
                    int tmpR = r.Next(0, iM.Count);
                    
                    Item tmpItem = iM[tmpR];
    
                   //set starting position for the item on the current map 
    
                    tmpItem.setStartingP( new Vector2((float)r.Next(4, maxTilesH - 4), (float)r.Next(4, maxTilesV - 4)));
    
    
    
                    if (tmpItem.getStartingP() == player.startingPos || tileArray[(int)tmpItem.getStartingP().X, (int)tmpItem.getStartingP().Y].isSolid)
                    {
                        it--;
                        continue;
                    }
                    mapItems.Add(tmpItem);
                    //if the map has a shop, populate it with items 
    
                } 
                if (curMap.isShopMap)
                    {
                    Item tmpShopItem = new Item();
                        foreach (Tile t in tileArray)
                            if (t.name == "floor")
                            {
                                tmpShopItem = iM[r.Next(0, iM.Count)];
                                tmpShopItem.setStartingP(t.absPos);
                               
                                //new Potion(potionTexture, potionNames[r.Next(0,
                                    //potionNames.Count())] + " potion", t.absPos.X, t.absPos.Y);
                                
                                mapItems.Add(tmpShopItem);
    
                            }
                        
                    }
                return;
            }
    The problem is hard to explain. Essentially, after the line
    Code:
     Item tmpItem = iM[tmpR];
    , the array element somehow has its value changed, with the startingPos becoming tmpItem's startingPos. It's as if the new Item is just pointing to the same object. So when the map has a shop, the items just pile on top of each other, because they have the same startingPos.

    I noticed that when I created objects of a subclass of Item (in the above code, Potions) and filled the shop with them, they were in the right place. So it's not the logic, but something in the memory screwing things up.

    Any ideas? Thanks so much to anyone who can help me.

    Katie

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: Random array element gets value changed with variable

    Quote Originally Posted by katnip84 View Post
    The problem is hard to explain. Essentially, after the line
    Code:
     Item tmpItem = iM[tmpR];
    , the array element somehow has its value changed, with the startingPos becoming tmpItem's startingPos. It's as if the new Item is just pointing to the same object. So when the map has a shop, the items just pile on top of each other, because they have the same startingPos.
    I'm pretty sure that's exactly what's happening. C# memory handling is similar to that of Java. When you say Item newItem = something, you're not creating a new object. You're creating a pointer to the 'something' object. If you want to create truly new object, you'll have to call Item newItem = new Item() and then initialize the values to the ones you want. (Probably have a copy constructor.)

    I didn't read through your code all the way, but I noticed that and thought I would mention it. Also, you should really come up with a better variable naming scheme. It's easy to understand for you, but impossible to understand for us.

  3. #3
    Join Date
    May 2012
    Posts
    6

    Re: Random array element gets value changed with variable

    Hi A_D,

    The whole thing was resolved by writing a copy constructor. Thank you!~! I had tried just having the new Item() before, but that had never worked.

    Sorry about the variable names. My function now takes List<Item> iMasterList, which I think is a bit clearer. Following a style was never one of my strongest suits... but I do make a lot of comments (which you may see one day if I need help again) :P

  4. #4
    Join Date
    Oct 2011
    Posts
    97

    Re: Random array element gets value changed with variable

    No problem. I'm not a huge fan of automatic memory management, so I tend to run into these issues a lot myself. I don't even really mind the automatic memory management, but they really need to explicitly differentiate between pointers and objects.

    Also, I was never a stickler for style either until I was given an orphaned project with a shitty design and no documentation to work on. Suddenly, it was like my eyes were opened to the importance of clean code and documentation. I'm sure the same will happen to you one day.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Random array element gets value changed with variable

    Quote Originally Posted by Access_Denied View Post
    No problem. I'm not a huge fan of automatic memory management, so I tend to run into these issues a lot myself. I don't even really mind the automatic memory management, but they really need to explicitly differentiate between pointers and objects.
    Ummm... they do. It's pretty clear and well documented. Here is one resource of many which explain how things work. It's not terribly complicated.

    http://www.codeproject.com/Articles/...lue-Types-in-C
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  6. #6
    Join Date
    Oct 2011
    Posts
    97

    Re: Random array element gets value changed with variable

    But you can use the same syntax for both. For example:

    Code:
    Item foo = new Item();
    Item foo2 = foo;
    
    foo2.method();
    In this case, you've changed the original foo item, not the copy you thought you changed. But in C++, it would look like this:

    Code:
    Item * foo = new Item();
    Item foo2 = *foo;
    
    foo2.method();
    Now you've actually copied data and changed the copied data, which is fairly easy to see because of the pointer syntax. I'm not saying that it's not just as good or can't perform the same functions, I'm just saying that it can get confusing. Especially for people who come from a C/C++ background where you can't just change between pointers to objects and objects.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Random array element gets value changed with variable

    You could say that about anything really. C# is not and is not intended to be C++. You'll probably have a hard time going from C++ to Lisp too, and perhaps a dynamic language like Ruby. They are different, it doesn't help to think of one in terms of another.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  8. #8
    Join Date
    Oct 2011
    Posts
    97

    Re: Random array element gets value changed with variable

    But when you're moving from one language to another, that's all you can do. When I moved to functional programming, the only thing I could do was compare it to procedural programming. Sure, now I know that you shouldn't compare the two, as they have different goals, but when first learning a new type of programming, it's all you can do. And that's exactly what people are going to do when MS finally convinces them to switch to C#: compare it to C++.

    EDIT: And this is all just a matter of opinion anyway. I personally think C# would be better if there was an explicit pointer type. But that's just my opinion. Not a fact.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Random array element gets value changed with variable

    Quote Originally Posted by Access_Denied View Post
    EDIT: And this is all just a matter of opinion anyway. I personally think C# would be better if there was an explicit pointer type. But that's just my opinion. Not a fact.
    If you have pointers then you can't make the guarantees about data integrity and memory management that you can otherwise. It would go against much of the original design of C#. Again, if you need/want pointers, use a different language....
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  10. #10
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Random array element gets value changed with variable

    You guys keep forgetting that C# has pointers and that it uses the exact same C++ syntax for them.
    Reference types are alike to pointers in some respect, but they are not pointers.
    Besides, it's relatively easy to distinguish them from value types, with most IDEs it only takes hovering the mouse over a variable.
    Besides from the "built-in" value types, most of the types in C# will be reference types anyway.

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Random array element gets value changed with variable

    Well yes, but there are strict rules as to when and how they may be used, so while the syntax is the same as C++, the semantics are not, and their use case is very narrow and rare in C#.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  12. #12
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Random array element gets value changed with variable

    I would say that it is rather the other way around: the reference types are semantically different from pointers (for example, no support for pointer argumetics, garbage collection), and thus, C# type system is conceptually different, which in turn imposes restrictions on pointers. But, I agree with you - it's a different language, and it should be treated as such.

  13. #13
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Random array element gets value changed with variable

    I agree with that
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  14. #14
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Random array element gets value changed with variable

    Quote Originally Posted by TheGreatCthulhu View Post
    (for example, no support for pointer argumetics [...])
    *arithmetics
    D4mn T9 dictionary.
    Missed the "h".
    Last edited by TheGreatCthulhu; May 10th, 2012 at 07:00 PM. Reason: because one should be able to say d4mn, d4mn it!

  15. #15
    Join Date
    May 2012
    Posts
    6

    Re: Random array element gets value changed with variable

    I'm not saying that it's not just as good or can't perform the same functions, I'm just saying that it can get confusing. Especially for people who come from a C/C++ background where you can't just change between pointers to objects and objects.
    That's definitely me

    I'm really thankful for the response so far, so I'm hoping someone among you might help me with a new problem.

    As it is, I create a new Item, copy the Item attributes in the constructor, and plunk it into List<Item> mapItems.

    But Item is essentially just supposed to be an abstract class (I think that's the correct term?), as all the items are derived types (Potions, Scrolls, Gold, Rings, etc). So In the process of creating a new Item, I lose all the methods and fields of the derived types. That's a really big problem...

    I've been scouring the help and looking at predicates, but I don't want to convert a base type object to a derived one... I just want to keep what's already there. Is there anything I can do?

    Sorry if this question is already answered elsewhere. I'd really appreciate any resources you could point me to

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured