introspection

Started by
10 comments, last by WitchLord 17 years, 12 months ago
i was wondering if there is a way to obtain the class name (or type name) for a given variable. something like: (as) class MyClass {}; MyClass thisObject; if (typeof("thisObject") == "MyClass") ... how could (if is possible, of course) i declare the typeof() global function ?
Advertisement
The simplest way, if it can't be easily added to the language, would be to let the overload resolution system handle it. You can bind a family of functions with the signature 'string typeof(const int& in) { return "int"; }' Bind one such function for every type, and it should work exactly how you want.
yes this could be a way to go... but if i define a class in the script i can't know in advance what kind of class name i've declared it. ah maybe i can register dynamically from the script a new typeof() funtion which takes the new declared class... mmmh i have to think about it. would be useful to have a GetObjectTypeName(int variableID) in CScriptEngine, which works cooperatively with GetGlobalVarIDByName.
At the moment a typeof() function is quite useless, since there is no polymorphism available. Once I've completed the polymorphism functionality I may also implement a typeof() function, or at least make it easier to register one from the application side.

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 know, but from my point of view i actually need to know the class name declared in the script in order to get its implemented methods to be used from a c++ class, which is impossible if i don't know the name of the class the scripter could write (and he could use whatever namespace he wish). i have a turnaround for this actually, having a method in which you manually specify the class name and variable name that gets binded with the implemented script behaviours.

thanx however
So, this is something you want to call from C++, not from inside the script? IIRC, the structures used to pass script declared classes to the app have a type id in them. Can't you use that id to look up methods?
i actually have something like this.

script:
class MyComponent {   //------------------------------------------------------  MyComponent(const String& thisObjectName)   {     this.self.addToDesktop(ComponentPeer.windowHasDropShadow);    this.self.setClassAndObject("MyComponent",thisObjectName);  }    //------------------------------------------------------  void mouseDown(const MouseEvent& in e)   {     this.myDragger.startDraggingComponent (this.self);       }  void mouseDrag(const MouseEvent& in e)   {     this.myDragger.dragComponent (this.self, e);       }    //------------------------------------------------------  void paint(Graphics& g)   {    Random r;    Path p;    AffineTransform t;    t.scaled(0.8f,0.6f);     for (int i=0; i<10; i++)    {        p.addLineSegment(rand.nextFloat()*w,               rand.nextFloat()*h,               rand.nextFloat()*w,               rand.nextFloat()*h,               2+rand.nextFloat()*20);        p.quadraticTo(rand.nextFloat()*w,               rand.nextFloat()*h,               rand.nextFloat()*w,               rand.nextFloat()*h);    }    gfx.setColour(Colour(1.0f,1.0f,0.0f,0.2f));    gfx.fillPath(p,t);    gfx.setColour(Colour(0.0f,0.0f,0.0f,0.8f));    gfx.strokePath(p,t);  }  //------------------------------------------------------  void timerCallback()  {    this.self.repaint();  }  //------------------------------------------------------  Component self;  ComponentDragger myDragger;};  //------------------------------------------------------MyComponent myObject("myObject");  void main() {  c.self.centreWithSize(200,200);  c.self.setVisible(!c.self.isVisible());  c.self.startTimer(50);} 


actually the real c++ component is the Component class, that let's you interact with the real object, the only way to attach MyComponent behaviours to the c++ Component is to pass to the engine side the class name you currently declared in the script, either with the global object name that you instantiate. this is not a real problem, but calling self.setClassAndObject("MyComponentScriptClassName",...) somehow is a repetition if i have a way to call self.setClassAndObject(typeof(this),...)

just asking....
Interesting problem. I'm actually doing something similar, but from the other side. My C++ code needs to 'automagically' know the name a type was bound to angelscript with. I end up with a simple function call: type_name<std::string>(). This function uses typeid() and a map to look up the string. If your concern is only code repetition, you can always just use a macro. :)

What, you aren't using my preprocessor? Why not?
yes Deyja, there are thousands ways to proceed and turn around the walls that are sticking in front of us... your way is something more easy cause you could rely on rtti, i was just trowing my 2 cent of having a basic sort of rtti in angelscript too: "just give me the name of a declared variable, Me, Powerful AngelScript Engine, i'll summon the classes you're looking for !"... eheh ;)

ah about the preprocessor, i've downloaded a couple of weeks ago and look at it, very nice indeed, is one of the things i want to have next in my engine implementation... thanx for the support
You could use the asIScriptAny interface for this. In the script it would look something like this:

  MyComponent(const String& thisObjectName)   {     this.self.addToDesktop(ComponentPeer.windowHasDropShadow);    this.self.setClassAndObject(any(@this),thisObjectName);  } 


The application code would be something like:

void setClassAndObject(asIScriptAny &obj, string &objName){  int typeId = obj->GetTypeId();  typeId &= ~asTYPEID_OBJHANDLE;  string typeName = engine->GetTypeDeclaration(typeId);  // Do whatever you do with the typeName and object name  ...}


It may not be as nice as a typeof() function, but it works for now.

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

This topic is closed to new replies.

Advertisement