Quote:
I'm not sure I would assign IDs to object instances. Not only are there several complexities regarding how to differentiate prototype from instance IDs, but also because in the vast majority of cases you don't need them to be uniquely identified beyond their "pointer value" in the code.
I would like to be able to agree with you, because IDs are fiddly. However I keep thinking of cases where, without them, you get into trouble. Without an ID, the only way of disambiguating one item from another is by its internal table reference (value), assuming we store objects (rooms, mobs etc.) as tables with their values being items inside that table.
Now, clearly table references are going to be unambiguous at a given moment, because otherwise they would refer to the same table.
Let's take for an example, there are five mobs in the room, all with the same vnum, but different (ie. 5 instances of the mob). Now the players have decided to target a particular mob, to kill it first. Better to have one dead mob and 4 left alive, than 5 slightly dead mobs. ;)
So, we need to know which mob each one is attacking, and like I say, we probably need to store its table reference. Other methods (like the "4th mob in the room") are likely to change if more mobs enter or leave.
Now say a player puts a curse on a mob, that lasts for quite a while (like, 15 minutes). So we make a timer, that says every minute or so, the mob of that address gets a downgrade to its health.
However the mob gets killed quite quickly, its corpse looted, and is removed from the world. The server then creates a new mob somewhere, and the Lua interpreter happens to re-use that table address (as it is now free). However we still have a timer for that curse, tied to that memory address, so our new mob starts getting damaged for no obvious reason.
You could probably construct a similar example for objects (eg. a sharpened sword, where the sharpening lasts for an hour, but which is subsequently destroyed).
While I was writing all that, a thought occurred to me ... maybe this wouldn't happen after all. This is because, in my example of the curse, the timer with the curse in it amounts to a reference to the table, and thus it can't be garbage collected, and thus the address can't be reused.
You still potentially have the problem of a curse affecting something that is dead, but perhaps when the mob is released from the world, as it were, its table is marked as "dead object", so that any other references to it see that it is dead, and do nothing (things like timers). |