Although I did this a couple of times from scratch, still no solution really fits.
I'm using lua for scripting in my games. Lua holds the "prototypes" of the game elements, that are copied to each entity on it's creation. A prototype represents a class of objects ("Soldier with shotgun"). Also, each prototype may hold event hooks (OnDie, OnSpotEnemy, etc). Clearly it's a prototype, not the object that will be handled.
So, each time a hook is executed, I need to create an object to represent the calling entity (and any entities it interacts with) only for the lifetime of a single script execution. What it means is that a instance of the object lives in lua only for the time needed to run this script.
Simple execution of this idea wasn't much work -- I used
lightuserdata to hold the C++ pointer to the entity encapsuled in a
being class. To access the fields of the C++ object, I overloaded the metatable of
being. However, the fact that the fields of the object are handled by a metatable practically breaks any reasonable implementation of inheritance.
I pondered with the usage of normal userdata, but I'm afraid that the dynamic allocation needed to run each script (some of them being ran each tick in the game loop) are going to ruin performance.
Any suggestions? Or maybe a better solution altogether?