Hi everyone! Not sure the best way to ask this question within the context of my use-case while also keeping the code to a minimum, but I will try my best!
The Question:
Is there a way to reset each instance of a member variable without me as the programmer needing to remember every instance created?
Description
Let's say I have a variety of functions my robot will perform, such as walk, jump, shake hands, etc. Each function has a series of steps:
For example,
void jump()
{
//Step 1: bend your legs so you're squatting. Once you're squatting, move to step 2
//Step 2: quickly unbend your legs so you leap into the air, then move to step 3
//Step 3: bend your legs again so that you have a gentle landing, the move to step 4
//Step 4: slowly unbend your legs so you don't jump, but instead merely reset back to your original stance
}
Maybe not the greatest example, but the point is that I feel the need to have distinct "steps" within the function, because multiple motors and sensors may all need to perform tasks within a single step. jump() needs to be repeatedly called over and over again in the loop() function so that it can perform all the tasks in step 1, check that it's completed them, and then move to step 2, and so on.
So, since each of my functions needs to keep track of which "step" it is in, I'm thinking of creating a class called StepManager, allowing each function to have its own instance of "int step" to help manage which step it should perform.
Let's say I use a remote control to perform each of these commands (walk, jump, etc.), and have a master reset button.
The goal is that if the master reset button is pressed, the "int step" instance within each function needs to reset to 0. That way, each function will begin at step == 0 the next time it is called.
It seems that in order to reset each instance of "step", I might need to create a vector/container/list that keeps track of every instance created, but this seems unclean to me since all instances will be known during compiling. I feel this way because the only places where these instances exist are in the functions I pre-define, such as walk(), jump(), etc.
Is there another, possibly easier and/or better way to reset the "step" within each function?
Abstract Code example with a bit more context
class StepManager //a class that allows each function to have its own instance of "step"
{
public:
int step = 0;
static void resetEveryInstanceOfStep()
{
//This is the question: is there a way to reset each instance of "int step" without me as the programmer needing to remember every instance created?
}
};
void walk()
{
static StepManager walkStep; //this static object will keep track of which "step" the function is in, even while the function is called over and over again
if (walkStep.step == 0)
{
//do some stuff here
function1step.step++;
}
else if (walkStep.step == 1)
{
//do some other stuff here
walkStep.step++;
}
else if (walkStep.step == 2)
{
//do something different here
walkStep.step++;
}
}
void jump()
{
static StepManager jumpStep; //this static object will keep track of which "step" the function is in, even while the function is called over and over again
if (jumpStep.step == 0)
{
//do some stuff here
jumpStep.step++;
}
else if (jumpStep.step == 1)
{
//do some other stuff here
jumpStep.step++;
}
else if (jumpStep.step == 2)
{
//do something different here
jumpStep.step++;
}
}
void setup()
{
}
void loop()
{
if (userIsHoldingDownButton1() == true) //while the user is holding down this button, run function1()
{
function1(); //continuously call function1(), which will cycle through each of the steps within function1()
}
else if (userIsHoldingDownButton2() == true)
{
function2(); //continuously call function2(), which will cycle through each of the steps within function2()
}
//imagine I have many more buttons, each which corresponds to its own function that contains many "steps"
if(masterResetButtonIsPressed() == true)
{
//StepManager::resetEveryInstanceOfStep();
//This is the question: is there a way to reset each instance of "int step"
// without me as the programmer needing to remember every instance created?
}
}
Any help and advice is appreciated!