3 Replies - 1072 Views - Last Post: 14 August 2011 - 02:42 PM Rate Topic: -----

#1 nunnbt473  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 14-August 11

Accessing object properties inside of an object array?

Posted 14 August 2011 - 09:31 AM

I am trying to write a simple game program, but lack a certain knowledge of objects in general. Here is what I am trying to do..

I have a person in the game, a character with stats. Ex: Hit Points, Strength..etc. Of course I will need multiple copies of this character for each person and each persons stats will grow independently of each other. The stats will likely be a mix of bytes / ints which will later be parsed if needed (hopefully in an effort to save memory instead of making them all ints) and the name will of course be a string.

So the problem I face is either A) How can I dynamically name these new objects at runtime so they can be manipulated later in real time or B) Can I make multiple instances/objects of the same class with an identical name if I place them in a unique location (like inside of an array / arrayList for objects)? and if so how would I access the object inside of the array?

Example:

Class characters

//An array to hold 100 players
object[] array1 = new object[100];

//HP..etc might extend into the 10,000+
int hp = 100;
int end = 100;
int mana = 100;

byte str = 10;
byte agil = 12;
..etc


characters players = new characters;

array1[0] = players(); ????


...or...

array1[0] = characters players = new characters; ????


However I still dont know how to access an object once Ive placed it inside of the array. Any help would be greatly appreciated and help me understand objects and arrays quite a bit.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing object properties inside of an object array?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: Accessing object properties inside of an object array?

Posted 14 August 2011 - 09:36 AM

First - you should not be deciding in advance how many players you are allocating space for. This should be dynamically adjusting as you add players. An object[] is therefore a bad choice. Try a List<>

List<player> myPlayers = new List<player>;


Then you can loop through the player list and access each player by index


myPlayers[5].HealthPoints += 10;// Give player 6 10 more points of health







Just an observation. If you had gone through a "Learn C# in 21 days" type of self-teaching book you would have picked this up. So I am guessing you are trying to teach yourself without first going through a book or tutorial: Just jumping in and trying to design and build a program right off the bat. That really is the hard way to go.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

We don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Aug 2011
Spoiler

This post has been edited by tlhIn`toq: 14 August 2011 - 09:37 AM

Was This Post Helpful? 2
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Accessing object properties inside of an object array?

Posted 14 August 2011 - 10:29 AM

You should be using a generic list as shown above. Don't use an ArrayList, and don't use a array of Object type (if you were to use an array, it would be of 'characters' type (or whatever type is holding the data for each character/player)). You'll just make life more difficult for yourself, and you'll sacrifice some of the benefits of strong typing.

Note that Object is just a class. You don't want your array to hold Object instances, you want it to hold your character/player instances. Don't confuse terminology here.

Object (and the alias 'object' used in your code) is a physical class.

Instances of any class are often called objects of the class. The two are unrelated terms.

Every class derives from the Object class...


Also,

Quote

...(hopefully in an effort to save memory instead of making them all ints)


I really wouldn't concern yourself with that. The only real time you need to even consider doing that sort of thing in C# is if your application is going to be used in a particularly low memory context, or perhaps when dealing with ginormous amounts of data.

In C#, you should generally just use the data type that logically fits with what your trying to achieve in your program (it is a high level language, after all!), and worry about memory issues if they become a problem. Even then though, it is likely that something else is causing the memory issue, rather than your choice of datatype :)

In general development, it'll just cause you unnecessary problems, and potentially even impact on the overall efficiency of your program.


Just to give you something to think about, if I used the 'sizeof' operator to get the size (in bytes) of this struct, what would the returned size be?

struct TheVeryHungryStruct {
     byte b;
     int i;
}



Note: I've used a byte type where I can in order to save 3 bytes...

Spoiler


Also, think about this, each thread's stack is typically allocated memory of 1MB, and that is really just for method parameters and local variables. You can fit over 262000 int variables in that, and all variables and parameters are popped off the stack (thus freeing the memory) after each method returns anyway! Plus, I'm not sure if the stack doesn't work in double word (32 bit) blocks anyway...(I may have just made that bit up...) (Admittedly, the pointers associated with the prologue and epilogue code for each method needs to be allocated in that memory also, but you get the idea).

As for heap memory, that will simply grow as necessary up to the limits of your PC and/or architecture (we're probably talking Gigabytes maximum size), and if your using your byte variables as instance fields in a class, heap memory is largely what you are concerned with.

That puts things into some sort of perspective I hope :).

This post has been edited by CodingSup3rnatur@l-360: 14 August 2011 - 02:53 PM

Was This Post Helpful? 3
  • +
  • -

#4 nunnbt473  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 14-August 11

Re: Accessing object properties inside of an object array?

Posted 14 August 2011 - 02:42 PM

First and foremost thank you for the informative and quick responses. Namely giving both example code with the reasoning behind has been quite informative. Its true I do have a bit of a swiss cheese approach, regrettably. However formal education in such things is a bit difficult since I am having troubles receiving aid for schooling and am out of work at the moment. I most certainly wouldn't take on any programming job of consequence without being quite confident in my skills and would recommend someone to a more experienced coder.

With that being said, I have actually had an introductory course in Java & VB.net and my Adv CIS Honors course in HS had some VB6. Sadly, that course would have been much better served with a more in depth look at one language than it would have two different languages. So for the time being I am left to discover things on my own as time and charity permits. I have tried looking through nearly a dozen tutorials, but none had an example of how to access an object inside of an array.

As for limiting the number of players/characters to 100 was merely an off the cuff example. I would likely start with a 250 limit simply for testing purposes, once I prepared it to a larger domain I would undoubtedly expand that to a much larger scale. Of course it then depends on other factors like how how/low end of a computer I design for as well as internet limitations..etc. I wasnt aware of generic lists yet, clearly I need more study of generics and enums.

I plan on designing this game in stages over time as I learn, begin simple and work my way up from there. Though I admit I was quite unaware of the 1MB stack/thread entirely and didn't stop to really consider how many ints I could effectively use. This was something that previous teachers and online tutorials alike neglected to mention.

As for becoming frusterated, well I tend to thrive on challenges, especially when I know the answer is lurking somewhere in some tutorial or API. Ive become quite accustomed to googling many a thing in my life time. Patience I have, I just found myself lacking the intelligence to point me to where I need to go this time round. This is undoubtedly a large and pain staking process ahead.. but I have no other deadline than what I give myself. It will be as much of a learning process as it is a result.

Again, thank you both for being both informative and patient. It is truly amazing how many coders I encounter which are neither.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1