How to execute a method in my own class every frame?

So i have class “A” and i have list “L” in there which i’d like to empty with its private method “ClearList” at the beginning of every frame.

public class A
{
	public List<T> L
	
	public A() { }

	private void ClearList()
	{
		this.L.Clear();
	}
}

I don’t really like to do it inside the plugin because when i hand the object “A” over to another plugin while the source plugin looses it then the “ClearList” method won’t run every frame anymore.

now what i found was IMainLoop described here: idxdeviceservice-and-imainloop so i ended up with this:

public class A : IPartImportsSatisfiedNotification
{
	[Import](Import)
	IHDEHost FHDEHost;

	public List<T> L
	
	public A() { }

	public void OnImportsSatisfied()
	{
		FHDEHost.MainLoop.OnPrepareGraph += ClearList;
	}

	private void ClearList(object sender, EventArgs e)
	{
		this.L.Clear();
	}
}

but: I’ve put a breakpoint at the “FHDEHost.MainLoop.OnPrepareGraph += ClearList;” part and at the “this.L.Clear();” part in visual studio. It stopped properly when i was subscribing to the mainloop event but it never stopped on “this.L.Clear();” and apparently “ClearList” was never executed.
What am I doing wrong? Thanks for any help!

hi microdee,

i made a quick test in a plugin class as elias was describing it in his post. it worked very well. the PrepareGraph is the right event, it happens shortly before the evaluate.

then i moved the code over to a new class as you posted it and it was not working. but not even the OnImportSatisfied was called. so i was not able to subscribe.

but if that works for you, check in the debugger whether the FHDEHost has a valid value and that the list L is not null. then i see no reason why it shouldn’t work…

Hm hard to guess what’s going wrong. Can you put a little example project together showing what exactly you’re trying to do and upload it as a zip file? The whole import stuff should only work on plugins, not on some custom classes - that’s why it is a little unclear to me why your subscribe method is even called.

@Elias: Ah i see i didn’t know it shouldn’t work on custom classes. In that case is there a way to subscribe to Mainloop events in vvvv (preferably PrepareGraph) in classes/objects which are not plugins? still I made you an example project.

@tonfilm: that’s just an example but the collection in my real case is constructed when my class is constructed. Yep FHDEHost is null :(

here’s how the example class looks like now:

public class CustomObject : IPartImportsSatisfiedNotification
    {
        [Import](Import)
        public IHDEHost FHDEHost;

        // simple fields which supposed to increment on every frame at different stages
        public int PrepareGraphIncrement = 0;
        public int PresentIncrement = 0;

        public CustomObject() { }

        // debug doesn't even stop here
        public void OnImportsSatisfied()
        {
            FHDEHost.MainLoop.OnPrepareGraph += OnPrepareGraph;
            FHDEHost.MainLoop.OnPresent += OnPresent;
        }

        private void OnPresent(object sender, EventArgs e)
        {
            this.PresentIncrement++;
        }

        private void OnPrepareGraph(object sender, EventArgs e)
        {
            this.PrepareGraphIncrement++;
        }
    }

only some int fields should be incremented on the events.

CustomObjectMainloopEventExample.7z (8.3 MB)

since the composition framework does not work on custom classes, you have to create the class from a plugin and pass the IHDEHost over in the constructor. then you can subscribe and do the work in the event handler.

IHDEHost FHDEHost;
    public A(IHDEHost host) 
    { 
        FHDEHost = host;
        FHDEHost.MainLoop.OnPrepareGraph += ClearList;
    }

yep it works! amazing! big thanks for the help!
just for the record it works when the class looks something similar:

public class CustomObject
    {
        public IHDEHost HDEHost;

        // simple fields which supposed to increment on every frame at different stages
        public int PrepareGraphIncrement = 0;
        public int PresentIncrement = 0;

        public CustomObject(IHDEHost hdehost)
        {
            this.HDEHost = hdehost;
            this.HDEHost.MainLoop.OnPrepareGraph += this.OnPrepareGraph;
            this.HDEHost.MainLoop.OnPresent += this.OnPresent;
        }

        private void OnPresent(object sender, EventArgs e)
        {
            this.PresentIncrement++;
        }

        private void OnPrepareGraph(object sender, EventArgs e)
        {
            this.PrepareGraphIncrement++;
        }
    }

And don’t forget to unsubscribe if you’re done using the event.