From: "Bernhard Glück" Date: 2001-08-31T01:44:47+09:00 Subject: [ruby-talk:20587] Calling ruby methods from C++ Hi there! I have been integrating Ruby into my game engine, and it works quite nicely, I can export my game objects to ruby classes which are then instantiated in scripts and can be modified very conveniently. Now I want to implement something the following way I have a C++ base class called Object which represents a game object. Many classes are derived from it and are all exportet to the script so I can do for example the following in Ruby m = Mesh.new() m.Load( " test.x" ) m.Show() which creates and displays a Mesh Object ( class Mesh: public Object in C++ by the way ) This all works very good and is stable. Now my game engine is driven by messages e.g the Graphics Parts sends out a message "Collision" to two colliding objects which are then received by the C++ implementation and handeled. Now I want to send messages which the C++ Side cannot handle to the script .. I want to do the following being able to inherit from Exported C++ Classes( which works fine ) and implement additional methods for the object in Ruby... Now when a message is not handeled by the C++ side it should call a method in the assosiacted ruby object (if an appropriate one exists ) Let me clarify with an example Prerequisite Mesh is a class exported from my C++ part AddToGame and Log are module methods from my engine, Log outputs a debug message and AddToGame registers an object with the Game Manager so that it receives messages and everything. In ruby: class SpaceShip < Mesh def OnDoSomething ( dummy ) Log("Just a dummy log output") end end m = SpaceShip.new() AddToGame( m ) Ok the C++ side of the Mesh ( or m as the instance ) does receive a "DoSomething" Message.. it can't handle it in C++ so it sends it too the m script object in which the OnDoSomething method should be called to handle it ... But I cant seem to get that working !!! When Mesh is exported from C++ it is with the following code: (ObjectPtr is a smartpointer by the way ,and the following code is a macro for easy exporting of Object derived classes..(cname is a macro parameter the real class name ) Of particular interest should be the obj->linkScriptClass call where the self VALUE is stored.... shouldn't the self VALUE be a handle to the SpaceShip class instance when used in the code above ? static VALUE cname##New( VALUE self ) \ { \ ObjectPtr * inst = new ObjectPtr(); \ *inst = Factory()->CreateObject(#cname); \ VALUE t = Data_Wrap_Struct(self,0,SE_DELETE(cname##Delete),inst); \ rb_obj_call_init(t, 0, NULL); \ Object * obj = inst->Get(); \ obj->LinkScriptClass( self ); \ return t; \ } \ And when I try to call the SpaceShip method it crashes all the time, I am doing so In the upper cases m_msg contains "DoSomething" and val is the VALUE stored in the above procedure ( the C++ Ruby "Constructor" ) String callString = "On"; callString += m_msg; // Generated for the method ID callId = rb_intern( callString ); // Now create the parameter array for the call... Array args; // And make the script call rb_funcall2_protect( val,callId,args.Size(),args.Begin() ); Any idea how to achieve what I want ? Thanks in advance Bernhard Gl�ck