CProcess: use processes as loops?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • CProcess: use processes as loops?

      i am using the Proces Manager included in the SourceCode 2.3. I'm trying to build a loop consisting of processes.


      The last process is linked to the first one:

      proc_0->SetNext(proc_1);
      proc_1->SetNext(proc_2);
      proc_2->SetNext(proc_0);

      If i run the programm, the code is processed only once.
      The proc_0 destructor isn't called after completion of proc_0. Instead it is called after proc_2 has finished !?


      it looks like this:

      proc_0 - contructor
      proc_0 - update
      proc_0 - kill

      proc_1 - contructor
      proc_1 - update
      proc_1 - kill
      proc_1 - destructor

      proc_2 - contructor
      proc_2 - update
      proc_2 - kill
      proc_2 - destructor

      proc_0 - destructor


      does the dependency between proc_2 and proc_0 avoid deconstruction of the process?
      Am i missing something or do i have to choose another way to loop processes?
    • RE: CProcess: use processes as loops?

      Originally posted by blub
      does the dependency between proc_2 and proc_0 avoid deconstruction of the process?
      Am i missing something or do i have to choose another way to loop processes?


      Yes. The next process is stored as a shared_ptr. Shared_ptr only deletes/calls the destructor on an object when it's use_count decrements to 0.

      The first process "proc_0" has two shared_ptr's to it. One is held by CProcessManager, and the other by proc_2. When it is "killed" the process manager (eventually) removes it and the count decrements to 1. When proc_2 is both "killed" and destroyed, proc_0's count becomes 0, and it is then destroyed. You cannot, as you have found out, loop processes by setting circular references (which is generally unwise when using shared_ptr's).

      I do have an idea on how you might accomplish this. I haven't tested this myself, but it should work. You are going to have to create a new derived class and override the SetNext() and Kill() functions. (Note: You can derive from any class that derived, at some point, from CProcess). Below is basically what these functions should look like. In Kill(), when creating a new LoopingProcess, make sure you set up the new process correctly (which depends entirely on what you want).

      Source Code

      1. class LoopingProcess : public CProcess
      2. {
      3. virtual void SetNext(shared_ptr<CProcess> nnext)
      4. {
      5. if (m_pNext)
      6. {
      7. m_pNext->SetNext(nnext);
      8. }
      9. else
      10. {
      11. m_pNext = nnext;
      12. }
      13. }
      14. virtual void Kill()
      15. {
      16. if (m_pNext)
      17. {
      18. shared_ptr<CProcess> newSelf = shared_ptr<CProcess>(new LoopingProcess(....));
      19. //Do whatever you need to make sure that newSelf is set up like the
      20. // current process (or however you need it)
      21. m_pNext->SetNext(newSelf);
      22. }
      23. CProcess::Kill();
      24. }
      25. };
      Display All



      This works because when a LoopingProcess is killed, it adds another LoopingProcess, matching it self, to the end of the "loop". All of the processes in this list or "loop" will need to be a LoopingProcess (or will need to derive from it). Again, I haven't tested this.
    • yes your code works fine. thank you for your reply.

      now I got stuck again.

      you might have noticed that I am new to game programming. so I have to make little steps...

      I am wondering if it is possible to make a dicision inside a process, to choose which process will be next.

      at the moment I am setting up all processes on game init and also set their following processes (SetNext).


      thx.
    • Originally posted by blub
      yes your code works fine. thank you for your reply.

      now I got stuck again.

      you might have noticed that I am new to game programming. so I have to make little steps...

      I am wondering if it is possible to make a dicision inside a process, to choose which process will be next.

      at the moment I am setting up all processes on game init and also set their following processes (SetNext).


      thx.


      No problem, happy to help.

      Yes, you can. You will, of course, need to store all the shared_ptr's to all the processes which could, potentially, be the next. If you want a variable number of possible choices, use one of the std containers.

      All you would then need to do is modify the OnUpdate() member function so that the process currently designated as "next" is also pointed to by m_pNext. That way, when the process manager calls GetNext(), it will select the correct process to attach.

      You could also modify GetNext() directly (you would need to make it virtual in CProcess.h first, but I recommend you do the first option).

      Make note though, any processes held by your "DecisionProcess" that are not selected, will be destroyed when your process is destroyed (unless something else also holds them). If you want them to stay around in someway, you will need to do something to ensure that.