Hey guys :)

I have been working on an MMORPG for the sole purpose of education recently because i want to start computer science at University in a couple of years and be really prepared for it (i'm only 17 this year)

So far i have basically all the networking sorted and am working on an objects system -- i want to make my own which goes like this:

There is an array for every field in an object
The index of the array (for example array[index]=x) is equivelant to the user index ID

I have some functions which go like this:

storeObject(name of the array to store in, data to store, user index);
readObject(name of the array, user index);
newObject(name of new array to be created, max values);

As you can see all functions above require me to use the parameter (which is a String) to call the array - the problem is just how do i do this?

consider this (i know it's completely wrong, i need major help)

storeObject(String na, String da1, int uidx) {
na[uidx] = da1;
}

How would i do that? To be more precise, how would i use the value of the string na be the name of the array which we are going to store in, and use it like above?

Thank you so much for your assistance! Every piece of advice is appreciated. If you have any critisism on my object system and would recommend an easier or more efficient method for use in threads and multiple classes on a server and client in an MMORPG please let me know :)

-Ollie

A more feasible way to decide which array to store it in would simply be to use if statements to decide. So

if (myString.equals("arrayName")){
arrayName[someIndex] = someData;
}

Other than that, you could look at the Java Class called "Class" and use some of those methods. Take a look in the "Object" class, it has a method called "getClass" that returns a Class Object. This method might be helpful. I'll warn you though that I don't personally mess around with "Class" so I can't guarantee results. But I recommend avoiding its use if at all possible.

Agreed, class Class is messy business -- you need to be sure you know what you are doing. Personally I like to use the equalsIgnoreCase() method instead of the equals() method for strings simply because the user might enter "Yes" or "yes" and you will want to evaluate such answers the same way.

Sorry i don't really understand about Class, would you please be able to provide a quick reference to where i can use a variable/array which has the name as the value of another variable?

thanks :D

This is how I think it works. Like I said before I've never used it. This is simply from reading the Javadocs I looked up on google.

public class RPG{
double variable1;
String variable2;

public static void main(String[] args){ 
RPG myGame = new RPG();
Class myClass = RPG.getClass();
Field myField = myClass.getDeclaredField(variable2);
myField.set(myGame, new Double(10.9));
}

}

And one last time I will warn you against this: I'm almost finished my cmsc degree and I've never encountered a reason to use something like that in Java. None of my teachers have even mentioned it. Maybe some other posters who have worked in industry will disagree, but I see it as a tool that is made available for important, but rare uses. Since you already know what all of your possible variable names are (they are defined in advance, Java is a strongly typed language!) you can just use if statements which are far more clear and almost certainly more efficient.

commented: Thanks a lot for all your help! +1

Thanks a lot for all your help, i'll probably use the if statements you suggested and build on your example if i can - i'll post here again if i get a successful method for others to see :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.