As I have few unrelated questions I picked generic topic instead of trying to divide these questions into more threads, hope its ok.
1) I'm using AngelScript for inline script events in libRocket UI library. This means that whenever there is event encountered in code (that looks like typical Javascript HTML event - onclick="dosomething()" etc.) I generate and compile function like this:
[source lang="cpp"]
std::string code = "void InlineEventFunc(Event @event) {\n" + m_ScriptSource + ";\n}";
r = m_Module->CompileFunction(section.c_str(), code.c_str(), -1, 0, &m_Func);
[/source]
This works well, but I want to achieve something that works like in Javascript where there is a special "scoped" variable this that holds element which just received this event. Is there a way to register such special variable not using global registration through asIScriptEngine, which will be global for all scripts and I don't want this. I just want it to be global for certain module and set it just before executing above function.
PS. While typing this question I suddenly realized I can pass this special variable to function when I call it, just by extending its argument list like this:
[source lang="cpp"]
std::string code = "void InlineEventFunc(Event @event, Element @this) {\n" + m_ScriptSource + ";\n}";
r = m_Module->CompileFunction(section.c_str(), code.c_str(), -1, 0, &m_Func);
[/source]
This should work I think in this case, but I still wonder if its possible to set module-scope variable and how is it done?
2) How do you solve problem of many similar value types in AngelScript? For example, I use std::string as my base string type and have it registered in AS, but library I mentioned above uses its own Rocket::Core::String type.
Every function I register that takes or returns this Rocket::Core::String requires me to wrap it with a function that will convert it on the fly from/to std::string/Rocket::String. This way I can stop worrying about converting back and forth between these two types (and Rocket::Core::String does not converter or constructor from std::string). I do it this way:
[source lang="cpp"]engine.RegisterObjectMethod(name, "void SetId(const string &in)", asFUNCTION(Element_SetId), asCALL_CDECL_OBJLAST);
void Element_SetId(const std::string& id, Core::Element* obj)
{
obj->SetId(id.c_str()); // SetId takes Core::String as an argument, but it also takes C string as constructor so it works
}[/source]
Is it the best way to solve problem with many different types introduced by various libraries? I will have the same problem with my vector class

3) Virtual methods - maybe I'm blind but I couldn't find how something like this would behave when registered in AS:
[source lang="cpp"]class A
{
virtual void Foo() { foo(); }
}
class B: A
{
virtual void Foo() { b_foo(); }
}
[/source]
Then do I do this to register class A:
[source lang="cpp"]
engine.RegisterObjectMethod("A", "void Foo()", asMETHOD(A, Foo), asCALL_THISCALL);
[/source]
but when I register B, do I have to do only this:
[source lang="cpp"]
engine.RegisterObjectMethod("B", "void Foo()", asMETHOD(B, Foo), asCALL_THISCALL);
[/source]
or this:
[source lang="cpp"]
engine.RegisterObjectMethod("B", "void Foo()", asMETHOD(A, Foo), asCALL_THISCALL);
engine.RegisterObjectMethod("B", "void Foo()", asMETHOD(B, Foo), asCALL_THISCALL);
[/source]
Will the second try to register "B", "void Foo()" override A's implementation, or it won't work? Should I only register B's implementation there? I ask mostly because if I template parent class so I can easily register its methods for children classes like this:
[source lang="cpp"]
// Register base class A
RegisterAMethods("A");
// Register child class B
RegisterAMethods("B");
RegisterBMethods();
[/source]
If RegisterAMethods already registers "Foo" for A's implementation, then RegisterBMethods registers it again, will the latter work? If not, I have to somehow pass info to RegisterAMethods to exclude its own Foo() when I use it to register child class.
I hope I make sense here, just trying to understand how virtual methods work in AS and how they are registered. If there is some page dedicated to this in manual, I'd gladly read it, but I just couldnt find anything specific on virtual methods.