Virtual functions

Started by
7 comments, last by meink 19 years, 11 months ago
Virtual functions and derived classes aren''t supported in AngelScript, but I''m curious to know what sort of workarounds people are using. I''ve set my class up so you can register new derived classes in script (http://noreality.net/files/programming/astest.zip - An old proof of concept that outlines the idea), and I''ve expanded this in my engine so that you can migrate the derived class into C++, but the AS code still uses the same interface. I''ll get around to making that into an example file soon. Is this the method that most people are using? Are other people doing things like this? I was thinking about perhaps writing it up into a tutorial... - Xavier
Advertisement
A tutorial would be excellent. If you want I can host it on my site, or if you prefer I''ll only put a link to it.

I''ll check out your solution when I get the time.

I have plans to support virtual functions natively, but it is still quite far down on the todo list.

__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library - Tower - free puzzle game

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

I checked out the code. It is a very good example that clearly shows how things can be done. It is basically how I imagined AngelScript would be used.

I would put a comment in the code for Item::collidePlayer() that it can be optimized by storing the function ID in a lookup tables so that GetFunctionIDByName() doesn''t have to be called every time. Also, it might be a good idea to use some kind of pooling for contexts, so that you don''t have to create and release them each time they will be called. If each call to Prepare() uses the same stackSize the context reuses the memory it previously allocated, thus saving some more time.

I must say that I misunderstood you when you mentioned virtual functions earlier. I thought you meant that AngelScript would call virtual C++ class methods and allow polymorphism. This is what I plan to support in the future.

__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library - Tower - free puzzle game

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

Yeah, I added many of those things you mentioned into my updated version (which I''ll be using for the tutorial).

For calling virtual functions from AS, I thought of having a static member function that calls this->function(). If the function was overridden in C++ it would be called, other wise the base implementation would dispatch the code to the AS defined function. Haven''t tested it though. I''ll check it out next week - I don''t have my comp for the weekend

- Xavier
I look forward to read your tutorial.

__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library - Tower - free puzzle game

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

http://noreality.net/as-polymorphism.zip

First draft of "Polymorphism in Angelscript". I''m hoping this is actually what polymorphism is - I''m a little sketchy on definitions - otherwise I''m going to have to change the title :S

I left a few things out, which I''ve mentioned at the end. I thought it would be better to focus on the core idea, and leave the rest as an exercise for the reader (or a later tutorial...)

I''m thinking of submitting to the gamedev scripting section, and would appreciate any feedback. I''m not exactly an expert writer...

- Xavier
I''ve written a precompiler which reads through your header files and generated a cpp file which registers all the variables and functions in all the classes/globally. If there''s enough demand I''ll try to make it a bit easier to use and make it available - it''s currently in a state where only I can use it.

One extra thing it does is make wrapper functions for all the ctors & dtors. If I was wanting to use polymorphism, I probably would have got the precompiler to generate non-virtual wrappers for all the virtual functions. As inheritence is still on the way, it''s unlikely that many people are useing virtual functions with AS.
meink:

I read the tutorial now and I thought it was very good. I especially liked the way you solved dynamic properties for the CItem class, I hadn''t thought about that before.

Of course, at a later time AngelScript will support declaration of structs so it should no longer be necessary to do it like that.

I suggest you write what version of AngelScript you wrote the tutorial for as things may change in future versions.

AndyMan:

You know people would be interested in seeing a pre-compiler that helps with the configuration of the engine. If you are short on time (like me) maybe you could release it as it is so that others can improve upon it, or learn from it and write their own pre-compilers.

__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library - Tower - free puzzle game

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

quote:Original post by WitchLord
AndyMan:

You know people would be interested in seeing a pre-compiler that helps with the configuration of the engine. If you are short on time (like me) maybe you could release it as it is so that others can improve upon it, or learn from it and write their own pre-compilers.


Someone might look into writing a new backend for SWIG.

SWIG is a popular C++ wrapper generator that supports lots of scripting languages. It can even handle wrapping templates, etc.

No idea what is involved with writing a new backend, though.

-Wade

This topic is closed to new replies.

Advertisement