Squirrel

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

sqplus and operators

Last post 05-14-2006, 3:33 PM by macdonag. 5 replies.
Sort Posts: Previous Next
  •  05-13-2006, 2:20 AM 870

    sqplus and operators

    Is there an example of using C++ operators with sqplus?  I tried func(&Test::operator+=, "add") but that didn't work.

    Thanks!
    --graham
  •  05-13-2006, 11:04 AM 872 in reply to 870

    Re: sqplus and operators


    struct Vector3 {
      float x,y,z;
      Vector3() {
        x = 1.f;
        y = 2.f;
        z = 3.f;
      }
      Vector3(float _x,float _y,float _z) : x(_x), y(_y), z(_z) {}
      Vector3 operator+(Vector3 & v) {
        return Vector3(x+v.x,y+v.y,z+v.z);
      }
    };

    SQClassDef<Vector3>(_T("Vector3")).
      var(&Vector3::x,_T("x")).
      var(&Vector3::y,_T("y")).
      var(&Vector3::z,_T("z")).
      func(&Vector3::operator+,_T("_add"));



    This allows + and += to work in script for Vector3. If using constant args, define SQPLUS_CONST_OPT before including SqPlus.h.

    See the Squirrel reference manual for other metamethods (all start with underscore).
  •  05-13-2006, 4:34 PM 874 in reply to 872

    Re: sqplus and operators

    Thanks.  I'm sure I'll have more questions later...
  •  05-13-2006, 4:52 PM 875 in reply to 874

    Re: sqplus and operators

    Actually, if I follow your example (or something similar) and I try to create a new Vector (in Squirrel) eg:

    local xxx = Vector3(1.0, 2.0, 3.0);

    I get a runtime error saying wrong number of arguments.

    Do I have to do something else to get it to recognise my constructor with multiple args?  What is the simplest way of achieving this?

    Thanks!
  •  05-13-2006, 7:15 PM 876 in reply to 875

    Re: sqplus and operators

     macdonag wrote:
    Actually, if I follow your example (or something similar) and I try to create a new Vector (in Squirrel) eg:

    local xxx = Vector3(1.0, 2.0, 3.0);

    I get a runtime error saying wrong number of arguments.

    Do I have to do something else to get it to recognise my constructor with multiple args?  What is the simplest way of achieving this?

    Thanks!


    Yes: see the examples in testSqPlus2.cpp for custom constructor/variable args.
  •  05-14-2006, 3:33 PM 877 in reply to 876

    Re: sqplus and operators

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