Menu

#60 Nested native instances are read-only

None
closed
nobody
None
5
2015-02-08
2014-02-25
Aerizeon
No

I have used sqrat to bind several C++ classes to squirrel, some of which are used in other classes.
Whenever an instance of a class is initialized in squirrel, I am unable to set any nested native variables after the first level (however, they do return their values).

This just provides an example, it's not actual code:

Pseudocode Example:

C++ Classes:

:::C++
class Point2D{
    float X = 5;
    float Y = 5;
};

class Window{
     string Name = "Default";
     Point2D Position = Point2D(); 
};

int main()
{
    [...]
    RootTable().Bind("Point2D",Class<Point2D>()
                     .Var("X",&Point2D::X));
    RootTable().Bind("Window",Class<Window>()
                     .Var("Position",&Window::Position)
                     .Var("Name",&Window::Name));
}

Squirrel Implementation:

:::squirrel
function OnInit()
{
    local Win = Window();
    Win.Name = "New";
    Win.Position = Point2D();
    ::print(Win.Name);        //Correctly Prints "New"
    ::print(Win.Position.X);  //Correctly prints 5 (default value)
    Win.Name = "Other";       //Tries to set non-nested variable
    Win.Position.X = 11;      //Tries to set nested variable
    ::print(Win.Name);        //Correctly prints "Other"
    ::print(Win.Position.X);  //Incorrectly still prints 5
}

Any ideas what the issue may be, or how to fix it?

Discussion

  • Wizzard

    Wizzard - 2014-02-26

    Nothing immediately strikes me as being wrong in the way you are using Sqrat.
    This might be a compiler optimization issue where no variable is being created.

    To test for this, try the following substitution

    class Point2D {
        Point2D() : X(5), Y(5) {}
        float X;
        float Y;
    };
    

    Other than that, I'll have to check to see if its a problem with Sqrat over the weekend.
    I use Sqrat in a similar way, but I always set the value of the position,
    and it is never default.

     

    Last edit: Wizzard 2014-02-26
  • Wizzard

    Wizzard - 2014-02-26
    • status: open --> pending
    • Group: -->
     
  • Aerizeon

    Aerizeon - 2014-02-26

    I changed the class definition as per your request, and it had no effect on the output
    The Definitions are now as follows (this is slightly different, as the original post was not real code):

    C++:

    :::c++
    class Point2D
    {
    public:
      Point2D(): X(5),Y(5) {}
        float X;
        float Y;
    };
    
    class Window
    {
    public:
      Window(): Name("Default"),Position(Point2D()) {}
        string  Name;
        Point2D Position;
    
    };
    
    int main()
    {
        HSQUIRRELVM SquirrelVM = sq_open(2048);
        sq_setprintfunc(SquirrelVM,SquirrelOutputHandler, NULL);
        sq_pushroottable(SquirrelVM);
        DefaultVM::Set(SquirrelVM);
        RootTable().Bind("Point2D",Class<Point2D>()
                      .Var("X", &Point2D::X)
                      .Var("Y", &Point2D::Y));
        RootTable().Bind("Window",Class<Window>()
                      .Var("Name", &Window::Name)
                      .Var("Position", &Window::Position));
        try
        {
        Script* Main = new Script();
            Main->CompileFile("test.sq");
            Main->Run();
        }
        catch(Sqrat::Exception ex)
            cout << ex.Message() << endl;
        return 0;
    }
    

    Squirrel:

    :::squirrel
    local Win = Window();
    print("Default:\r\n" + Win.Name + "\r\n" + Win.Position.X + "\r\nChanging\r\n");
    Win.Name = "New";
    Win.Position.X = 1000;
    print("Default:\r\n" + Win.Name + "\r\n" + Win.Position.X + "\r\nChanging\r\n");
    
     
  • Aerizeon

    Aerizeon - 2014-02-26

    Forgot to include this in my post, however I am compiling in the following environment:

    • Windows 7 x64 using:
    • GCC 4.7.0 for MinGW (x86 only)
    • Msys basic targeting MinGW/GCC 4.7.0-x86
    • Squirrel is statically linked, compiled with all defaults
    • SQRat is included as it's header
    • No compiler optimizations
    • Default C++ Standard (no 0x or 11)
    • Targeting x86 (as MinGW does not support x64 compilation - at least when I last checked)
     
  • Wizzard

    Wizzard - 2014-02-26

    Yeah. I'll try to investigate this over the weekend. Hmm...

     
  • Wizzard

    Wizzard - 2014-03-01

    Fixed in commit [d1cb7d]

    There was an issue where Sqrat was not returning references in its get metamethod for classes

     

    Related

    Commit: [d1cb7d]

  • Wizzard

    Wizzard - 2014-03-01
    • status: pending --> accepted
     
  • Wizzard

    Wizzard - 2014-03-01
    • status: accepted --> closed
     
  • Aerizeon

    Aerizeon - 2014-03-01

    Awesome, thank you :D

     

Log in to post a comment.

MongoDB Logo MongoDB