Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I have a game where there are several predefined constants and charts (a XP chart, cost of goods and so on). Those could be defined at runtime, or load from files at start-up. The question is how should those logic routines access the constants and charts?

For example, I could try using global variables, but that cause all classes relying on the variables to be tightly coupled with them.

share|improve this question
up vote 3 down vote accepted

It really depends upon what type (and quantity/size) of data you're talking about. If it is a small bit of data that can be safely kept in memory for the duration of execution, something like a static class would be my recommendation.

public static class Constants
    public static int[] Levels = { 0, 100, 200, 400, 800, 1600, 3200, 6400 }

This way, the experience required to get to Level X is Constatns.Levels[X - 1]; and accessing other data is easy as well, if you need a collection of Items, you can add it here too, if your Items data is too large to store in memory, you can also implement caching logic within the static class.

Making it static keeps a single copy in memory, and ensures that every client of this class is getting the same data.

While this does "tightly couple" your classes together, tight coupling is not a bad thing if the design really needs it, which in the case of Item stats and XP charts, it is, IMHO, reasonable to tightly couple these. I always try to keep it simple, until something forces it to be complicated.

share|improve this answer

Create an object to represent each piece and pass that object in to functions that need it. This is no different to any other data- you pass it around.

share|improve this answer

I recommend using one globally accessible class - lets call it Dictionary (using flash terminology). The Dictionary is basically a list of name-value pairs (data, rules, constants) you want accessible throughout your application. Using the name (string) other classes will be able to retrieve the values (numbers, string, classes etc) which are interfaced into your code. So your implementation is basically IOC except the instantiation has been previously done in the Dictionary. You can also listen to changes to the Dictionary, etc.

share|improve this answer
This approach is good for debugging - you can pipe the dictionary to a http server and edit the numbers on the fly from another app, or print the values on the screen, etc. – tenpn Mar 18 '11 at 11:07

Well if you were using Java then I would reccomend putting your data in XML files and using something like JAXB or simple-xml in order to get the data that you want in the right objects.

share|improve this answer
If I'm not completely wrong, the question is not about how to store and load the data, but rather how to provide access to it once it's loaded (software design, OOP). – bummzack Mar 15 '11 at 9:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.