I'm generally struggling with ownership scheme for my game, and was hoping that refCount check would be a way to seal my idea finally. The main problem I have is removing object from game world which should happen when the object is "destroyed". Let me describe what I have now, maybe someone can suggest some idea as I'm totally lost between several approaches, none of whose seems to work 100%.
1. I use ProxyObject on C++ side which has some functionality and is used by script object (it's created together with script object and kept alive as long as script object exists). So this is very similar to what you have in Game example, but I only allow creation of objects through special factory function defined on C++ side, so you can't do
Object @newobject = Object()
inside script as the ctor is private and you can only construct new object by calling
createObject('/some/file.as')
inside scripts.
2. So, inside scripts I use Object base class, it's also inherited by more specialized derived classes like Item, Monster etc. (very simplified, it also makes heavy use of mixins). On C++ side, ProxyObject is inserted into a list of "world objects" and is iterated over, and provides a bridge between C++ and scripting. It's for example used to call methods like tick() on script objects and so on.
3. It's important to note that I keep one reference on C++ side to such script objects, so they stay alive when nothing else refers to them. This is kind of main ownership when object is put into the world (the World references it). Other situations is when the object is contained inside other object - that object then holds the reference to it's children.
3. Various objects will have to refer to other objects somehow and this is where the idea starts to have some weak spots. If I use references to script objects, I'm risking that I won't be able to really destroy the object because if any other object keeps reference to another object, it will keep it alive indefinitely (as long as it's not letting it go). Imagine situation where I want to keep reference to whom attacked me last so I have member Object @lastAttackedBy - once I store it, it adds reference to that object and if we do a coding mistake and keep it while the referred object is destroyed (so World releases it's reference and assumes that the object should now go away) we have problem - as long as that lastAttackedBy is not cleared we have object hanging in limbo, as it should not exist but it does and it can be called on, which may lead to weird situations (object is already removed from the world).
4. So above example shows that references cause a lot of headache when it comes to finalizing life of an object. Not sure how to deal with this. Weakrefs may help a bit, but nothing stops anyone from holding real reference and this won't be easily caught (here comes my question about verying refcount and checking if destroyed objects are not held alive against the logic)
I know this is probably some common problem, I searched around and haven't found any solution that would apply here. Some people use handles (as in, just some int id for the object) and re-request them each time they're needed, which does not keep reference and allows us to get null if the object is already gone. I'm considering this but it adds some overhead in code when we need to always fetch the object before we can use it. But this may be simpler to manage than the reference hell
Any ideas welcome! How do others manage game object lifetime - and I'm talking about more complex situation where objects come and go during the game and need to be taken care of properly to not cause weird behaviour.