• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
93i

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

5 posts in this topic

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

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

when i make a script like this:

[CODE]class MyClass : SuperClass
{
}[/CODE]

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()
0

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] ).

Here is his reply:


[quote]
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 Edited by alraz
1

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='alraz' timestamp='1342008689' post='4957995']

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(); }
....
[/quote]

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:
[code]
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);
}
[/code]

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?
0

Share this post


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

[code]
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;
}
[/code]


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:

[code]
// 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;
}
[/code]

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

[code]
// 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
}
[/code]

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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0