Sign in to follow this  

Proper way of creating as class in C++

This topic is 1645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this