Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    passing variable to plugin

    SDK Help
    0
    12
    874
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      On 12/10/2013 at 01:04, xxxxxxxx wrote:

      User Information:
      Cinema 4D Version:   r14 
      Platform:      
      Language(s) :     C++  ;

      ---------
      Hi, 
      I have been searching and trying for a while, now, but I can't get it to work to pass a variable from main (PluginStart) to my plugin, where I would like to read the value of that variable.

      can someone explain how things like this work?

      cheers,
      Ello

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 12/10/2013 at 03:09, xxxxxxxx wrote:

        Hi, you could go with a global variable for PODs (int, float, char, ...) and use it wherever you want in your entire plugin.

        Int my_global_variable = 0; // allowed
          
        String my_global_string; // (* ) not allowed because internal pointer table to the constructor is not build yet
        String* my_global_string = nullptr; // correct!  
        Bool PluginStart()
        {
            my_global_string = NewObj(String);
            if (!my_global_string)
                return false;
          
            // your code
          
            return true;
        }
          
        void PluginEnd()
        {
            DeleteMem(my_global_string);
        }
        

        (* ) If you want to use a c4d class you can use a global pointer like String* and allocate it in PluginStart and Free it in PluginEnd.

        Or you store some values in the global data container.

        Cheers, Seb

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 12/10/2013 at 04:58, xxxxxxxx wrote:

          i guess i dont't understand. in my main.cpp i have declared a boolean:
          Bool test = false;

          now i want to check this boolean inside another file (myPlugin.cpp)
          BaseObject *myPlugin::GetVirtualObjects(BaseObject *op, HierarchyHelp *hh)
          {
          if (test) //do something
          ...
          }

          of course test is undeclared here. so my question is how can i grab the variable test inside GetVirtualObjects

          1 Reply Last reply Reply Quote 0
          • H
            Helper
            last edited by

            On 12/10/2013 at 07:48, xxxxxxxx wrote:

            You can use the extern keyword. See How do I share a variable between source files in C?

            Best,
            -Niklas

            1 Reply Last reply Reply Quote 0
            • H
              Helper
              last edited by

              On 12/10/2013 at 08:56, xxxxxxxx wrote:

              thank you. i tried this but still i must do something wrong.

              in the main.cpp i have #include "myPlugin.h"

              and in myPlugin.h i have

              #ifndef _myPlugin_H_
              #define _myPlugin_H_

              extern int myTest;

              enum
              {
               ...

              when i try to compile i get the following error:

              1>  Main.cpp
              1>  myPlugin.cpp
              1>     Creating library C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\obj\myPlugin\x64_Debug\myPlugin.cdl64.lib and object C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\obj\myPlugin\x64_Debug\myPlugin.cdl64.exp
              1>Main.obj : error LNK2001: unresolved external symbol "int myTest" (?isRegistered@@3HA)
              1>myPlugin.obj : error LNK2001: unresolved external symbol "int myTest" (?isRegistered@@3HA)
              1>C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\myPlugin.cdl64 : fatal error LNK1120: 1 unresolved externals

              i found this thread http://stackoverflow.com/questions/7004031/c-unresolved-externals but i dont understand what i need to do now...

              1 Reply Last reply Reply Quote 0
              • H
                Helper
                last edited by

                On 12/10/2013 at 09:08, xxxxxxxx wrote:

                Howdy,

                Well, in everything I've read about c++ programming, there seems to be a consensus that global variables are frowned upon.

                What I'd suggest is to create a static variable in your main.cpp file, and create accessor functions like this:

                #include "MyPlugin.h"
                  
                static bool test;
                  
                bool PluginStart(void)
                {
                     SetTest(false);
                     // rest of startup functions....
                }
                  
                void SetTest(bool t)
                {
                     test = t;
                }
                  
                bool GetTest(void)
                {
                     return test;
                }
                

                Then in your main header file (MyPlugin.h or whatever your plugin is named) you prototype the accessor functions:

                void SetTest(bool t);
                bool GetTest(void);
                

                Then you simply include the main header file in each .cpp file where you need access to the "test" variable:

                #include "MyPlugin.h"
                //......
                  
                if(GetTest())
                {
                     // do something
                }
                

                Adios,
                Cactus Dan

                1 Reply Last reply Reply Quote 0
                • H
                  Helper
                  last edited by

                  On 12/10/2013 at 09:29, xxxxxxxx wrote:

                  thank you very much for the input.. 
                  still get the following error message:

                  1>  myPlugin.cpp
                  1>myPlugin.obj : error LNK2001: unresolved external symbol "int __cdecl getTest(void)" (?getTest@@YAHXZ)
                  1>C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\myPlugin.cdl64 : fatal error LNK1120: 1 unresolved externals

                  the thread i mentioned earlier said it is some problem with the linker. and since all those tests result in the same error i guess it must be some general setting i am missing??

                  EDIT: my fault. i mixed a uppercase letter 😉 now working. many thanks !

                  1 Reply Last reply Reply Quote 0
                  • H
                    Helper
                    last edited by

                    On 12/10/2013 at 09:36, xxxxxxxx wrote:

                    Howdy,

                    Did you forget to include the header?

                    Adios,
                    Cactus Dan

                    1 Reply Last reply Reply Quote 0
                    • H
                      Helper
                      last edited by

                      On 12/10/2013 at 09:37, xxxxxxxx wrote:

                      ..no, it was a typo... now working 😄

                      1 Reply Last reply Reply Quote 0
                      • H
                        Helper
                        last edited by

                        On 12/10/2013 at 09:45, xxxxxxxx wrote:

                        Howdy,

                        Ah! Glad it's working for you.

                        Adios,
                        Cactus Dan

                        1 Reply Last reply Reply Quote 0
                        • H
                          Helper
                          last edited by

                          On 12/10/2013 at 09:46, xxxxxxxx wrote:

                          i am happy, too, since this helped me solve the thing with my demo version of the plugin, too 🙂

                          1 Reply Last reply Reply Quote 0
                          • H
                            Helper
                            last edited by

                            On 14/10/2013 at 12:06, xxxxxxxx wrote:

                            It's quite easy as Niklas suggested.

                            In your main.cpp:

                              
                            bool your_global_variable = false;  
                              
                            PluginStart()  
                            {  
                              your_global_variable = true;  
                            }  
                            

                            In any other file (e.g. myPlugin.h) :

                              
                            extern bool your_global_variable;  
                              
                            int any_function(int a, int b) //or anywhere else in this file  
                            {  
                              //do something with your_global_variable  
                              return your_global_variable ? (a + b) : (a - b);  
                            }  
                            

                            You don't need to include any file anywhere. You just need the declarations to make the scope see it.

                            Originally posted by xxxxxxxx

                            thank you. i tried this but still i must do something wrong.

                            in the main.cpp i have #include "myPlugin.h"

                            and in myPlugin.h i have

                            #ifndef _myPlugin_H_
                            #define _myPlugin_H_

                            extern int myTest;

                            enum
                            {
                             ...

                            when i try to compile i get the following error:
                            [quote]
                            1>  Main.cpp
                            1>  myPlugin.cpp
                            1>     Creating library C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\obj\myPlugin\x64_Debug\myPlugin.cdl64.lib and object C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\obj\myPlugin\x64_Debug\myPlugin.cdl64.exp
                            1>Main.obj : error LNK2001: unresolved external symbol "int myTest" (?isRegistered@@3HA)
                            1>myPlugin.obj : error LNK2001: unresolved external symbol "int myTest" (?isRegistered@@3HA)
                            1>C:\Program Files\MAXON\CINEMA 4D R14\plugins\myPlugin\myPlugin.cdl64 : fatal error LNK1120: 1 unresolved externals

                            i found this thread http://stackoverflow.com/questions/7004031/c-unresolved-externals but i dont understand what i need to do now...
                            [/QUOTE]

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post