Sign in to follow this  
Bismuth

Ideas for class inheritance

Recommended Posts

Hey! I've been following the development of Angelscript for quite a while. It has made some great progress in the past few years. Yet, it still lacks a few things I'd love to see. I've come up with a few ideas and requests to share, these are just a few. Final Functions (Methods) Sometimes you (as a developer) would like to give your users the ability to write their own scripts, but there are a few cases where one has to restrict certain actions from being taken. Suppose you have script classes A, B and C. Class A is the base class, B inherits from A and C inherits from B. Your users are free to write their own classes that inherit from any of these. Class B implements a special function (method) that you don't want your users to replace in an inherited class. It should remain constant throughout all derived classes. Final methods are basically class functions that cannot be overridden in any subclass. They are only valid inside classes and can be declared by a final keyword.
// This is the base class
class Monster {
	int health;
	
	// Here is a standard method
	void Hit(int damage) {
		health -= damage;
		if (health <= 0) {
			health = 0;
			Die();
		}
	}
	
	// Here is our final function
	final void Die() {
		Spawn("Blood");
		Unspawn("Self");
	}
	
	void Shoot() {
		// Shooting stuff code
	}
}

// Here is a derived class
Class BetterMonster : Monster {
	
	// This class inherits all member functions and variables from the base class.
	
	// A standard method can be replaced by simply inserting new code.
	void Hit(int damage) {
		health -= damage;
		Spawn("Blood");
		if (health <= 0) {
			health = 0;
			Die();
		}
	}
	
	// Trying to override a final function throws an error.
	void Die() {
		Spawn("Butterflies");
		Unspawn("Self");
	}
}

Script inheritance for registered classes I've noticed Angelscript supports script class inheritance, but lacks registered class inheritance. It seems that a registered class can inherit from another registered class, but a script class can only inherit from another script class and cannot inherit from a registered class. Too bad, because I was looking to create a base-most class that implements some c++ accelerated class methods for all subclasses to use. It could be a nice addition, so I'm adding it to my wish list. Also, I'm not sure about overriding inherited registered methods. Perhaps all registered methods should automatically become final. There. I have a few more ideas on my paper, but I'll share those another day. Regards, Bismuth

Share this post


Link to post
Share on other sites
That's an interesting way of faking the inheritance you described there, SiCrane. In fact, you don't even need to register any classes to use c++ accelerated methods. Instead you register a few normal functions and then call them from methods in the "root" class. Any subclass will inherit them. Yet, it's still possible to override them. I'd just like to see a way to prevent script writers from overwriting these methods (accidentally or deliberately). While your method is a nice workaround, I'm more concerned that using such approach would only confuse end-users and script writers. It should be fine for behind-the-scene scripts though.


Okay. Adding stuff to my list, there's also the concept of final classes. A Wikipedia article on Java classes describes them as "A final class cannot be subclassed". Pretty simple, if you don't want your users to create any subclasses.

There are also final variables, but I'm unsure about those.

Wikipedia article

Share this post


Link to post
Share on other sites
As a C++ developer I've never felt the need for final methods, which probably because the language doesn't have this support built-in. I can see the usefulness of it in a scripted environment where the application may provide the default script class. I'll add this to my to-do list, though it will probably be a long time before I get to implement this.

Script inheritance of registered classes is already on my to-do list, but it is not an easy subject due to the differences in technology. I have several ideas of how it can be implemented, but all of them have some drawbacks. For now, this too is quite far down on the priority.

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