Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

SqPlus and Namespaces

Last post 10-09-2005, 10:50 AM by John Schultz. 1 replies.
Sort Posts: Previous Next
  •  10-09-2005, 1:47 AM 355

    SqPlus and Namespaces

    Hello all. I want to create a namespace function so I may access it from the script as "Sys.Debug("some text")"
    Here's my cpp code:

    SquirrelVM::PushRootTable();
    SquirrelVM::PushValue("Sys");
    SquirrelVM::CreateTable();
    SquirrelObject Root = SquirrelVM::GetRootTable();
    SquirrelObject SysNamespace = Root.GetValue("Sys");

    Register(SquirrelVM::GetVMPtr(), SysNamespace, WrapOutputDebug, "Debug");

    I'm quite sure I'm doing this correct. The Problem seems to be at the Register line, I get a plethora of compile errors, most of which seem to be somehow related to the SqPlus::Call function ("expects 3 arguments, provided 4", etc) , however when I just use the Register overload which only takes three parameters (hsquirrelvm, func, string), it compiles fine. Hrm, any help is appreciated!
  •  10-09-2005, 10:50 AM 357 in reply to 355

    Re: SqPlus and Namespaces

    Register requires an object handle (instead of a SquirrelObject):

    Register(SquirrelVM::GetVMPtr(), SysNamespace.GetObjectHandle(), WrapOutputDebug, "Debug");

    Tested examples:

    void globalFunc(const char * s,int val) {
      printf("globalFunc: s: %s val: %d\n",s,val);
    } // globalFunc

    // You can add functions/vars here, as well as bind globals to be accessed through this class as shown in the Namespace example below.
    // If the class is instantiated in script, the instance is "locked", preventing accidental changes to elements.
    // Thus using an instance as the namespace can be a better design for development.
    struct NameSpaceClass {
    };

    ...

    // Init and test:

    HSQUIRRELVM v = SquirrelVM::GetVMPtr();
    SquirrelObject root = SquirrelVM::GetRootTable();

    // Create a namespace using a table.
    SquirrelObject nameSpaceTable = SquirrelVM::CreateTable();
    root.SetValue("Namespace1",nameSpaceTable);
    Register(v,nameSpaceTable.GetObjectHandle(),globalFunc,"nameSpaceFunc");

    // Create a namespace using a class. If an instance is created from this class, using the instance will prevent accidental changes to the instance members.
    // Using the class/instance form also allows extra information to be added to the proxy class (and registered), if desired (such as vars/funcs).
    SQClassDef("NameSpace2").
      staticFunc(globalFunc,"nameSpaceFunc");

    SquirrelObject testNameSpace = SquirrelVM::CompileBuffer("\
      Namespace1.nameSpaceFunc(\"Hello Namespace1 (table),\",321); \n\
      NameSpace2.nameSpaceFunc(\"Hello Namespace2 (class),\",654); \n\
      local NameSpace3 = NameSpace2(); \n\
      NameSpace3.nameSpaceFunc(\"Hello Namespace3 (instance),\",987); \n\
    ");

    SquirrelVM::RunScript(testNameSpace);


    John
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems