views:

604

answers:

6

I am programming a small game for my school assignment, the game is a simple 2D game with monsters, items and bullets. Basically you run around and tries to collect all the item coins, the monsters tries to prevent you and you can shoot them down with the bullets that you collect. Very simple.

The question is, i have added the monsters, items, walls, player and bullets to a static class named LiveObjects, these objects can i then access from any place in the code. Is it a bad practice? Whats the alternative? (It's not multithreaded)

LiveObjects.cs

internal static class LiveObjects
{        
    public static List<Item> items = new List<Item>(); // List with all the items
    public static List<Monster> monsters = new List<Monster>(); // List with all present monsters
    public static List<Wall> walls = new List<Wall>(); // List with the walls
    public static List<Bullet> bullets = new List<Bullet>(); // List with the bullets
    public static Player player = new Player(0, 0); // The player object
}

I use a lot of different classes to manipulate the data inside the LiveObjects and then to avoid passing an entire List, i can just call it directly inside any method.

+1  A: 

If your application is multithreaded you might need to think about the issues around multiple threads accessing and modifying static instances. Here's a good artical on threading:

http://www.yoda.arachsys.com/csharp/threads/

Charlie
It's not multithreaded
Patrick
A: 

One advantage of static classes is that it is the only place you can define extension methods.

Brian Rasmussen
A: 

I don't intend this as a serious answer but among the pro's, as you have discovered, is that it's very simple to get to your entities.

Among the con's is that some people hate static classes so much that they will not hire you because you've used them. http://thegrenade.blogspot.com/2009/01/static-methods-and-classes-are-always.html

grenade
A: 

The alternative is passing around a GameContext (or GameWorld, LiveObjects, you get the point) instance to all of the classes that need to know what is going on in the current game:

internal class GameContext
{
    /// <summary>Items in the world</summary>
    public List<Item> items = new List<Item>();
}

public class BigItem : GameItem
{
    internal GameContext Context;

    public BigItem(GameContext context, string name)
    {
        this.Context = context;
        // ...
    }
}
sixlettervariables
The problem is that several different methods in different classes may need access to the LiveObjects. I have one main file called Game.cs, this just initializes the game and uses a lot of different files for different purposes, like when the monsters move, i call the static monster class with the command AI.MoveMonsters(). Here i would have had to send the entire List with objects in order to preform this.
Patrick
Understood, basically this is just an alternative. I would suggest not exposing direct access to ANY of the lists. You could provide a static method on the GameContext that provided the Current context. From the current context you would invoke methods to access the lists and update the lists.
sixlettervariables
A: 

I think this usage of a static class is fine - especially in a single-threaded application. Like others have said, your only alternative is to pass around some sort of contextual object that contains these lists.

My personal opinion, in this instance, is that you have chosen the best solution for your problem. It is easy to implement, easy to use, and easy to change if need be down the road.

Andrew Hare
+2  A: 

Pros:

  • Easy access
  • Easy coding

Cons:

  • No thread safety
  • No encapsulation
  • Reduced maintainability
DanDan