Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

How to call script objects constructor?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 05 December 2012 - 06:37 AM

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?

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
0Likes
Like

Posted 05 December 2012 - 07:02 AM

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

#3 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 05 December 2012 - 07:38 AM

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?

#4 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
0Likes
Like

Posted 05 December 2012 - 07:52 AM

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

#5 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 05 December 2012 - 08:21 AM

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?

#6 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
1Likes
Like

Posted 05 December 2012 - 10:33 AM

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

#7 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 05 December 2012 - 11:20 AM

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.

#8 Andreas Jonsson   Moderators   -  Reputation: 3293

Like
1Likes
Like

Posted 05 December 2012 - 12:47 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS