Advertisement

Using C++ Classes within Scripts

Started by February 26, 2010 08:19 AM
1 comment, last by SiCrane 14 years, 9 months ago
Hi guys, Realise this might be a bit of a stupid question, but although the C++ code for exposing a class to AngelScript is available all over the place, I can't seem to find an example of how to use such a class in the actual .as file. I have set all the factory functions and (I think) covered all the bases as regards exposing the class, but when I try to use it in AngelScript by simply using the class name, it gives an error. Below is the C++ code:

#include "f_baseentity.h"
#include "f_script.h"
#include "angelscript.h"

BaseEntity::BaseEntity()
{
	refCount = 1;
}

BaseEntity* Ref_Factory()
{
	return new BaseEntity();
}

void BaseEntity::Addref()
{
	refCount++;
}

void BaseEntity::Release()
{
	if( --refCount == 0 )
		delete this;
}

BaseEntity::~BaseEntity()
{
}

void BaseEntity::RegisterClass(FScript *script)
{
	asIScriptEngine *engine = script->getEngine();
	engine->RegisterObjectType("BaseEntity", 0, asOBJ_REF);
	engine->RegisterObjectBehaviour("BaseEntity", asBEHAVE_FACTORY, "BaseEntity@ f()", asFUNCTION(Ref_Factory), asCALL_CDECL);
	engine->RegisterObjectBehaviour("BaseEntity", asBEHAVE_ADDREF, "void f()", asMETHOD(BaseEntity, Addref), asCALL_THISCALL);
	engine->RegisterObjectBehaviour("BaseEntity", asBEHAVE_RELEASE, "void f()", asMETHOD(BaseEntity, Release), asCALL_THISCALL);
}
I have tried a number of things in AngelScript, including inheritance, and also defining a class in AS with the same name and then inheriting from that, but no dice. Any help is much appreciated.
Currently you can not inherit classes defined from c++. If you want to use the class you defined you can use it like
BaseEntity @entity = @BaseEntity();
Advertisement
Here is a discussion on faking inheritance from C++ classes.

This topic is closed to new replies.

Advertisement