• Advertisement
Sign in to follow this  

How to call script objects constructor?

This topic is 1902 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


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?

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

  • Advertisement