Game Development Community

Understand tVector.h (Vector)

by Demolishun · in Torque 3D Professional · 07/26/2012 (11:10 am) · 1 replies

I am trying to understand how to use the Vector class in the engine in tVector.h.

Here is how I am trying to use this:
// function call vars passed to exec
   // these are used by the opcode functions
   U32 *ip;
   const char **functionName; 
   Namespace **thisNamespace; 
   U32 *argc; 
   const char ***argv;
   bool *noCalls;
   StringTableEntry *packageName;
   S32 *setFrame;

   typedef struct ExecCallStackEntry{
      U32 ip;
      const char *functionName; 
      Namespace *thisNamespace; 
      U32 argc; 
      const char **argv;
      bool noCalls;
      StringTableEntry packageName;
      S32 setFrame;
      ExecCallStackEntry(){}
      ExecCallStackEntry(U32 _ip, const char *_functionName, Namespace *_thisNamespace, U32 _argc, const char **_argv, bool _noCalls, StringTableEntry _packageName, S32 _setFrame){
         ip=_ip; 
         functionName=_functionName;
         thisNamespace=_thisNamespace;
         argc=_argc;
         argv=_argv;
         noCalls=_noCalls;
         packageName=_packageName;
         setFrame=_setFrame;
      }
   };
   Vector<ExecCallStackEntry> ExecCallStack;

   void stackPush(ExecCallStackEntry *tmp){    
      ExecCallStack.push_front(*tmp);
      ip = &(tmp->ip);
      functionName = &(tmp->functionName);
      thisNamespace = &(tmp->thisNamespace);
      argc = &(tmp->argc);
      argv = &(tmp->argv);
      noCalls = &(tmp->noCalls);
      packageName = &(tmp->packageName);
      setFrame = &(tmp->setFrame);
   }

   void stackPop(){ 
      Con::printf("stack size: %d",ExecCallStack.size());
      ExecCallStack.pop_front();
      if(ExecCallStack.size() > 0){
         ExecCallStackEntry *tmp = &(ExecCallStack.front());
         ip = &(tmp->ip);
         functionName = &(tmp->functionName);
         thisNamespace = &(tmp->thisNamespace);
         argc = &(tmp->argc);
         argv = &(tmp->argv);
         noCalls = &(tmp->noCalls);
         packageName = &(tmp->packageName);
         setFrame = &(tmp->setFrame);
      }      
   }
This is in the CodeBlock class associated with the TS VM.

Also, I initialize the vector like this in the CodeBlock constructor:
ExecCallStackEntry *tmp = new ExecCallStackEntry(0, NULL, NULL, 0, NULL, 0, NULL, 0);
stackPush(tmp);

Now I use this in the CodeBlock::exec to capture when the input params to the function change.
At the beginning of the function:
ExecCallStackEntry *tmp = new ExecCallStackEntry(_ip, _functionName, _thisNamespace, _argc, _argv, _noCalls, _packageName, _setFrame);
   stackPush(tmp);
At the end (before the CodeBlock object destroys itself:
stackPop();

Oh, crap. Now I know what is happening. The CodeBlock object destroys itself. So all my variables need to be static. Great, I get to the end of asking how to make this work and solve it at the last sentence. Well, I am posting this anyway as a future note to self...

About the author

I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67


#1
07/26/2012 (11:28 am)
Ahh, found one big error.
This:
void stackPush(ExecCallStackEntry *tmp){      
      ExecCallStack.push_front(*tmp);  
      ip = &(tmp->ip);  
      functionName = &(tmp->functionName);  
      thisNamespace = &(tmp->thisNamespace);  
      argc = &(tmp->argc);  
      argv = &(tmp->argv);  
      noCalls = &(tmp->noCalls);  
      packageName = &(tmp->packageName);  
      setFrame = &(tmp->setFrame);  
   }
Should be:
void stackPush(ExecCallStackEntry *tmp){      
      ExecCallStack.push_front(*tmp); 

      tmp = &(ExecCallStack.front()); // points to memory in stack

      ip = &(tmp->ip);  
      functionName = &(tmp->functionName);  
      thisNamespace = &(tmp->thisNamespace);  
      argc = &(tmp->argc);  
      argv = &(tmp->argv);  
      noCalls = &(tmp->noCalls);  
      packageName = &(tmp->packageName);  
      setFrame = &(tmp->setFrame);  
   }