🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Registering non-default constructors

Started by
0 comments, last by WitchLord 16 years, 3 months ago
Hello! I recently started a small game project and decided to take a look at AngelScript to implement a simple in-game console and later maybe also scripting-support. The first I want to do is exposing my Lightsource class to the script engine. My class has several methods like this:

class Light
{
    SetDiffuse(const Color &color);
}
which would be used like this in C++:

  light0.SetDiffuse(Color(1.0f, 0, 0));
From what I figured out, the best way to make this usable in Angelscript is to register my Color class as a value type in AngelScript, so what I did is this:


	static void DefineScriptInterface(asIScriptEngine* engine)
	{
	    int error = 0;
		
	    //register class name
	    error = engine->RegisterObjectType("Color", sizeof(Color), asOBJ_VALUE 
														| asOBJ_POD 
														| asOBJ_APP_CLASS_ASSIGNMENT
												| asOBJ_APP_CLASS_CONSTRUCTOR
																	);
			assert(error >= 0);
			
			//register data members
			error = engine->RegisterObjectProperty("Color", "float r", offsetof(Color, r));
			assert(error >= 0);
			
			error = engine->RegisterObjectProperty("Color", "float g", offsetof(Color, g));
			assert(error >= 0);

			error = engine->RegisterObjectProperty("Color", "float b", offsetof(Color, b));
			assert(error >= 0);

			error = engine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f(float r, float g, float b)",
asFUNCTION(&Color::ScriptConstructor), asCALL_CDECL);
			assert(error >= 0);
			
			
		
		}

		static void ScriptConstructor(void *memory, float r, float g, float b)
		{
			//placement-new and explicit initialisation
			new (memory) Color(r, g, b);
		}


My problem is, that I'm not quite able to figure out how to expose the non-default constructor to the script engine, everything I've tried results in an asNOT_SUPPORTED error :( . Your help would be appreciated... Greetings Martin Edit: sorry, can't get my code formated right here
Advertisement
Just a tiny change:

error = engine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f(float r, float g, float b)",asFUNCTION(&Color::ScriptConstructor), asCALL_CDECL_OBJFIRST);


asCALL_CDECL_OBJFIRST tells AngelScript that the global function should work as a class method, where the object pointer is passed as the first parameter.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement