简体   繁体   中英

Static - Good or Bad?

Alright, so I've been trying to figure out this for far too long now.

I've read countless articles/questions on static , and also singletons.

This is how I have been using static for quite some time:

I have a GameManager.cs, which has TileMap.cs & Player.cs. TileMap.cs needs to access the player's camera, for updating the position of the map:

GameManager.cs:

public static TileMap map;
public static Player player;

Update code, draw, etc. below...

There will only be one of each of these.

TileMap.cs:

Vector2 mapPosition = new Vector2(GameManager.Player.position.X, GameManager.Player.position.Y);

Is this acceptable? What downsides are there to this?

Edit: Seeing as though this question is too broad, let me see if I can be more specific.

Is there a better way to do this rather than through static methods? I have multiple lists of classes inside TileMap.cs, some of which have Lists inside of them (thinking mostly of my particle engine), so would Update(Player player) be more efficient, or would it not really matter?

PS, I have noticed when the player moves the game map sort of "jitters" (lags for a small fraction of a second). Could this be causing it?

Thanks,
Shyy

As you are asking about downsides:

1) does the code that uses static variables involved in muti threading ? If yes, you may consider locking management. Which easily makes apparently easy code complicated.

2) Are Player , position and others structures ? If so, every time accessing them via property, you create a copy of instances and not access to the references directly. Considering that code provided some 2D engine, and you are creating a Vector , so probably some rendering pipeline code, this may introduce some serious performance implications.

I've noticed similar questions quite often professionally. So let me give you a straight answer, of which I am well aware that it doens't apply to the general case. See it as an opinion of what I consider 'best practice for beginners'.

static is often used for making variables available across class boundaries, as-if they are singletons. The singleton pattern here is just a design pattern wrapper (which doesn't solve most of the problems). While this might make programs easier to write, using static can also make programs much more complex if you want to make your application multi-threaded.

In general I therefore think it's a good idea to avoid using static alltogether and simply pass objects around.

There is one exception, that is: if you need to have data that is accessible across thread boundaries. If this is the case, you'll quickly find yourself in a world of hurt, and it is best to learn as much as you can about locking and use that for all static variables (including their members if they are structures/classes!).

If that isn't good enough as well, you can continue on that path and learn about things like interlocked and memory barriers (but I wouldn't recommend that if you don't need it).

Fortunately, most applications are fast enough to work in a single thread. I imagine your application to be no exception (and you can probably use a standard game framework to do the multi-threaded part of the application -- PS: if so, be careful with class variables as well, since they might be passed across thread boundaries as well).

As for your lag: I think you should use a good profiler to find performance hotspots; it's probably unrelated to the use of static .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM