Is it OK to create lots of event listeners?
For context, I'm a beginner in C#, and I try to build a tile-based village simulator.
I have lots of tiles on my map (50 000+). Each tile is represented by a Tile object in my game's data (this is where I handle the simulation logic), as well as a GameObject for the display. What i want to do is to change my GameObject's sprite when the tile's data changed. So far I do it with events like follows.
In my simulation logic object, I create an event that triggers when the tile changes:
public event TileTypeChangedHandler TypeChanged;
TileType type;
public TileType Type {
set {
type = value;
TypeChanged (this);
}
}
Then when I create the tiles GameObjects, I register listeners:
// in a loop over all my tiles
tile.TypeChanged += new TileTypeChangedHandler (OnTileTypeChanged);
It works fine, but not having experience with C# I don't know if it's an efficient way to do these kind of things. How big is the overhead of creating all these events and listeners? Is there a better way to listen for changes in the data to update your display?
you should check the profiler for these kinds of tests. you won't find many users with that many gameobjects in a scene.
from what I can see you have a one to one relationship between logic object and gameobject. I would instantiate the gameobject along with the logic object and keep a reference to it. other then that, if another system is interested in changes, I'd create clusters of logic objects which have events and the logic object that changes as parameter.
but use the profiler really.