• 9
• 9
• 11
• 13
• 9

Factory Functions

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

Recommended Posts

Is it possible to create an instance of an object that was defined by the user (asIScriptObject) with out calling the factory function (constructor)?

Or is it possible to define your own version of factory function?

Ok the reason for asking (so that you understand my problem), is this. Assuming that you are familar with the game example included in the as library; users must create a function a particular way in order for angel script to create the object. This seems tedious and very un-user friendly. I am looking to remove the use of constructors and rather let users use a Start() function to define any terms they would like. This is due to the fact I will be using serialized data to override the start variables.

So any help would be awesome :)

chasester

Share on other sites

If the class doesn't define any constructor at all, then it will get a default constructor. asIScriptEngine::CreateScriptObject can be used to create instances of classes that have default constructors (or you can manually call the default factory). The application can then call the Start method on the class that the user defined.

When deserializing a script object you should preferably use the method asIScriptEngine::CreateUninitializedScriptObject. The method will basically just allocate the necessary memory, but will not call any of the class' constructors. Once created the application should enumerate the properties of the object and set the value of each of them to whatever was serialized before.

Share on other sites

again very helpful, thanx very much :)

chasester

Share on other sites

again very helpful, thanx very much

chasester

i had problems implementing this, its probably due to my lack of understanding what this exactly does (so ill give you more details on what exactly im doing):

//in hallo.h similar to angelscript.h
struct scriptmgr // similar to class scriptmgr
{
...
asIScriptObject *CreateController(const str &script, gamenode *gameObj)
{
asIScriptObject *instance;
scriptctrl *ctrl = getctrlscript(script);
//scriptctrl is similar to CGameObj::struct SControler
//getctrlscript runs the same code as class scriptmgr::GetControlScript()
if (!ctrl) return 0;
instance = *((asIscriptObject**)asEngine->CreateScriptObject(ctrl->type));
//scriptctrl::type is a IScriptObjectType
if(!instance) return 0;
//I assume this is the correct cast, this returns a value (i think its a nullptr)
// so !instance is false
instance->AddRef(); // this throws a null pointer error and crashes the program
//it will throw a runtime error any time instance-> so this would seem that instance is null or pointing to bad data
return instance;
}
...
};



So im doing something wrong, the main point of this is to force the default constructor when c++ calls the code, but if the user calls it in as they can use what ever constructor that they state.

On a side note, i would like the CGameObj  @self; link to be seamless, would it be better to define this inside the game code as a base class rather than using a interface.

I think im asking about a billion questions sorry I feel like I'm over complicating this

chasester

Edited by chasester123

Share on other sites

instance = *((asIscriptObject**)asEngine->CreateScriptObject(ctrl->type));


This should be instance = (asIScriptObject*)asEngine->CreateScriptObject(ctrl->type);

instance->AddRef(); // this throws a null pointer error and crashes the program


This shouldn't be done. The pointer returned by CreateScriptObject already has one reference counted for, which is owned by the application that called CreateScriptObject. When you are done with the object you should call instance->Release() to free the memory.

On a side note, i would like the CGameObj  @self; link to be seamless, would it be better to define this inside the game code as a base class rather than using a interface.

I didn't quite understand your question. Do you want that the script writer doesn't have to declare CGameObj @self in the script class? If so, yes you can have the application implement a base class that all scripts controllers must inherit from instead of the controller interface.

Share on other sites

We've got such a system in our code. All script classes that extend C++ classes must inherit from a base class automatically included in the script, which defines a self variable as well as a way to let the application initialize that variable.

Share on other sites

thanx everyone

@func factory

Ya pointers confuse me most the time, But thanks for the fix and the explaination

@interface vs inheritance

I figured that was what i needed to do :)

thanx

chasester