Squirrel

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

Create instance of a Squirrel class in C++

Last post 06-04-2008, 12:00 PM by AndiNo. 3 replies.
Sort Posts: Previous Next
  •  05-31-2008, 10:50 AM 2552

    Create instance of a Squirrel class in C++

    Hi! I'm new to Squirrel but it seems to be exactly the type of scripting language I'm looking for. At the moment I only initialize/close Squirrel in my game (very basic stuff). For my game objects I want to implement a class structure like it is in Unreal. I have a base class "CGameObject" in C++ which is also known in Squirrel script. Every game object should be defined in script as a subclass of CGameObject. I guess this works so far, but let's assume I have an array of CGameObjects in C++ and a class Enemy defined in Squirrel as subclass of CGameObject. How can I create an object/instance of that class in C++? There must be something like
    CGameObject go = sq_createobject("Enemy");
    btw: I'm using SqPlus.
    Thanks in advance!
  •  06-03-2008, 11:30 AM 2555 in reply to 2552

    Re: Create instance of a Squirrel class in C++

    The SqPlus Wiki-site http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlus.html states that "C++ classes can inherit from Squirrel classes". If someone could give me a hint on how to do that I might be a step closer to what I want to accomplish.
    Another possibility would be to create a vector/array of SquirrelObjects, although I'm not sure it would work the same way I want it to...
  •  06-04-2008, 9:38 AM 2558 in reply to 2552

    Re: Create instance of a Squirrel class in C++

    Hello,

    Creating objects:

    There are several ways, of course including running a script snippet that does it.

    At C/C++ level, it would still need to happen through the stack. I understand that new instances are created by pushing the class object on the stack and calling meta method _opcall. Something like:

      HSQUIRRELVM v = /* from somehwre */
      sq_pushroottable( v );
      sq_pushstring( v, _SC("Player") )
      sq_get( v, 2 );  // Get the class
      sq_createinstance( v, 1 );

    --------

    Second question, extending a Squirrel class in C++:

    Inheriting in C++ from Squirrel is easier than the other way around. Basically, in Squirrel:

      class MyClass {
        function MyFunc( ){ return "A return value" }
      }

    Then using SqPlus, you can declare in C++ :

      class MyDerivedClass {
      public:
        const char* MyFuncOverride( ){ return "Override return value"; }
     }

    and then bind the class using SqClassDef:

      SQClassDef<MyDerivedClass>( "MyDerivedClass", "MyClass" ).
        func( &MyDerivedClass::MyFuncOverride, "MyFunc" );

    SqPlus then generates the Squirrel class that does the binding.


    Regards
    // ATS.
  •  06-04-2008, 12:00 PM 2559 in reply to 2558

    Re: Create instance of a Squirrel class in C++

    I used the past few days to wade through the forum... Currently I'm sticking with an array of SquirrelObjects, this seems to do the trick. First I create an instance with CreateConstructNativeClassInstance, then I attach a SquirrelObject to it with AttachToStackObject(-1). From there I just have to use GetValue to get a class-specific function and then call it for example.

    @ats
    This is very interesting, especially the C++ inheritance from Squirrel. Maybe this will be useful in the future. Thank you for your "enlightenment" ;)
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems