Squirrel

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

SqPlus - filling pure virtual functions in squirrel [Partly solved it now just optimising help needed]

Last post 07-20-2008, 7:19 AM by Sudi. 1 replies.
Sort Posts: Previous Next
  •  07-19-2008, 2:53 PM 2643

    SqPlus - filling pure virtual functions in squirrel [Partly solved it now just optimising help needed]

    I have the following setup in my game:

    i have a IAppFrame and a IGameState class. now when initing a new instance/implemantation of IGameState it automaticly connects to IAppFrame. IAppFrame then calls the Updatefunction of IGameState. this all works great when i implement a GameState in c++ but not when doing it with squirrel b somehow the function isnot overiden.

    Ok searched the forum for this problem and the only thing i found was someone saying that u might have to call the scripted function from witin c++.
  •  07-20-2008, 7:19 AM 2644 in reply to 2643

    Re: SqPlus - filling pure virtual functions in squirrel

    OK i'm getting closer to solving this problem.
    I took a look at python and saw they are using a wrapper class. So i did the same with squirrel.
    Now i can pass a class made in squirrel back to c++ and the functions are called.

    Note: i made the wrapper a template bc boost.python is doing it too. dunno why maybe i will remove it when i find out it doesn't do anything i would need(still ready boost code).
    One thing is there a way i could check if the function is declared in the squirel object? bc like this i can only wrap pure virtual function none with standard implementations.  oh and another thing am i recreating the function everytime i call it? maybe there is a more ellegant way of doing this.

    C++:
    //class to export to squirrel
    class FOO
    {
    public:
             virtual void do(void) = 0;
    };

    //the wrapper
    template<typename T>
        class wrapper
        {
        public:

            wrapper(void)
            {
                AppFrame::ILogger::log("creating wrapper");
            }

            virtual ~wrapper(void)
            {
            }

            void setObject(SquirrelObject obj)
            {
                m_obj = obj;
            }
            template<typename R>
            SquirrelFunction<R>& getFunction(const char* name)
            {
                SquirrelFunction<R> f(m_obj, name);
                AppFrame::ILogger::log("created fuction without");
                return f;
            }
        private:
            SquirrelObject m_obj;
        };

    //make a squirrel imp for exporting
    class SQFOO : public FOO, public wrapper<FOO>
    {
    public:
             virtual void do(void)
            {
                      getFunction<void>("do")();
            }
    };

    //now export
    DECLARE_INSTANCE_TYPE(FOO)
    DECLARE_INSTANCE_TYPE(SQFOO)

    //this is the important part where u bind the squirrel object to c++ one so u can really use vars from the squirrel object
    int FooConstructor(HSQUIRRELVM v)
    {
             SQFOO* f = new SQFOO();
            int r =  PostConstruct<SQFOO>(v, state, ReleaseClassPtr<SQFOO>::release);
            StackHandler sa(v);
            HSQOBJECT ho = sa.GetObjectHandle(1); // OT_INSTANCE
            SquirrelObject instance(ho);
            state->setObject(instance);
            return r;
    }

    //bind the constructor
    void bind_IGameState(void)
    {
            SQClassDef<SQFOO>(_T("FOO")).
            staticFunc(FooConstructor,_T("constructor"));
    }

    SQUIRREL:
    class MyFoo extends FOO
    {
             function do
             {
                   print("here i am");
             }
    }
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems