Hello,
I'm following similar approach as is demonstrated in Angelscript's "game" sample - so I have a C++ object that provides some very low level functionality (positioning, rendering, physics). These are all registered with Angelscript and available for that C++ object, lets call it GameObject. Then I want to achieve something very similar to what happens in sample game - I have various script-side objects that all refer to instance of this C++ side object. I just don't want to do this in such explicit way, where the object is expected to be passed in constructor and is then provided upon instantiating script object on C++ side (game/source/scriptmgr.cpp:230). This has one drawback - it requires the class to always provide this constructor that takes GameObject@ as a single argument.
What I wanted initially, was to make a script-side object that will be a wrapper around C++ level functionality, so it won't require a convention of calling C++ methods like this: `self.someMethod` but will provide a wrapper that can be then inherited from, allowing us to completely hide this bridge between AS and C++:
shared abstract class ScriptObject: IController
{
ScriptObject(GameObject@ obj)
{
@self = obj;
}
void render()
{
self.render();
}
void move()
{
self.move();
}
}
class Npc: ScriptObject
{
Npc()
{
# setup npc
}
}
class Monster: Npc
{
Monster()
{
# setup monster
}
}
Above is some pseudocode of what I mean - notice how child classes do not define constructors that take GameObject - this is my main problem with the AS's game implementation, because it requires EVERY class to provide this constructor, and having many objects this is a lot of code that should be transparent to the user, and provided "by default".
So the main question of this post is: IS IT DOABLE? And if so, how can it be achieved? I was thinking about requiring only empty constructor, and then providing "self" by a setter on base class (ScriptObject) through IController interface or something like this, but maybe there is a better way? Has anyone tried something similar? I think it could be great if we had a way to hide this bridge between C++ and script and make the base script class a bit more transparent.
PS. (EDIT) Of course, rubber duck programming had to kick in, once I explained it and looked at official docs, I found another method which does not take the C++ object when constructing script object, but reverses the situation - it creates C++ object itself in a constructor, so it's always guaranteed to be constructed (http://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_inheritappclass.html). It's interesting solution and I need to check if this does what I need fully, but if anyone has other ideas how to make the whole process more automated and less reliable on implementing it in every possible class in hierarchy - please share them!