Jump to content

New to C#_Understanding how to access and name objects

- - - - -

  • Please log in to reply
2 replies to this topic

#1
nunnbt473

nunnbt473

    Newbie

  • Members
  • Pip
  • 5 posts
I'll prelude this with that I am new to both the forums and the C# language, though I have taken an introductory class in Java and VB.net and VB6 a few years back. However, I am having some difficulty with the fundamentals of objects/classes. I am working on a simple game, an RPG.

First off I have characters, ex: a person which would have stats (Hit Points, Strength, Agility...etc).

I know that almost everything in C# is an object. if I use a command like..

public class monsters
{
byte hp = 100;
byte str = 10;

monster customName = new Name will create a new instance of that class as an object called customName, yes? I would set a parameter like..
customName.hp = 120; yes?

So this is what is getting me, say I am making players. How do I tell the objects apart as I create them? If I want player1 and player2 to be seperate objects, each with their own stats (parameters).. how do I set some kind of loop to create and access these names?

Is it possible to do..

command line get user input.. a name..

string x = getLine.. blah blah.

monster x = new monster; ??

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
You need to learn about arrays. I suggest you either get a good book on C# or work through these lessons

#3
nunnbt473

nunnbt473

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for pointing me in the right direction and a quick reply. Before I was (I think the proper term is) trying to dynamically name my objects. But I see if I put them in an array, the scope of the objects allows me to make identical objects of the same name, yes? And I would track them by referring to the array placement? Hm...

I also see that arrayList falls under an iList Interface, which is designed to access objects by the array index.. which is exactly what I wanted to do initially.


If database != null then new object...

databaseInfo copy to object 1 property at a time while its inside of an array..?


And in order to access the object inside of the array I would.. hm? I could store the entire array as a string and parse it later or store it as an object and change the properties directly?


array[index] = object.property(variable)?? It doesn't seem like Ive quite got that figure out yet.

So I started looking at an arrayList. I notice that it automatically expands as needed so I wouldn't need to adjust it for every character logged in. I also see that SyncRoot and Synchronized have some way of accessing the arrayList in which multiple instances? of the same array can be open and still synced. (Ergo different players could have their stats updated in realtime inside of the same arrayList).


Or perhaps I could use hashtables and connector a character ID with the first half of the hash and the second half would be a value. With a series of these hash tables I could store all the information almost as efficiently, but at a cost of memory... I guess for that matter I could just use a few different arrays. Perhaps multi-dimensional arrays to store the information as variables?

Ive searched several different websites / tutorials, but I havnt come across anything resembling exactly what I am trying to do. I know I can place an object into an array or arrayList, but I still lack the means to access that object and update various elements in that array in real time.

Perhaps a 2D array list to store the basic information of the character accompanied by a 1D arrayList for the name?

arrayList characterStats 10x10
     HP     end     mp        str      agil   ...etc

Char 1      19     234     23         10      11 

char 2      12     21       42         34      55


     column 1         2          3          4       5

row 1        1,1      1,2        1,3      1,4    1,5

row 2        2,1      2,2        3,2      4,2    5,2


characterName


name1

name2

name3..etc

Or would it be more effective to read/write the database (not yet created but will be) directly each time a change occurs or once a day..etc?

Or would it make the most sense to store it all as a string and parse the information as needed? While most items (like str/agil..etc) wouldnt change often (few times an hour), things like hp/end/mana would change frequently (few times a minute).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users