RegisterGlobalFunction returning script class

Started by
1 comment, last by mk1x86 10 years, 9 months ago

Sorry for creating so many threads recently. I've got some kind of chicken and egg problem.

There's a class "EffectC" implemented in C++ and also an angelscript class "Effect" which is loaded in a "base" module. When EffectC is created, it creates an instance of "Effect" which has a constructor, taking EffectC as parameter:


shared class Effect : DynamicBase, Object
{
	private EffectC@ self;
	Effect(EffectC@ obj) { @self = obj; }
	//...
}

Now here's the problem: In Angelscript I'd like to create a new effect, calling a globalFunction "Effect@ createEffect()". This should actually be a registered C++ function, that creates "EffectC", which in turn creates an instance of "Effect" and returns it.


r = engine->RegisterGlobalFunction("Effect@ createEffect(uint res)", asFUNCTION(asCreateEffect), asCALL_CDECL);

This doesn't work, obviously, because the engine doesn't know about "Effect" (identifier "Effect" is not a data type).

Is it even possible to use a C++ function and return an asIScriptObject?

Advertisement

It is possible for registered function to create and return script classes. Though you can't register the function with a declaration to return the class directly as the class type is not known at the time of registration. To solve this you can either register an interface that the script class must implement, or use the ScriptHandle add-on to return a generic reference.

The manual has an article for how to use script classes from the application:

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_use_script_class.html

It ought to answer your questions. If not, please let me know so I can improve it.

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

Using an interface worked perfectly. Thanks a lot :)

This topic is closed to new replies.

Advertisement