Completely Lost

Started by
1 comment, last by lxnyce 18 years, 9 months ago
I have tried to upgrade to the 2.0 version a couple of times, but each time something breaks and I put off messing with it for a while. Can someone please show me how to do something really simple? Below is a unit test, I converted to describe my problem. I have a class VertexBuffer which is defined by code. I want to have a script function which takes in this class as a pointer (reference, or whatever else there is), so that I can call functions on the class. Now from the code, I want to call the script defined function with the c++ VeretxBuffer pointer. How should I go about doing this?

#include "utils.h"

namespace TestImport
{

#define TESTNAME "TestClassPointer"

//! Really simple VertexBuffer class for testing
class VertexBuffer {
public:
	//! Doesn't really do anything, just here so we can call it from a script
	virtual void Vertex3f( float x, float y, float z) {
		float xlocal = x; /* Just make sure we reached here */
	}
};

//! Really simple script to test the vertex buffer loading
static const char *script1 =
"void VertexBufferCreate( VertexBuffer *vb )          \n"
"{                                                    \n"
"  vb->Vertex3f(1,2,3);                               \n"
"}                                                    \n";

bool Test() {
	bool fail = false;

	int number = 0;
	int r;

 	asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

	r = engine->RegisterObjectType("VertexBuffer", 0, asOBJ_CLASS); assert( r >= 0 );
	r = engine->RegisterObjectMethod("VertexBuffer", "void Vertex3f( float x, float y, float z)", asMETHOD(VertexBuffer, Vertex3f), asCALL_THISCALL); assert( r >= 0 );
	//! Is there a way to register a global vertex buffer object 
	static VertexBuffer *vb = NULL;
	/* How would I register VB with the script so that it is accessed as a global property? */

	COutStream out;
	engine->AddScriptSection(0, TESTNAME ":1", script1, strlen(script1), 0);
	engine->Build(0, &out);

	/* How do I call VertexBufferCreate in the script and pass vb as an argument to it? */
	engine->ExecuteString(0, "VertexBufferCreate( NULL )", &out);
	engine->Release();

	if( number != 1 )
	{
		fail = true;
	}

	// Success
	return fail;
}

} // namespace


I really love angelscript, but even since the introduction of @, in, out, etc.. I have been really lost. I just want to get back on track so any help would be appreciated.
Advertisement
1. Pointers are no longer available in the script language. You could write your script like this instead:

static const char *script1 ="void VertexBufferCreate( VertexBuffer &inout vb )    \n""{                                                    \n""  vb.Vertex3f(1,2,3);                                \n""}                                                    \n";


2. ExecuteString() doesn't allow you to pass parameters any other way than as script expressions, thus it may be a bit difficult to pass the VertexBuffer to the script function this way. You'll need to create a context and call the script function directly instead.

VertexBuffer vb;// Assuming you're using version 2.3.0asIScriptContext *ctx = engine->CreateContext();ctx->Prepare(engine->GetFunctionIdByDecl(0, "void VertexBufferCreate(VertexBuffer &inout)"));ctx->SetObjectArg(0, &vb); // Pass in the reference to the vbint r = ctx->Execute();if( r != asEXECUTION_FINISHED ){  // The script didn't finish correctly, do something...}


3. Yes, you can register a global vertex buffer object, if you want. E.g:

VertexBuffer vb;engine->RegisterGlobalProperty("VertexBuffer vb", &vb); 


4. The way you registered your VertexBuffer type may not be sufficient. You may have to do it something like this:

engine->RegisterObjectType("VertexBuffer", sizeof(VertexBuffer), asOBJ_CLASS_CDA);engine->RegisterObjectBehaviour("VertexBuffer", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(VertexBuffer_Construct), asCALL_CDECL_OBJLAST);engine->RegisterObjectBehaviour("VertexBuffer", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(VertexBuffer_Destruct), asCALL_CDECL_OBJLAST);engine->RegisterObjectBehaviour("VertexBuffer", asBEHAVE_ASSIGNMENT, "VertexBuffer &f(const VertexBuffer &in)", asMETHODPR(VertexBuffer, (const VertexBuffer &), VertexBuffer &), asCALL_THISCALL);


Or if you wish to have the vertex buffer support object handles, you may replace the DESTRUCT behaviour with the ADDREF/RELEASE behaviours. The ADDREF/RELEASE behaviours are then responsible for increasing and decreasing a reference counter for the object. When the refernce counter reaches zero it should automatically destroy the object.




WARNING! I didn't compile any of the above code as I'm not at home right now, and don't have the AngelScript code at hand. It is quite possible that it contains some error.


Please take a look at the samples in the AngelScript zip for more information on how to register objects, functions, and properties.

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

Thanks WitchLord, your fast responses is also one of the reason I love AngelScript. This clarifies things alot for me though. I'll let you know how it goes.

This topic is closed to new replies.

Advertisement