YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Method called from C++ into sq class cannot access class member
comarius
#1 Posted : Thursday, May 24, 2018 9:36:31 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/10/2017(UTC)
Posts: 5
Location: Toronto

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi,
Ass son I access a class member it crashes.
Angular brackets were replaced with lt gt because this forum sql crashes.

Code:

class Some extends CppClass
{
    _var=null;

    constructor(name)
    {
        base.constructor(name); //   -> calls C++ ctor OK
        _var=7;
    }

    function getParam(aaa) // called from C++ getParam("xxx")  OK
    {
        return param;
    }

    function getValue() // called from C++ getValue() AN ERROR HAS OCCURED [the index '_var' does not exist]
    {
        return _var;
//      return this._var;  [the index '_var' does not exist]
    }
}

var some = Some("abc");
print(some.getValue());         //OK
print(some.getParam("xxx"));    // OK

Now from C++
Code:

CppClass* PSCR;
class CppClass
{
    std::string _name;
public:
    CppClass(const char* name)_name(name)
    {
        PSCR = this; //for the purpose of this demo
    }
    void getValue()
    {
        Sqrat::Object classobj = Sqrat::RootTable().GetSlot("Some");
        if(!classobj.IsNull())
        {
            Sqrat::Function f1 = classobj.GetSlot( "getParam" ).Cast<Sqrat::Function>();
            Sqrat::Function f2 = classobj.GetSlot( "getValue" ).Cast<Sqrat::Function>();
            try{
                Sqrat::SharedPtr<char*>   srv;
                srv = f1.Evaluate<char*>("xxx");
                std::cout << (*srv.Get()) << "\n";      //   printx xxx

                srv = f2.Evaluate<char*>();     // BUM!!!!
                                                //AN ERROR HAS OCCURED [the index '_var' does not exist]

           
            }catch(Sqrat::Exception ex){
                std::cout << ex.Message();
            }
        }
    }   

    static void expose(SqEnv& e){
        Sqrat::Class<SqDev> cls(e.theVM(), _SC("CppClass"));
        cls.Ctor<const char*>();
        Sqrat::RootTable().Bind(_SC("CppClass"), cls);
    }

};

main()
{
    /* ... */
    CppClass::expose(VM);
    // run the script...
    PSCR->getValue();
   
}

















mariusco
#2 Posted : Monday, July 16, 2018 5:03:43 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/11/2018(UTC)
Posts: 4
Location: toronto

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Resolved:
-language details/ public private, all inline... for clarity


C++:
Code:

class Cpp
{   
     Cpp(Sqrat::Object o ):_o(o){}

     static void expose(HSQUIRRELVM vm){
        Sqrat::Class<Cpp> cls(vm, _SC("Cpp"));
        cls.Ctor %lt; Sqrat::Object >();
        cls.Func(_SC("CallMe"), &Cpp::CallMe);
        Sqrat::RootTable().Bind(_SC("Cpp"), cls);
     }
     
     int CallMe(int some)
     {
         Sqrat::Function f = Sqrat::Function(_o, "scriptFoo"); // on the _o
         Sqrat::SharedPtr<int>   srv = f.Evaluate<int>(some);
         return *srv.Get();
     }
     
     Sqrat::Object   _o;
};



Script:
Code:

class Script extend Cpp
{
    member=null;
    constructor(x)
    {
        base.constructor(this);  // calls Cpp(Sqrat::Object o )
        member=x;                // de-staticit
    }

    function scriptFoo(mm)       
    {
       member = member + x;      // to show it goes on instance
       print(member + "\n");
       return member;
    }
}


var aScript = Script(1);
var bScript = Script(2);

aScript.CallMe(5);   //prints 6
bScript.CallMe(5);   //prints 7

Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.092 seconds.