Flood of "Change" Events

Started by
2 comments, last by EWClay 11 years, 1 month ago

I am building an editor of sorts, which means my mesh geometry can change. Certain parts of my application will want to be notified when a mesh's geometry changes. So I added a "GeometryChanged" event to my mesh class. The problem is that several of my Mesh API functions can raise the GeometryChanged event, and often these calls will be made sequentially. Here is a simple example:

mesh.AddVertices(...);

mesh.AddIndices(...);

mesh.Indices[3] = 2;

Each of these changes the geometry of the mesh, and would trigger 3 "GeometryChanged" events. If the event handlers were heavy duty this would be pretty wasteful. One solution I am considering is to wrap changes between a BeginGeometryChange()/EndGeometryChange pair, and only Begin/EndGeometryChange would raise events.

This would work and is relatively easy to integrate, but I am wondering if there is a more elegant solution. I find this somewhat inelegant because of the extra API calls to Begin/EndGeometryChange that the client must manually remember to call. I can of course add debug asserts to make sure they are properly called, but still.

-----Quat
Advertisement

There is nothing wrong or inelegant with your idea to begin/end a series of events. This is called “batching” and is the normal way to go about this. It is also heavily used in undo/redo systems, especially related to typing events.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Another approach is to wrap the begin/end calls in a scoped handle class, which calls begin when it is created, and end when it is destructed (i.e. falls out of scope)
class Mesh
{
	 friend class MeshModifier;
private:
	 void begin_modifications();
	 void end_modifications();

	 void add_vertices_impl();
	 void add_indices_impl();
};

class MeshModifier
{
	 Mesh &mMesh;
public:
	 MeshModifier(Mesh &mesh) : mMesh(mesh) {mMesh.begin_modifications();}
	 ~MeshModifier() {mMesh.end_modifications();}

	 void add_vertices();
	 void add_indices();
};

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Do you need events at all? Can you flag the mesh as changed, and defer processing the change until a fixed point in the update?

This topic is closed to new replies.

Advertisement