How to store a value that will be manipulated by many Max objects?
Oct 13 2021 | 5:48 pm
In general I'm curious what the best practice is for storing a value that several Max objects will need to be able to access and manipulate.
My particular usecase: There will be many instances of an object in my patch. I want each instance to auto-register in the notification system using object_register(). This requires me to generate a unique name for each instance of the object. To do this, I believe I need a way to keep a running count of how many instances exist. Each new instance of the object needs access to that counter.
t_symbol* seems like an obvious place to start. Since that is a piece of memory that is accessible to all max objects.
Say I have something like: t_symbol* ObjectCount = gensym("1");
By casting ObjectCount->s_name from char* to int and back. I should be able to register my object with a name based on the current value of ObjectCount, then increment it, and then store the new value for the next instance to use.
The problem is that Object Count has to be initialized before it can be used. So before I can get the current value of the t_symbol I have to initialize the t_symbol. Otherwise I crash when trying to use ObjectCount because, within the object instance, I am using a variable that hasn't been initialized.
The result is that every time I create a new instance of the object I reset ObjectCount to it's initial value.
I'm still learning C++, so perhaps there's already a design pattern for this that I'm not familiar with. Or maybe t_symbol is the wrong direction in the first place? I thought I'd post here to see if anybody has any input as I continue with my own research. Please let me know if you have any thoughts.