🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Factory Functions

Started by
5 comments, last by chasester123 8 years, 7 months ago

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

Advertisement

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.

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

again very helpful, thanx very much :)

chasester

again very helpful, thanx very much smile.png

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

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.

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

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.

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

This topic is closed to new replies.

Advertisement