How to call script objects constructor?

Started by
6 comments, last by WitchLord 11 years, 4 months ago

asIScriptObject *obj = static_cast<asIScriptObject*>(engine->CreateUninitializedScriptObject(t->GetTypeId()));

//manually initialize object properties
//Call default constructor, but how?


That is what i want to do.

How do i call default constructor of an existing object?
Advertisement
If you use CreateUnitializedScriptObject() it is exactly because you don't want to call the constructor. This way of creating an object is used when you want to initialize each of the members manually, presumably for deserializing an object from a file.

If you want to create an initialized script object, then either use CreateScriptObject() that calls the default constructor, or call the script object's factory function if you want to use a constructor other than the default constructor.

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

i need to do it :(

here is why


class Object
{
RigidBody @rigidBody;

Object()
{
rigidBody.setVelocity(Vec3(1,0,0));
}
};


idea is that when i create this object in c++:
- assign properties
- call constructor
- constructor now has all needed properties to properly initialize object.

alternative is that i force classes to have a init() function that i call after CreateScriptObject.
But then rigidBody in the example above will be null and cause all sorts of confusion for those who wants to use constructor.

So, there are no ways of accomplishing this?
Why not pass the rigid body as an argument to the Object constructor?


class Object
{
RigidBody @rigidBody;
Object(RigidBody @rb)
{
@rigidBody = rb;
rigidBody.setVelocity(Vec3(1,0,0));
}
}


Then call the factory function to create the object, instead of calling CreateScriptObject().

You find the factory function by calling asIObjectType::GetFactoryByDecl("Object @Object(RigidBody@)").

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

in that case i force
-create a constructor with a specific parameters
-must do all the correct assignments in it

that's too much to ask for the scripter.

during design phase i thought asIObjectType::GetMethodByIndex also returns constructors. Combined with CreateUninitializedScriptObject it was enough.

Is there a design limitation for GetMethodByIndex not returning constructors?
Just like in C++ the constructor is not an ordinary method. The constructor expects the memory to be unused and it will do all the initialization.

You're asking to call the constructor on an already (partly) initialized object. This way you may end up with memory leaks as the constructor would overwrite the members that you manually initialized.

If you absolutely must use CreateUnitializedObject() and then manually initialize some of the members before letting the script initialize the rest, then you can only do so with an ordinary class method, e.g. Init().

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

Okay, i am gonna ignore the default constructor.

You are working on member initialization in declarations, this will help to reduce usage of default constructor, Great :)
Thank you.

On an unrelated note,
Do you have any plans to add compile time type deduction?
like auto in c++ or var in c#. coz we all are a little lazy.
Possibly. With mixin classes (and possible future mixin functions) 'auto' can certainly make code reuse easier. But it is definitely not a high priority item for me.

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