Overrid C++ methods from angelscript, possible? alternatives?

Started by
4 comments, last by 93i 11 years, 8 months ago
Hi,

i am new to angelscript and i'm trying to make a game engine scriptable, where i have C++ objects and i want certain methos of this objects be overwriteable from angelscipt.

I have the class registered with

engine->RegisterObjectBehaviour("SuperClass", asBEHAVE_FACTORY, "SuperClass@ f()", asFUNCTION(SuperClass_Factory), asCALL_CDECL);

when i make a script like this:

class MyClass : SuperClass
{
}


i get a error message:

Can't inherit from class 'SuperClass' marked as final

Can i 'unset' the final, or is this not possible, and if it is not possible, what would be the best (as in the easiest for the script author) to achieve a similar beavour, so that a script editor can overwrite methods, like for example onCollision()
Advertisement
Hello.

I asked the very same question to Andreas not long ago. And he told me of a work-around for this, (as well as why it doesn't work wink.png ).

Here is his reply:



Unfortunately, it is not possible to inherit directly from application registered objects as they are not instances of asCScriptObject.

It is possible to implement a script proxy class that gives a light wrapper on top of the application registered object, and then have the script classes inherit from the proxy instead. For example:


shared class FSMProxy
{
FSMProxy() { @inner = FSM(); }
bool ChangeState(string v) { return inner.ChangeState(v); }
string CurrState {
get const { return inner.CurrState; }
set { inner.CurrState = value; }

private FSM @inner;
}


The script code for the proxy class can be generated by the application and added as a secondary script section to the script modules that will use the FSM class.

Regards,
Andreas
[/quote]


In this case "FSM" is my SuperClass and FSMProxy is the derived class
format c: /q
If you really want to use inheritance, then the post above is the best alternative. However, there are other ways of allowing scripts to customize behaviours for game entitites.

Take a look at the game sample in the SDK for example. It links a script class, called the "controller", to the C++ object. Whenever an event occurs on the object, the C++ class checks if the script class implements the respective event handler, and if so makes the call. The C++ object can provide its own default handlers when the script class doesn't implement any overrides, for example, simple collision response based on physics simulation.

Regards,
Andreas

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



It is possible to implement a script proxy class that gives a light wrapper on top of the application registered object, and then have the script classes inherit from the proxy instead. For example:


shared class FSMProxy
{
FSMProxy() { @inner = FSM(); }
....


this works fine in general, but now i have the following problem:

i have a class 'Drawable' that provides all functions for drawable objects, like move, rotate and so on... then there are class like 'Image' that does provide functionality on top of this class. so i have:

class Drawable {
Drawable() {
@drawableInner = NativeDrawable();
}

function move(float x, float y) {
drawableInner.move(x, y);
}
NativeDrawable @drawableInner;
}

class Image : Drawable {
Image() {
@imageInner = NativeImage();
}
NativeImage @imageInner;
}

function test() {
@img = Image();
img.move(10, 0);
}


So this example would move the '@drawableInner' c++ object, but not the @imageInner of Image.

Ofcourse i can reimplement all the move and rotate and so on function in my Image wrapper, but thats ugly, is there a better way?
The Image class has to implement an override for the move() method, because the Drawable base class doesn't know anything about the derived Image class.


class Image : Drawable
{
Image() {
@imageInner = NativeImage();
}
void move(float x, float y) { // Override the base class' move() method
imageInner.move(10,0);
Drawable::move(x, y); // Call the base class' move() method too
}
NativeImage @imageInner;
}



Referring back to my previous post. Perhaps you should really consider not inherit from the C++ classes? Instead provide C++ classes that can allow a script object to override part of the behaviour? For example:


// C++
//
class ScriptableDrawable : public Drawable
{
ScriptableDrawable()
{
scriptObject = 0;
}
void move(float x, float y)
{
if( scriptObject && hasMoveMethod )
{
// Call the move() method on the script object
....
}
else
{
// Fallback to the original implementation
Drawable::move(x, y);
}
}
asIScriptObject *scriptObject;
}


Now expose this ScriptableDrawable class to the script instead of the original Drawable class. The script can then create the instance of the ScriptableDrawable class and then provide a class to override the behaviour if desired. C++ on the other hand can treat the ScriptableDrawable class just as a normal Drawable class, without even caring that it might have a script object doing the actual execution


// AngelScript
class MyDrawable
{
MyDrawable(Drawable@ d) {
@d.scriptObject = this; // Tell the C++ object that MyDrawable will provide the implementation
@drawable = d; // Keep a reference to the original C++ object, so it can be manipulated
}

void move(float x, float y) {
// Override the default C++ behaviour
x *= 2; y *= 2;
drawable.x += x;
drawable.y += y;
}

Drawable @drawable;
}
void func()
{
Drawable draw; // Creates the ScriptableDrawable object
MyDrawable(draw); // Creates the script object and bind it to the ScriptableDrawable object

draw.move(x,y); // Call the move method on the ScriptableDrawable object, which will in turn call the move method on the MyDrawable object
}


You'll do the same for the Image class of course, i.e. Implement a proxy ScriptableImage class and expose that to the script instead of the original Image class. As the ScriptableImage class inherits from Image that inherits from Drawable, the ScriptableImage class can be treated by C++ as a normal Drawable class.

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

Ok, i'll give it a try, might work better

This topic is closed to new replies.

Advertisement