Show Changes Show Changes
Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Administration Page Administration Page
Topic Locks Topic Locks



2/22/2017 3:51:55 AM
11/8/2015 8:28:32 AM
2/24/2012 1:15:47 PM
5/31/2011 3:17:11 AM
4/26/2011 7:23:18 AM
List all versions List all versions

RSS feed for the SquirrelWiki namespace

Sq Plus Bind Class

Back to SqPlus


What do I need to bind a class to Squirrel?

You need to do three things:

1) Bind the class type and name to Squirrel(

2) Define what member functions / variables / attributes this class has.

3) If you want to construct any of these objects from within a script, define a constructor and destructor.

1 Bind the class type and name

In your header file, use one of the macros DECLARE_INSTANCE_TYPE(Type) or DECLARE_INSTANCE_TYPE_NAME(Type,Name).

The first will bind the class Type, and call it "Type" within Squirrel.

The second will bind the class Type, and call it "Name" within Squirrel.

Let's say that you have this class:

namespace Math
		class Vector3 
				int _x,_y,_z;
				Vector3(int x, int y, int z) : _x(x), _y(y), _z(z) {}
} // Math

You'd bind it like this:

DECLARE_INSTANCE_TYPE(Math::Vector3) // If you wanted it to be "Math::Vector3" within Squirrel

or like this:

DECLARE_INSTANCE_TYPE_NAME(Math::Vector3,Vector3) // If you wanted it to be "Vector3" within Squirrel

2 Define the member functions

At some initialization step in your code, you need to run SqPlus's class definition functor to define all of the attributes of your class that you want made known to Squirrel. Be sure that the name field here matches the name that you used to bind the class with the macro in step one. For Vector3, it would look like this:

    staticFunc(constructVector3,_T("constructor")). // More on this in step 3.
    var(&Vector3::_x,_T("x")).		// Make the member variable _x known as "x". Do the same for _y and _z.

SqClassDef binds variables with the .var() method, member functions with .func(), and static functions with .staticFunc(). Static functions are interesting because you can bind functions that have nothing to do with a class to that class as static member functions. This brings us to #3.

3 Define the constructor and destructor

Note that in the above SQClassDef<>, a staticFunc was used to bind a static function to Vector3. Squirrel uses the word "constructor" to bind constructors, and now we just need to define the function that's called on construction.

int constructVector3(int x, int y, int z, HSQUIRRELVM v) {
		return PostConstruct<Math::Vector3>(v, new Math::Vector3(x, y, z), ReleaseClassPtr<Math::Vector3>::release);

This just defines one functor that creates a new Vector3, and a second functor that defines how to delete it again. SqPlus has many functors for creation and deletion in different ways, and you can define your own as well. If your class doesn't need to be passed any arguments on construction, both this step and the .staticFunc() call in #2 aren't needed. SqPlus also has the ability to allow constructors with varying amounts and types of arguments, but that's outside the scope of this introductory page.


Now, in your Squirrel script, you can just create and use Vector3s like this:

		local Vector3 vec = Vector3(1,2,3);
		vec.y = 37;

Take a look at the minimal example for how this all comes together.

Not logged in. Log in

squirrel's community wiki

This is FlexWiki, an open source wiki engine.

Change Style

Recent Topics