ixM
7th February 2012, 22:43
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:
class Wrapper_Drawable : public QObject, public QScriptable
{
Q_OBJECT
public:
explicit Wrapper_Drawable(Drawable* drawable = 0);
virtual Drawable* drawable() const;
public slots:
QScriptValue getPosition() const;
QScriptValue setPosition();
protected:
Drawable* m_drawable;
};
Q_DECLARE_METATYPE(Wrapper_Drawable*)
class Wrapper_MeshDrawable : public Wrapper_Drawable
{
Q_OBJECT
public:
Wrapper_MeshDrawable(MeshDrawable* meshDrawable = 0);
virtual MeshDrawable* drawable() const;
public slots:
QScriptValue addTriangle();
};
Q_DECLARE_METATYPE(Wrapper_MeshDrawable*)
I declare those classes to the script engine:
Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
wrapper_drawable_obj.setPrototype(m_scriptEngine->newObject());
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(), wrapper_drawable_obj);
m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);
Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
wrapper_meshDrawable_obj.setPrototype(wrapper_draw able_obj);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(), wrapper_meshDrawable_obj);
m_scriptEngine->globalObject().setProperty("MeshDrawable", wrapper_meshDrawable_obj);
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
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:
class Wrapper_Drawable : public QObject, public QScriptable
{
Q_OBJECT
public:
explicit Wrapper_Drawable(Drawable* drawable = 0);
virtual Drawable* drawable() const;
public slots:
QScriptValue getPosition() const;
QScriptValue setPosition();
protected:
Drawable* m_drawable;
};
Q_DECLARE_METATYPE(Wrapper_Drawable*)
class Wrapper_MeshDrawable : public Wrapper_Drawable
{
Q_OBJECT
public:
Wrapper_MeshDrawable(MeshDrawable* meshDrawable = 0);
virtual MeshDrawable* drawable() const;
public slots:
QScriptValue addTriangle();
};
Q_DECLARE_METATYPE(Wrapper_MeshDrawable*)
I declare those classes to the script engine:
Wrapper_Drawable* wrapper_drawable = new Wrapper_Drawable();
QScriptValue wrapper_drawable_obj = m_scriptEngine->newQObject(wrapper_drawable);
wrapper_drawable_obj.setPrototype(m_scriptEngine->newObject());
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_Drawable*>(), wrapper_drawable_obj);
m_scriptEngine->globalObject().setProperty("Drawable", wrapper_drawable_obj);
Wrapper_MeshDrawable* wrapper_meshDrawable = new Wrapper_MeshDrawable();
QScriptValue wrapper_meshDrawable_obj = m_scriptEngine->newQObject(wrapper_meshDrawable);
wrapper_meshDrawable_obj.setPrototype(wrapper_draw able_obj);
m_scriptEngine->setDefaultPrototype(qMetaTypeId<Wrapper_MeshDrawable*>(), wrapper_meshDrawable_obj);
m_scriptEngine->globalObject().setProperty("MeshDrawable", wrapper_meshDrawable_obj);
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