Advertisement

Proper way of creating as class in C++

Started by July 16, 2013 12:22 PM
1 comment, last by WitchLord 11 years, 4 months ago

I've googled but couldn't come up with a proper solution. I'd like to create an instance of an angelscript class from C++. My current approach:


asIObjectType* type = modBase->GetObjectTypeByName("Test");
asIScriptObject* obj = static_cast<asIScriptObject*>(engine->CreateScriptObject(type->GetTypeId()));

While this works I wondered, why CreateScriptObject returned a void pointer instead of a script object. Investigating as_scriptengine.cpp, I noticed, that the method was marked as deprecated and creation should be done via a factory. How does it work? The class is guaranteed to have a default constructor.

Also, what's wrong with the way it's currently done?

asIObjectType has the functions GetFactoryByDecl(), GetFactoryByIndex() and so one, that lets you get the factory functions for an object type. You can use those factory functions to create objects of that type.
Advertisement

How to create instances of script classes using the factory functions is described in the manual:

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

asIScriptEngine::CreateScriptObject() is not yet deprecated, but I have plans to do so in the near future as it should take as input the asIObjectType* instead of the type id. It returns a void* instead of an asIScriptObject* because it can be used to create application registered types as well that do not derive from the asIScriptObject interface.

CreateScriptObject() only allows you to create objects with the default factory, i.e. with no arguments. If you need to use one of the non-default factories, then you must do it by calling the appropriate factory using an asIScriptContext as described in the manual.

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