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

Dani AI

Generated

Java doesn't let a runtime String magically become a variable name. Variable names are a compile-time concept. For this task the pragmatic choices are (a) map names to real objects you manage yourself, or (b) use reflection to look up class fields — which works but is brittle and slow for game state. As warned, reflection is a special-purpose tool; prefer a simple map or an OO design for an MMORPG. To avoid the typo/case problems hinted at, use constants or enums for keys instead of free-form strings.

A clean, thread-friendly approach is to store your per-field arrays in a concurrent map keyed by name, and use atomic arrays (or per-player objects) for element-level updates. Example:

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

public class NamedArrays {
  private final ConcurrentHashMap<String, AtomicReferenceArray<String>> store = new ConcurrentHashMap<>();

  public void newArray(String name, int max) {
    store.putIfAbsent(name, new AtomicReferenceArray<>(max));
  }

  public void storeValue(String name, String value, int uid) {
    AtomicReferenceArray<String> arr = store.get(name);
    if (arr == null) throw new IllegalArgumentException("Unknown array: " + name);
    arr.set(uid, value);
  }

  public String readValue(String name, int uid) {
    AtomicReferenceArray<String> arr = store.get(name);
    return arr == null ? null : arr.get(uid);
  }
}

Notes and cautions:

  • ConcurrentHashMap + AtomicReferenceArray gives safe concurrent access for independent element reads/writes. If you need atomic updates across multiple fields for the same player, add a per-player lock or use a Player object with synchronized methods.
  • Consider an OO model: an array/map of Player objects indexed by userId is usually easier to reason about than lots of parallel arrays.
  • Use enums/constants for field names to prevent runtime typos and to make refactors safe.
  • Think about persistence and serialization early (saving player state, syncing client/server).

These patterns scale better than long if/else chains and are easier to maintain as your project grows.

Recommended Answers

All 5 Replies

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.