Trying to Simplify the Addition of New Objects to a Game

I am thinking of restarting a game I was working on and I wanted it to be very easy to add new objects into the game without having to get into my code every time. In games with alot of tiles for the same kind of object how do people generally handle it? I was thinking if I had say a wall class, it would read from a txt file containing its name, location and other important attributes/rules.

For instance something like this for a water class.

water.txt


Name = "pond_tile"

Location = "forest" + "town"

Animate = "false"

Sprite = "pond_tile.png"
///////////////////////////////////////////////

Name = "river_tile"

Location = "forest" + "jungle" + "town" + "desert"

Animate = "true"

Sprite = "river_tile.png"

I think I want to get the core game out of the way and have just one type of tile for each class at the beginning. After the games how I want it I could pump out loads of tile types and biomes this way. Another benefit would be the ease of modding a game with a set up like this which I’d love to support. I was just thinking of ideas and kind of remember a game might have used something similar… minecraft? A couple questions I have are “Is there better ways to do this?”, “Any downsides?”, “Have any of you used something similar”, and “How did it work out for you?”. Pretty broad topic, maybe could have been in Game Play and Design but am looking forward to any input.

Your question is very broad but from what I’ve read I would advice you to use a map editor like tiled ( http://www.mapeditor.org ). There are already loaders and renderers for several libraries, like libgdx and slick2d. Like this you create your levels externally in the editor and simply load them in your game code. You need to change a level? Fire up tiled and fix it… tiled is also pretty generic, so it allows you to create multiple layers, create polygon objects, assign properties to layers, tiles, objects, which you can in turn read from your code.

Well… What you’re talking about sounds a lot like a class hierarchy/inheritance design issue.

In essence, creating a set of classes generic enough that they can be configured at runtime.

I think what will help you a lot is to read up on software design, and build a few UML class diagrams so you can wrap your head around how every piece fits together.

I’m doing something similar myself, and my process is as follows:

  • I create an specific class I need
  • I try to make it as configurable as possible via it’s constructor (say, if the class is a projectile, I pass it’s damage, speed and graphic throught the constructor)
  • When another class shares common aspects, I extract said aspects into a third class, and make those two classes extend the base class
  • Rinse and repeat

This method is quite error prone and convoluted, compared to doing a proper design document, but it is also more fun, as you’re testing components as you progress.

In any case, it’s not a simple task, and keep in mind that, with inheritance, errors in base classes can and will propagate to their children, resulting in some tough to crack bugs.

Always remember to log everything:


class CLASSY
{

  public void METHODICAL()
  {
      try{ ... }
      catch(Exception EXCEPTIONAL)
      {
         System.out.println(   "CLASSY::METHODICAL: Caught a " 
                                      + "[" + EXCEPTIONAL.getClass().getName()+ "]"
                                      + " with Message [ " + EXCEPTIONAL.getMessage()+ " ]");
         // throw some stack tracing if you prefer
      }
}

:slight_smile: