Hi everyone,

I'm trying to use QtScript to allow "users" (just me for now :P) to control the game-engine I'm creating (nothing complicated as I'm just doing that for fun).

I've decided to create wrappers for all the classes I want to expose to the script engine. Those wrappers inherit from QObject and QScriptable.

As an example, let's consider the following classes:

  • Drawable (abstract): contains all information that all drawable objects share (position, name, parent, children,...)
  • MeshDrawable (extends Drawable): implements pure virtual functions for loading and drawing a mesh
  • Wrapper_Drawable: wraps a Drawable
  • Wrapper_MeshDrawable (extends Wrapper_Drawable): wraps a MeshDrawable


Let's concentrate on the last two classes:

Qt Code:
  1. class Wrapper_Drawable : public QObject, public QScriptable
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit Wrapper_Drawable(Drawable* drawable = 0);
  6.  
  7. virtual Drawable* drawable() const;
  8.  
  9. public slots:
  10. QScriptValue getPosition() const;
  11. QScriptValue setPosition();
  12.  
  13. protected:
  14. Drawable* m_drawable;
  15. };
  16.  
  17. Q_DECLARE_METATYPE(Wrapper_Drawable*)
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. class Wrapper_MeshDrawable : public Wrapper_Drawable
  2. {
  3. Q_OBJECT
  4. public:
  5. Wrapper_MeshDrawable(MeshDrawable* meshDrawable = 0);
  6.  
  7. virtual MeshDrawable* drawable() const;
  8.  
  9. public slots:
  10. QScriptValue addTriangle();
  11. };
  12.  
  13. Q_DECLARE_METATYPE(Wrapper_MeshDrawable*)
To copy to clipboard, switch view to plain text mode 

I declare those classes to the script engine:

Qt Code:
  1. Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
  2. QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
  3. wrapper_drawable_obj.setPrototype(m_scriptEngine->newObject());
  4. m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(), wrapper_drawable_obj);
  5. m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);
  6.  
  7. Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
  8. QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
  9. wrapper_meshDrawable_obj.setPrototype(wrapper_drawable_obj);
  10. m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(), wrapper_meshDrawable_obj);
  11. m_scriptEngine->globalObject().setProperty("MeshDrawable", wrapper_meshDrawable_obj);
To copy to clipboard, switch view to plain text mode 

But it doesn't do what I'm expecting (these are my questions BTW):

  • If I skip the last two lines of the two blocks above, and do qDebug() << wrapper_meshDrawable_obj.instanceOf(wrapper_drawab le_obj);, the result is false. How is that possible? Am I setting the prototype chain wrong?
  • I have read somewhere (or think I've read) that even just using the 4th line of the two blocks above (setDefaultPrototype()) should let the engine know that when it creates a new QScriptValue from a Wrapper_MeshDrawable, its prototype is the one of Wrapper_Drawable. Isn't that correct?
  • When I print the properties of Drawable, it has none. When I print the properties of MeshDrawable, it has all the properties of Drawable but not his. It seems that there is a shift in the prototype chain.


I hope my questions are clear but I would be happy to give any further information if you need some to help me.


Thank you very much in advance!

Regards,


Maximilien